// TM1637 SevenSegment Counter Wokwi Example
//
// https://wokwi.com/projects/339227323398095442
#include <TM1637.h>
const int buttonNumAdj = 6;
int buttonState = 0;
int beforeState = 0;
const int CLK1 = 2; //第一个1637
const int DIO1 = 3;
const int CLK2 = 4; //第二个1637
const int DIO2 = 5;
TM1637 tm1(CLK1, DIO1); //实例化第一个1637
TM1637 tm2(CLK2, DIO2); //实例化第二个1637
void setup() {
tm1.init(); //初始化第一个1637
tm1.set(BRIGHT_TYPICAL);
tm2.init(); //初始化第二个1637
tm2.set(BRIGHT_TYPICAL);
Serial.begin(9600); //测试用
}
unsigned int incr = 0; //次数计数器
unsigned int counter = 0; //时间计数器
unsigned int total = 100; //总共倒计时的时长
void loop() {
if (total >= 1200) { //调整计数范围,大于120秒就返回20秒,意思就是20-120秒内可调
total = 100; //也可以改为0,就是从10-120秒可调。
}
buttonState = digitalRead(buttonNumAdj);
if (buttonState == HIGH) { //调整倒计时总时长
total = total + 100; //总数+100,就是加10秒
tm1.display(1, (total / 1000) % 10); //显示调整后第2位数字
tm1.display(2, (total / 100) % 10); //显示调整后第3位数字
tm1.display(3, (total / 10) % 10); //显示调整后第4位数字
incr = 0; //计数器清0
counter = 0; //时间计数器清0
Serial.println(total); //测试用
digitalWrite(13, HIGH); //测试用
delay(500);
}
else{
digitalWrite(13, LOW); //测试用
}
//tm.display(0, (counter / 1000) % 10);
// tm1.display(0, incr);
tm1.display(1, (counter / 1000) % 10); //显示第2位数字
tm1.display(2, (counter / 100) % 10); //显示第3位数字
tm1.display(3, (counter / 10) % 10); //显示第4位数字
//tm.display(3, counter % 10);
tm2.display(2, (incr /10) % 10); //显示第2屏的十位
tm2.display(3, incr % 10); //显示第2屏的个位
counter++; //时间自增
Serial.println(counter); //测试用
if (counter > total) { //次数计数器自增,如果计时到达最大值,则计时归0,次数(incr)加1
counter = 0;
incr ++;
}
if (buttonState != beforeState){ //按键去抖
delay(200);
}
delay(100);
}