Commit 558c583a authored by Zhuoyu Zhang's avatar Zhuoyu Zhang
Browse files

4.6

parent 4cafaf0f
......@@ -3,21 +3,21 @@
#include <Encoder.h>
// 数码管value:0~9
byte smg[10] = {0xc0, 0xf9, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
byte smg[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
// 数码管位选引脚
int com[4] = {6, 5, 4, 3};
// 旋转编码器引脚
#define CLK 30
#define SW 31
#define DT 32
#define CLK PIN_A3
#define SW PIN_A4
#define DT PIN_A5
// 蜂鸣器引脚
#define BEEP PIN_A2
// 是否停止计时
boolean isStop = true;
boolean isStop = false;
// 旋转编码器类
Encoder encoder(CLK, DT);
......@@ -32,10 +32,10 @@ byte selectedTimeIndex = 0;
int numTimes = 19;
// minute值
int TimerMinute = 0;
int TimerMinute = 19;
// second值
int TimerSecond = 0;
int TimerSecond = 59;
// 旋转编码器值
int encoderVal = 0;
......@@ -64,10 +64,30 @@ void displaySMG(int COM, int value)
Write8Pins(pin, smg[value]);
}
// 读取旋转编码器的旋转
int getEncoderTurn()
{ // return -1,0,or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(CLK);
int newB = digitalRead(DT);
if (newA != oldA || newB != oldB)
{ // something has changed
if (oldA == LOW && newA == HIGH)
{
result = -(oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
// 改变预设时间
void changeSetTime(int value)
{
selectedTimeIndex = value;
selectedTimeIndex += value;
if (selectedTimeIndex < 0)
{
selectedTimeIndex = numTimes;
......@@ -89,6 +109,7 @@ void updateTime()
if (curMillis > (lastMillis + 1000) && (TimerMinute > 0 || TimerSecond > 0))
{
// 实现滴答声
// Serial.println("666");
digitalWrite(BEEP, HIGH);
delay(10);
digitalWrite(BEEP, LOW);
......@@ -109,6 +130,7 @@ void updateTime()
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(SW, INPUT);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
......@@ -116,36 +138,44 @@ void setup()
// selectedTimeIndex = EEPROM.read(0);
// TimerMinute = times[selectedTimeIndex] / 100;
// TimerSecond = times[selectedTimeIndex] % 100;
displaySMG(0,1);
}
void loop()
{
// // put your main code here, to run repeatedly:
// // 读取旋转编码器的值
// encoderVal = encoder.read();
// // 旋转编码器按键按下
// if (!digitalRead(SW))
// {
// isStop = !isStop;
// digitalWrite(BEEP, LOW);
// // 等待按键抬起
// while (!digitalRead(SW))
// ;
// EEPROM.write(0, selectedTimeIndex);
// }
// // 计时暂停状态
// if (isStop)
// {
// changeSetTime(encoderVal);
// }
// else
// {
// updateTime();
// }
// displaySMG(0, TimerMinute / 10);
// displaySMG(1, TimerMinute % 10);
// displaySMG(2, TimerSecond / 10);
// displaySMG(3, TimerSecond % 10);
// put your main code here, to run repeatedly:
// 读取旋转编码器的值
encoderVal = getEncoderTurn();
// 旋转编码器按键按下
if (!digitalRead(SW))
{
isStop = !isStop;
digitalWrite(BEEP, LOW);
// 等待按键抬起
while (!digitalRead(SW))
;
// EEPROM.write(0, selectedTimeIndex);
}
// 计时暂停状态
if (isStop)
{
changeSetTime(encoderVal);
}
else
{
updateTime();
}
Serial.println(encoderVal);
// 计时结束蜂鸣器报警
if (TimerMinute == 0 && TimerSecond ==0)
{
digitalWrite(BEEP, HIGH);
}
displaySMG(0, TimerMinute / 10);
delay(3);
displaySMG(1, TimerMinute % 10);
delay(3);
displaySMG(2, TimerSecond / 10);
delay(3);
displaySMG(3, TimerSecond % 10);
delay(3);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment