Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Zhuoyu Zhang
MDSY
Commits
558c583a
Commit
558c583a
authored
Apr 06, 2023
by
Zhuoyu Zhang
Browse files
4.6
parent
4cafaf0f
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main.cpp
View file @
558c583a
...
...
@@ -3,21 +3,21 @@
#include
<Encoder.h>
// 数码管value:0~9
byte
smg
[
10
]
=
{
0xc0
,
0xf9
,
0x
5b
,
0x
4f
,
0x
66
,
0x
6d
,
0x
7d
,
0x
07
,
0x
7f
,
0x
6f
};
byte
smg
[
10
]
=
{
0xc0
,
0xf9
,
0x
a4
,
0x
b0
,
0x
99
,
0x
92
,
0x
82
,
0x
f8
,
0x
80
,
0x
90
};
// 数码管位选引脚
int
com
[
4
]
=
{
6
,
5
,
4
,
3
};
// 旋转编码器引脚
#define CLK 3
0
#define SW
31
#define DT
32
#define CLK
PIN_A
3
#define SW
PIN_A4
#define DT
PIN_A5
// 蜂鸣器引脚
#define BEEP PIN_A2
// 是否停止计时
boolean
isStop
=
tru
e
;
boolean
isStop
=
fals
e
;
// 旋转编码器类
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
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment