#include <TM1637.h>
#define CLK1 2 // Pin zegara dla pierwszego wyświetlacza
#define DIO1 3 // Pin danych dla pierwszego wyświetlacza
#define CLK2 10 // Pin zegara dla drugiego wyświetlacza
#define DIO2 11 // Pin danych dla drugiego wyświetlacza
//ten po lewej
TM1637 tm1(CLK2, DIO2);
TM1637 tm2(CLK1, DIO1);
//ten po prawej
#define butRight 4
#define butLeft 5
#define butA 9
#define butB 8
#define butC 7
#define butD 6
unsigned long previousMillis = 0;
const long interval = 1000; // Odświeżanie co 1 sekundę
int clockA;
int clockB;
int increment;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(butRight, INPUT_PULLUP);
pinMode(butLeft, INPUT_PULLUP);
pinMode(butA, INPUT_PULLUP);
pinMode(butB, INPUT_PULLUP);
pinMode(butC, INPUT_PULLUP);
pinMode(butD, INPUT_PULLUP);
pinMode(CLK1, OUTPUT);
pinMode(DIO1, OUTPUT);
pinMode(CLK2, OUTPUT);
pinMode(DIO2, OUTPUT);
tm1.init();
tm2.init();
tm1.set(BRIGHT_TYPICAL); //ustawienie jasnosci
tm2.set(BRIGHT_TYPICAL);
}
void loop() {
int channel = 1;
//wybor kanalu
for(;;){
if(digitalRead(butB) == LOW){
delay(200);
channel++;
if(channel > 5){
channel = 1;
}
}
if(digitalRead(butA) == LOW){
delay(200);
channel--;
if(channel < 1){
channel = 5;
}
}
tm2.display(3, channel);
if(digitalRead(butD) == LOW){
delay(150);
break;
}
}
// ustwienie czasu
tm2.display(3, 0x40);
clockA = startingTime(channel);
clockB = clockA;
increment = clockSet(channel);
updateDisplay(clockA, tm1);
updateDisplay(clockB, tm2);
//rozpoczecie gry
for(;;){
if(digitalRead(butC) == LOW){
delay(200);
break;
}
}
//gra
for(;;){
for(;;){
if(clockA == 0){
break;
}
if(digitalRead(butC) == LOW){
delay(150);
pause();
}
updateDisplay(clockA, tm1);
clockA = updateTime(clockA);
if(digitalRead(butLeft) == LOW){
delay(130);
clockA += increment;
updateDisplay(clockA, tm1);
break;
}
}
if(clockA == 0 || clockB == 0){
break;
}
for(;;){
if(clockB == 0){
break;
}
if(digitalRead(butC) == LOW){
delay(150);
pause();
}
updateDisplay(clockB, tm2);
clockB = updateTime(clockB);
if(digitalRead(butRight) == LOW){
delay(130);
clockB += increment;
updateDisplay(clockB, tm2);
break;
}
}
if(clockA == 0 || clockB == 0){
break;
}
}
//koniec gry
delay(4000);
for(;;){
if(digitalRead(butD) == LOW){
delay(200);
tm1.display(0, 0x40);
tm1.display(1, 0x40);
tm1.display(2, 0x40);
tm1.display(3, 0x40);
tm2.display(0, 0x40);
tm2.display(1, 0x40);
tm2.display(2, 0x40);
tm2.display(3, 0x40);
break;
}
}
}
int clockSet(int channel){
switch(channel){
case 1:
tm1.display(1, 5);
tm1.display(2, 0);
tm1.display(3, 0);
tm2.display(1, 5);
tm2.display(2, 0);
tm2.display(3, 0);
return 0;
case 2:
tm1.display(0, 1);
tm1.display(1, 0);
tm1.display(2, 0);
tm1.display(3, 0);
tm2.display(0, 1);
tm2.display(1, 0);
tm2.display(2, 0);
tm2.display(3, 0);
return 0;
case 3:
tm1.display(1, 3);
tm1.display(2, 0);
tm1.display(3, 2);
tm2.display(1, 3);
tm2.display(2, 0);
tm2.display(3, 2);
return 20;
case 4:
tm1.display(1, 0);
tm1.display(2, 3);
tm1.display(3, 0);
tm2.display(1, 0);
tm2.display(2, 3);
tm2.display(3, 0);
}
return 0;
}
//ustawienie czasu gry
int startingTime(int channel){
switch(channel){
case 1:
return 3000;
case 2:
return 6000;
case 3:
return 1820;
case 4:
return 300;
case 5:
return manualSetting(clockA, tm1);
}
return 0;
}
//aktualizacja zegara
int updateTime(int clock){
delay(95);
return clock - 1;
}
//odswiezanie wyswietlaczy
void updateDisplay(int clock, TM1637 tm){
clock = clock / 6;
int digit = clock / 1000;
clock -= (1000 * digit);
tm.display(0, digit);
digit = clock / 100;
clock -= (100 * digit);
tm.display(1, digit);
clock *= 60;
digit = clock / 1000;
clock -= (1000 * digit);
tm.display(2, digit);
digit = clock / 100;
if(digit != 9 && clock/50 > digit *2) digit++;
tm.display(3, digit);
}
//obsluga pauzy
void pause(){
for(;;){
if(digitalRead(butC) == LOW){
delay(100);
break;
}
}
clockA = manualSetting(clockA, tm1);
clockB = manualSetting(clockB, tm2);
}
//manual settings
int manualSetting(int clock, TM1637 tm){
int a, b, c, d;
clock = clock / 6;
int digit = clock / 1000;
clock -= (1000 * digit);
for(;;){
tm.display(0, 0x40);
delay(150);
tm.display(0, digit);
delay(150);
tm.display(0, digit);
if(digitalRead(butA) == LOW){
delay(100);
digit--;
if(digit < 0){
digit = 5;
}
}
if(digitalRead(butB) == LOW){
delay(100);
digit++;
if(digit > 5){
digit = 0;
}
}
if(digitalRead(butD) == LOW){
delay(100);
break;
}
}
a = digit;
digit = clock / 100;
clock -= (100 * digit);
for(;;){
tm.display(1, 0x40);
delay(150);
tm.display(1, digit);
delay(150);
tm.display(1, digit);
if(digitalRead(butA) == LOW){
delay(100);
digit--;
if(digit < 0){
digit = 9;
}
}
if(digitalRead(butB) == LOW){
delay(100);
digit++;
if(digit > 9){
digit = 0;
}
}
if(digitalRead(butD) == LOW){
delay(100);
break;
}
}
b = digit;
clock *= 60;
digit = clock / 1000;
clock -= (1000 * digit);
for(;;){
tm.display(2, 0x40);
delay(150);
tm.display(2, digit);
delay(150);
tm.display(2, digit);
if(digitalRead(butA) == LOW){
delay(100);
digit--;
if(digit < 0){
digit = 5;
}
}
if(digitalRead(butB) == LOW){
delay(100);
digit++;
if(digit > 5){
digit = 0;
}
}
if(digitalRead(butD) == LOW){
delay(100);
break;
}
}
c = digit;
digit = clock / 100;
for(;;){
tm.display(3, 0x40);
delay(150);
tm.display(3, digit);
delay(150);
tm.display(3, digit);
if(digitalRead(butA) == LOW){
delay(100);
digit--;
if(digit < 0){
digit = 9;
}
}
if(digitalRead(butB) == LOW){
delay(100);
digit++;
if(digit > 9){
digit = 0;
}
}
if(digitalRead(butD) == LOW){
delay(100);
break;
}
}
d = digit;
return (a*1000 + b*100)*6 + c *100 + d * 10;
}