8//##########################################################################################################################################################################################################################
//### Initialize
//##########################################################################################################################################################################################################################
//MODULE 1 - TIMER
//#############################################################################################################
#include <TM1637Display.h>
#include "pitches.h"
// Pins
#define mod1CLK_PIN 3
#define mod1DIO_PIN 2
TM1637Display mod1Display(mod1CLK_PIN, mod1DIO_PIN);
const int mod1LedPins[] = {4, 5}; // Pins for Strike LEDs
const int mod1ButtonPin = 53; // Pin for error button TESTING
#define mod1SpeakerPin 51
// Timer
const unsigned long mod1CountdownTime = 15; // 5 minutes in seconds
unsigned long mod1StartTime;
unsigned long mod1CurrentTime;
unsigned long mod1ElapsedTime;
//Strikes
int mod1StrikeCount = 0;
//##########################################################################################################################################################################################################################
//### SETUP
//##########################################################################################################################################################################################################################
void setup() {
Serial.begin(9600);
//MODULE 1 - TIMER
mod1Display.setBrightness(7); // Set the brightness of the display (0-7)
mod1Display.clear(); // Clear the display
mod1StartTime = millis(); // Record the starting time
for (int i = 0; i < 2; i++) {
pinMode(mod1LedPins[i], OUTPUT);
}
pinMode(mod1ButtonPin, INPUT_PULLUP);
}
//##########################################################################################################################################################################################################################
//### FUNCTIONS
//##########################################################################################################################################################################################################################
//#############################################################################################################
//### MODULE 1 - TIMER
//#############################################################################################################
void mod1UpdateElapsedTime() {
mod1CurrentTime = millis();
mod1ElapsedTime = (mod1CurrentTime - mod1StartTime) / 1000;
}
void mod1HandleCountdown() {
if (mod1ElapsedTime <= mod1CountdownTime) {
unsigned long mod1RemainingTime = mod1CountdownTime - mod1ElapsedTime;
mod1DisplayRemainingTime(mod1RemainingTime);
if (mod1RemainingTime == 0) {
mod1StartBlinking();
}
}
}
void mod1DisplayRemainingTime(unsigned long mod1RemainingTime) {
unsigned int minutes = mod1RemainingTime / 60;
unsigned int seconds = mod1RemainingTime % 60;
mod1Display.showNumberDecEx(minutes * 100 + seconds, 0b01000000, true);
}
void mod1StartBlinking() {
for (int i = 0; i < 10; i++) {
mod1Display.showNumberDecEx(0, 0b01000000, true);
delay(250);
mod1Display.clear();
delay(250);
tone(mod1SpeakerPin, NOTE_E4);
delay(10);
noTone(mod1SpeakerPin);
delay(10);
}
mod1ResetGame();
}
void mod1CheckButtonPress() {
if (digitalRead(mod1ButtonPin) == LOW) {
mod1StrikeCount++;
Serial.println("Strike added!");
mod1UpdateStrikes();
}
}
void mod1Beep(int times) {
for (int i = 0; i < times; i++) {
tone(mod1SpeakerPin, NOTE_E4);
delay(10);
noTone(mod1SpeakerPin);
delay(10);
}
}
void mod1UpdateStrikes() {
if (mod1StrikeCount >= 3) {
mod1Beep(5);
Serial.println("BOOOOM!");
mod1ResetGame();
} else if (mod1StrikeCount == 2) {
mod1Beep(2);
} else if (mod1StrikeCount == 1) {
mod1Beep(1);
}
for (int i = 0; i < 2; i++) {
digitalWrite(mod1LedPins[i], i < mod1StrikeCount ? HIGH : LOW);
}
}
void mod1ResetGame(){
delay(5000);
mod1StrikeCount = 0;
for (int i = 0; i < 2; i++) {
digitalWrite(mod1LedPins[i], LOW);
}
mod1StartTime = millis();
Serial.println("RESET");
}
//##########################################################################################################################################################################################################################
//### Loop
//##########################################################################################################################################################################################################################
void loop() {
//MODULE 1 - TIMER
//#############################################################################################################
mod1UpdateElapsedTime();
mod1HandleCountdown();
mod1CheckButtonPress();
}