#include <TM1637.h>
int CLK = 2;
int DIO = 3;
int counter = 0;
int btn = 4;
int redMR = 11;
int yalowMR = 12;
int greenMR = 13;
int redSR = 7;
int yalowSR = 6;
int greenSR = 5;
int flag=0;
TM1637 tm(CLK, DIO);
void setup() {
// put your setup code here, to run once:
tm.init();
tm.set(BRIGHT_TYPICAL);
//tm.color("red");
pinMode(redMR, OUTPUT);
pinMode(yalowMR, OUTPUT);
pinMode(greenMR, OUTPUT);
pinMode(redSR, OUTPUT);
pinMode(yalowSR, OUTPUT);
pinMode(greenSR, OUTPUT);
//------------------------------
digitalWrite(redMR, LOW);
digitalWrite(yalowMR, LOW);
digitalWrite(greenMR, HIGH);
digitalWrite(redSR, HIGH);
digitalWrite(yalowSR, LOW);
digitalWrite(greenSR, LOW);
pinMode(btn, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bool checkBTN=digitalRead(btn);
if(checkBTN){
btnIsOn();
}
else{
btnIsOff();
}
// Serial.println(counter);
//Serial.println(flag);
delay(100);
}
void btnIsOff(){
digitalWrite(redMR, LOW);
digitalWrite(yalowMR, LOW);
digitalWrite(greenMR, HIGH);
digitalWrite(redSR, HIGH);
digitalWrite(yalowSR, LOW);
digitalWrite(greenSR, LOW);
counter=0;
tm.display(1, 0);
tm.display(2, 0);
}
void btnIsOn(){
tm.display(1, (counter / 100) % 10);
tm.display(2, (counter / 10) % 10);
counter++;
if(flag==3){
flag=0;
}
if (counter >= 0 && counter <= 50 && flag==0) {
stateOne();
}
if (counter >= 0 && counter <= 100 && flag==1) {
stateTwo();
}
if (counter >= 0 && counter <= 50 && flag==2) {
stateThreeO();
}
}
void stateOne(){
digitalWrite(redMR, LOW);
digitalWrite(yalowMR, HIGH);
digitalWrite(greenMR, LOW);
digitalWrite(redSR, HIGH);
digitalWrite(yalowSR, LOW);
digitalWrite(greenSR, LOW);
if(counter==50){
flag++;
counter = 0;
}
}
void stateTwo(){
digitalWrite(redMR, HIGH);
digitalWrite(yalowMR, LOW);
digitalWrite(greenMR, LOW);
digitalWrite(redSR, LOW);
digitalWrite(yalowSR, LOW);
digitalWrite(greenSR, HIGH);
if(counter==100){
flag++;
counter = 0;
}
}
void stateThreeO(){
digitalWrite(redMR, HIGH);
digitalWrite(yalowMR, LOW);
digitalWrite(greenMR, LOW);
digitalWrite(redSR, LOW);
digitalWrite(yalowSR, HIGH);
digitalWrite(greenSR, LOW);
if(counter==50){
flag++;
counter = 0;
}
}