-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.ino_.backup
223 lines (184 loc) · 4.9 KB
/
timer.ino_.backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <TimerOne.h>
#include "TM1637.h"
#define ON 1
#define OFF 0
int8_t TimeDisp[] = {0x00, 0x00, 0x00, 0x00};
unsigned char ClockPoint = 1; // 双跳灯
unsigned char Update;
unsigned char halfsecond = 0;
unsigned char second = 0;
unsigned char minute = 0;
unsigned char hour = 0;
bool alertEnable = false;
unsigned char alertEnableCount = 0;
// 时钟引脚, Arduino Nano 只有 2,3 引脚支持 attachInterrupt
#define CLK 2
#define DIO 3
TM1637 tm1637(CLK, DIO);
const int buzzPinAnalog = A0; // 蜂鸣器
const int vibratorPinAnalog = A1; // 震动模块
const int resetButtonPinAnalog = A2; // 重置开关按键
// 旋钮
// http://www.dfrobot.com.cn/goods-1421.html
const int encoderButtonPin = 7; // 旋钮开关
const int encoderPinA = 8; //
const int encoderPinB = 9; //
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
void setup()
{
Serial.begin(9600);
Serial.println("TIMER SETUP");
pinMode(buzzPinAnalog, OUTPUT); // 定义蜂鸣器引脚为输出引脚
pinMode(vibratorPinAnalog, OUTPUT); // 定义震动模块引脚为输出引脚
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(encoderButtonPin, INPUT);
digitalWrite(encoderPinA, HIGH); // turn pullup resistor on
digitalWrite(encoderPinB, HIGH); // turn pullup resistor on
// 时钟初始化,定义计时器中断服务程序
tm1637.set();
tm1637.init();
Timer1.initialize(500000); // timing for 500ms
Timer1.attachInterrupt(TimingISR); // declare the interrupt serve routine:TimingISR
}
void loop()
{
// int sensorValue = analogRead(resetButtonPinAnalog);
// Serial.print("sensorValue ");
// Serial.println(sensorValue);
// if (sensorValue == 8)
// {
// // TODO: 切换显示模式
// Serial.println("you sensor button down!!!");
// }
// 在 loop 函数中代替 attachInterrupt 来监听更新 Encoder
UpdateEncoder();
// 输出 encoder value
// TODO: 用于设置时间或报警时长
Serial.println(ReadEncoderValue());
// 重置按钮按下时,数值归零
if (IsEncoderButtonPushDown())
{
Serial.println("you push button down!!!");
alertEnableCount = 0;
ClockPoint = 1;
halfsecond = 0;
second = 0;
minute = 0;
hour = 0;
Update = OFF;
TimeUpdate(); // 更新时钟 TimeDisp
tm1637.display(TimeDisp); // 更新数码管显示
analogWrite(vibratorPinAnalog, 256); // 震动提示
return;
}
else
{
Alert(false, false);
analogWrite(vibratorPinAnalog, 0); // 震动提示
}
// 每分钟报警一次
if (second == 59)
{
Alert(true, true);
delay(50);
}
else
{
Alert(false, false);
}
if (Update == ON)
{
TimeUpdate();
tm1637.display(TimeDisp);
}
}
// TimingISR 计时器中断服务程序
void TimingISR()
{
halfsecond++;
Update = ON;
if (halfsecond == 2)
{
second++;
if (IsEncoderButtonPushDown())
{
alertEnableCount++;
if (alertEnableCount == 5)
{
alertEnable = !alertEnable;
alertEnableCount = 0;
}
}
if (second == 60)
{
minute++;
Serial.println("Another minute passed.");
if (minute == 60)
{
hour++;
if (hour == 24)
hour = 0;
minute = 0;
}
second = 0;
}
halfsecond = 0;
}
ClockPoint = (~ClockPoint) & 0x01;
}
// TimeUpdate 更新数码管显示时间
void TimeUpdate(void)
{
tm1637.point(ClockPoint ? POINT_ON : POINT_OFF);
// 时钟
// TimeDisp[0] = hour / 10;
// TimeDisp[1] = hour % 10;
// TimeDisp[2] = minute / 10;
// TimeDisp[3] = minute % 10;
TimeDisp[0] = minute / 10;
TimeDisp[1] = minute % 10;
TimeDisp[2] = second / 10;
TimeDisp[3] = second % 10;
Update = OFF;
}
// UpdateEncoder 更新 360 度编码器开关数值
void UpdateEncoder()
{
int MSB = digitalRead(encoderPinA); // MSB = most significant bit
int LSB = digitalRead(encoderPinB); // LSB = least significant bit
int encoded = (MSB << 1) | LSB; // converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; // adding it to the previous encoded value
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
encoderValue++;
if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
encoderValue--;
lastEncoded = encoded; // store this value for next time
}
// ReadEncoderValue 读取 360 度编码器数值
long ReadEncoderValue(void)
{
return encoderValue / 4;
}
// IsEncoderButtonPushDown 360 度编码器开关是否按下
boolean IsEncoderButtonPushDown(void)
{
if (!digitalRead(encoderButtonPin))
{
delay(5);
if (!digitalRead(encoderButtonPin))
return true;
}
return false;
}
// Alert 蜂鸣和震动
void Alert(boolean buzz, boolean vibrator)
{
if (alertEnable)
{
analogWrite(buzzPinAnalog, buzz ? 256 : 0);
analogWrite(vibratorPinAnalog, vibrator ? 256 : 0);
}
}