#include int lightSwitch = 2; int buzzPin = 7; int pushButton = 8; int CLK1 = 3; int DIO1 = 4; int CLK2 = 5; int DIO2 = 6; int brightness = 2; //1-7 int pos[] = {0,1,2,3}; int displayName0; int displayNum; int startMenu = 1; //time long s; long s0; long s1; long s2; long ds0; long ds1; long ds2; long d1, d2, d3, d4; int onDelay = 750; int offDelay = 500; int loss = 0; int startSwitch; int startSelect; int pushButtonHit; int select = 0; long timeScale = 60L * 1000; // 1 minute long timeIteration = 6; long clockTimer[] = {1L * timeScale, 3L * timeScale, 5L * timeScale, 10L * timeScale, 15L * timeScale, 30L * timeScale, 60L * timeScale}; int paused = 0; TM1637 tm1(CLK1,DIO1); TM1637 tm2(CLK2,DIO2); void updateDisplay(int displayName0, long ms) { int minutes = (ms / 60000) % 100; // Up to 99 minutes int seconds = (ms / 1000) % 60; int deciseconds = (ms / 100) % 10; // Tenths of a second if (displayName0 == 1) { if (ms >= timeScale) { // Show MM:SS if (minutes / 10 >= 1) { tm1.display(pos[0], minutes / 10); } else { tm1.display(pos[0], 0x7f); } tm1.display(pos[1], minutes % 10); tm1.display(pos[2], seconds / 10); tm1.display(pos[3], seconds % 10); } else { // Show SS.DD if (seconds / 10 >= 1) { tm1.display(pos[0], seconds / 10); } else { tm1.display(pos[0], 0x7f); } tm1.display(pos[1], seconds % 10); tm1.display(pos[2], deciseconds); tm1.display(pos[3], 0x7f); } } else if (displayName0 == 2) { if (ms >= timeScale) { if (minutes / 10 >= 1) { tm2.display(pos[0], minutes / 10); } else { tm2.display(pos[0], 0x7f); } tm2.display(pos[1], minutes % 10); tm2.display(pos[2], seconds / 10); tm2.display(pos[3], seconds % 10); } else { if (seconds / 10 >= 1) { tm2.display(pos[0], seconds / 10); } else { tm2.display(pos[0], 0x7f); } tm2.display(pos[1], seconds % 10); tm2.display(pos[2], deciseconds); tm2.display(pos[3], 0x7f); } } } void pause() { bool visible = false; while (true) { // Blink ON: show time if (visible) { updateDisplay(1, ds1); updateDisplay(2, ds2); } // Blink OFF: clear displays else { for (int j = 0; j <=3; j++) { tm1.display(pos[j], 0x7f); tm2.display(pos[j], 0x7f); } } visible = !visible; // Check for button press to unpause for (int i = 0; i < 50; i++) { // 500ms total (10ms * 50) if (digitalRead(pushButton) == HIGH) { delay(10); // debounce while (digitalRead(pushButton) == HIGH); // wait for release delay(10); // Restore display before exiting pause updateDisplay(1, ds1); updateDisplay(2, ds2); return; } delay(10); } } } void gameOver(int player) { while(digitalRead(pushButton) == 0) { if (player == 1) { for (int i = 0; i < 4; i++) { tm1.display(pos[i],0); } if (loss < 3) { digitalWrite(buzzPin,HIGH); } delay(500); for (int i = 0; i < 4; i++) { tm1.display(pos[i],0x7f); } digitalWrite(buzzPin,LOW); delay(500); } else { for (int i = 0; i < 4; i++) { tm2.display(pos[i],0); } if (loss < 3) { digitalWrite(buzzPin,HIGH); } delay(500); for (int i = 0; i < 4; i++) { tm2.display(pos[i],0x7f); } digitalWrite(buzzPin,LOW); delay(500); } if (loss < 10) { loss++; } } startMenu = 1; loss = 0; select --; setup2(); } void setup() { ////////////////////////////////////// === SETUP === /////////////////////////////////////////////////////////////// // put your setup code here, to run once: tm1.init(); tm1.set(brightness); tm1.point(POINT_ON); //makes 1500 15:00 tm2.init(); tm2.set(brightness); tm2.point(POINT_ON); //makes 1500 15:00 pinMode(lightSwitch,INPUT); pinMode(buzzPin,OUTPUT); pinMode(pushButton,INPUT); setup2(); Serial.begin(9600); } void setup2() { startSwitch = digitalRead(lightSwitch); s = clockTimer[select]; s1 = s; s2 = s; ds1 = s; ds2 = s; updateDisplay(1,s1); updateDisplay(2,s2); } void loop() { ////////////////////////////////////// === LOOP === /////////////////////////////////////////////////////////////// //============================================================================ Start Menu while (startMenu == 1) { //start menu loop if (digitalRead(pushButton) == 1) { pushButtonHit = 1; } if (digitalRead(pushButton) == 0 && pushButtonHit == 1) { if (select < sizeof(clockTimer)/sizeof(clockTimer[0]) - 1) { select++; } else { select = 0; } pushButtonHit = 0; s = clockTimer[select]; s1 = s; s2 = s; ds1 = s; ds2 = s; updateDisplay(1,s1); updateDisplay(2,s2); startSelect = 0; } else { startSelect = 1; } if (digitalRead(lightSwitch) != startSwitch) { //break the start menu loop startMenu = 0; } delay(50); } //============================================================================ Pause if (digitalRead(pushButton) == 1) { delay(10); // debounce while (digitalRead(pushButton) == 1); // wait for release delay(10); pause(); // call pause feature } //============================================================================ Time Switch if (digitalRead(lightSwitch) == 1) { //PLAYER 1 updateDisplay(1,s1); if (s1 <= 0) { // LOSS CONDITION gameOver(1); } s1 -= timeIteration; ds1 = s1; } else if (digitalRead(lightSwitch) == 0) { // PLAYER 2 updateDisplay(2,s2); if (s2 <= 0) { // LOSS CONDITION gameOver(2); } s2 -= timeIteration; ds2 = s2; } }