/*
copyied last code i asked cgpd to check
flashes red or green led on pcb when counting
yellow changed to 10 seconds
speed 1000 normal speed
buzz long = 200ms
buzz short = 100ms
stop and start debounce
still has false stop
need to check buzzer times
with enable and disable interupt delay inc from 100 to 150
speed = 950 speed slightly faster
*/
// Pin connections
const int startButtonPin = 2;
const int stopButtonPin = 3;
const int fightTimeButtonPin = 4; //green leds
const int restTimeButtonPin = 5; // red leds
const int fightLedMinutesPins[] = {6, 7, 8, 9, 10, 11};
const int restLedMinutesPins[] = {12, 13, A0};
const int displayPinRed = A1;
const int displayPinGreen = A2;
const int displayPinYellow = A3;
const int buzzerPin = A4;
const int yellowTime = 10; // time in seconds when yellow is on
const int longBuzz = 500;
const int shortBuzz = 250;
const int seconds = 60 ;
const int enableIntDelay = 250 ;
const int disenableIntDelay = 250 ;
// Variables
int fightTimeMinutes = 3; // Default fight time
int restTimeMinutes = 1; // Default rest time
int fightTimeSeconds;
int restTimeSeconds;
int restyellowTimeSeconds ; // need both as they are dec in main timing loop
int fightyellowTimeSeconds; // need both as they are dec in main timing loop
int startButtonState = HIGH;
int stopButtonState = HIGH;
int fightTimeButtonState = HIGH;
int restTimeButtonState = HIGH;
int readingFightMinutes;
int readingRestMinutes;
int flashingLedFightMinutes = 0; // assign which led flashes
int flashingLedRestMinutes = 0;
int delayTime = 940; // should be 1000 , -- to speed up timer
int secondsCounter= 0;
int fightSecondsCounter =0;
int restSecondsCounter = 0;
int lightUp = 0;
int whichRestLedToLightup = 0;
int whichFightLedToLightup = 0;
int restSecondsCounterAdjust = 59;
int fightSecondsCounterAdjust = 59;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelayInc = 50; // Adjust as needed
unsigned long debounceDelayStop = 450; // for stop button
unsigned long debounceDelayStart = 100; //for start button
bool buttonStateRest= HIGH;
bool buttonStateFight= HIGH;
bool lastRestButtonState = HIGH;
bool lastFightButtonState = HIGH;
volatile bool timerRunning = false;
volatile bool fightInc = false;
volatile bool restInc = false;
volatile bool blinkFightTimeLedState = HIGH;
volatile bool blinkRestTimeLedState = HIGH;
volatile bool startButtonPressed = false;
volatile bool stopButtonPressed = false;
// end of edc dddddddddddddddddddddddddddddddddddddddd
void setup() { // void setup start
// Initialize digital pins
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(fightTimeButtonPin, INPUT_PULLUP);
pinMode(restTimeButtonPin, INPUT_PULLUP);
for (int i = 0; i < 6; i++) {
pinMode(fightLedMinutesPins[i], OUTPUT); // sets all green leds to output mode
}
for (int i = 0; i < 3; i++) {
pinMode(restLedMinutesPins[i], OUTPUT); // sets all red leds to output mode
}
pinMode(displayPinRed, OUTPUT); // drive transistors to drive relays
pinMode(displayPinGreen, OUTPUT);
pinMode(displayPinYellow, OUTPUT);
pinMode(buzzerPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(stopButtonPin), handleStopButton, FALLING);
attachInterrupt(digitalPinToInterrupt(startButtonPin), handleStartButton, FALLING);
updateLedsandreadButtons();
resetTimes ();
} // end of void setup
// -------------------------------------------------
void loop() { //start void loop
readAndHandleFightButton(); //looks at inc fight minutes
readAndHandleRestButton(); //looks at inc rest minutes
if (timerRunning == true) { // set true if start button pressed
mainTimer (); // skips this if start button has not been pressed
}
} // end of void loop
// ====================================================
// void functiions ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
void resetTimes () {
restyellowTimeSeconds = yellowTime ;
fightyellowTimeSeconds = yellowTime;
restTimeSeconds = restTimeMinutes * seconds;
fightTimeSeconds = fightTimeMinutes * seconds ;
fightSecondsCounter = secondsCounter;
restSecondsCounter = secondsCounter;
whichRestLedToLightup = lightUp;
whichFightLedToLightup =lightUp;
}
// ........................................................
void mainTimer() {
if (timerRunning) {
resetTimes ();
}
setDisplayFight(); // green on interupt enabled
while (timerRunning && (fightTimeSeconds - fightyellowTimeSeconds > 0)) {
blinkFightLed(); // call to flash green fight minutes led
fightTimeSeconds -- ; // dec fightyellowTimeSeconds
}
if (timerRunning ) {
// disable interupt
buzzerShort();
setDisplayFightAndYellow(); // green on yellow on red off
// interupt enabled
}
while (timerRunning && fightyellowTimeSeconds > 0) {
blinkFightLed();
fightyellowTimeSeconds --; // decrement fightyellowTimeSeconds
}
if (timerRunning) {
// interupt disabled
buzzerLong();
setDisplayRest(); // red on
// interupt enabled
}
while (timerRunning && (restTimeSeconds - restyellowTimeSeconds > 0)) {
blinkRestLed (); // red on green off yellow off
restTimeSeconds --; //decrement rest time seconds
}
if (timerRunning) {
buzzerShortEnDisable();
}
while (timerRunning && restyellowTimeSeconds > 0) {
setDisplayRestAndYellow(); // display
blinkRestLed(); // number of rest minutes
restyellowTimeSeconds --; //dec rest time seconds
}
if (timerRunning) {
buzzerLongEnDisable();
}
} // end main count
// ............................................................
void updateLedsandreadButtons(){
updateLEDs(); // not sure if below is needed
//startButtonState = digitalRead(startButtonPin);
//stopButtonState = digitalRead(stopButtonPin);
//fightTimeButtonState = digitalRead(fightTimeButtonPin);
//restTimeButtonState = digitalRead(restTimeButtonPin);
}
void updateLEDs() { // leds on pcb
for (int i = 0; i < 6; i++) { // set all green leds low
digitalWrite(fightLedMinutesPins[i], LOW);
}
for (int i = 0; i < 3; i++) { // set all red leds low
digitalWrite(restLedMinutesPins[i], LOW);
}
for (int i = 0; i < fightTimeMinutes; i++) { // sets high acording to minutes sellected
digitalWrite(fightLedMinutesPins[i], HIGH);
}
for (int i = 0; i < restTimeMinutes; i++) {
digitalWrite(restLedMinutesPins[i], HIGH);
}
} // end of update leds
void buzzerLong() {
disableStopInterrupt();
digitalWrite(buzzerPin, HIGH);
delay(longBuzz);
digitalWrite(buzzerPin, LOW);
enableStopInterrupt();
}
void buzzerLongEnDisable(){
disableStopInterrupt();
digitalWrite(buzzerPin, HIGH);
delay(longBuzz);
digitalWrite(buzzerPin, LOW);
enableStopInterrupt();
}
void buzzerShort() {
disableStopInterrupt();
digitalWrite(buzzerPin, HIGH);
delay(shortBuzz);
digitalWrite(buzzerPin, LOW);
enableStopInterrupt();
}
void buzzerShortEnDisable() {
disableStopInterrupt();
digitalWrite(buzzerPin, HIGH);
delay(shortBuzz);
digitalWrite(buzzerPin, LOW);
enableStopInterrupt();
}
void blinkRestLed() {
digitalWrite(restLedMinutesPins[whichRestLedToLightup], blinkRestTimeLedState );
delay(delayTime); // delay 1 sec
restSecondsCounter ++;
//Serial.println(restSecondsCounter);
if (restSecondsCounter > restSecondsCounterAdjust ) {
restSecondsCounter = 0;
digitalWrite(restLedMinutesPins[whichRestLedToLightup],HIGH);
digitalWrite( restLedMinutesPins[whichRestLedToLightup],HIGH);
whichRestLedToLightup ++;
}
if (whichRestLedToLightup > restTimeMinutes -1) {
whichRestLedToLightup = 0;
}
//Serial.println(whichRestLedToLightup);
blinkRestTimeLedState = !blinkRestTimeLedState;
digitalWrite(restLedMinutesPins[whichRestLedToLightup], blinkRestTimeLedState);
}
void blinkFightLed () {
digitalWrite(fightLedMinutesPins[whichFightLedToLightup], blinkFightTimeLedState );
delay(delayTime); // delay 1 sec
fightSecondsCounter++;
//Serial.println(fightSecondsCounter);
if (fightSecondsCounter > fightSecondsCounterAdjust) {
fightSecondsCounter = 0;
digitalWrite(fightLedMinutesPins[whichFightLedToLightup], HIGH);
whichFightLedToLightup ++;
}
if (whichFightLedToLightup > fightTimeMinutes - 1) {
whichFightLedToLightup = 0;
}
//Serial.println(whichFightLedToLightup);
blinkFightTimeLedState = !blinkFightTimeLedState ;
digitalWrite(fightLedMinutesPins[whichFightLedToLightup], blinkFightTimeLedState );
}
void disableStopInterrupt() {
detachInterrupt(digitalPinToInterrupt(stopButtonPin)); // Disable interrupt
delay(disenableIntDelay);
}
void enableStopInterrupt() {
delay(enableIntDelay);
attachInterrupt(digitalPinToInterrupt(stopButtonPin), handleStopButton, FALLING); // Reattach interrupt
}
void setDisplayFight() { // only green
disableStopInterrupt();
digitalWrite(displayPinRed, LOW);
digitalWrite(displayPinYellow, LOW);
digitalWrite(displayPinGreen, HIGH);
enableStopInterrupt();
}
void setDisplayFightAndYellow() { //green and yellow
disableStopInterrupt();
digitalWrite(displayPinRed, LOW);
digitalWrite(displayPinGreen, HIGH);
digitalWrite(displayPinYellow, HIGH);
enableStopInterrupt();
}
void setDisplayRest() { // only red
disableStopInterrupt();
digitalWrite(displayPinGreen, LOW);
digitalWrite(displayPinYellow, LOW);
digitalWrite(displayPinRed, HIGH);
enableStopInterrupt();
}
void setDisplayRestAndYellow() { // red and yellow
disableStopInterrupt();
digitalWrite(displayPinGreen, LOW);
digitalWrite(displayPinRed, HIGH);
digitalWrite(displayPinYellow, HIGH);
enableStopInterrupt();
}
void setDisplayAllOff() {
disableStopInterrupt();
digitalWrite(displayPinRed, LOW); // Turn off red display
digitalWrite(displayPinGreen, LOW); // Turn off green display
digitalWrite(displayPinYellow, LOW); // Turn off yellow display
enableStopInterrupt();
}
void handleStopButton() {
if (millis() - lastDebounceTime > debounceDelayStop) {
stopButtonPressed = true;
lastDebounceTime = millis();
timerRunning = false;
setDisplayAllOff();
resetTimes;
stopButtonPressed = false; // Reset the flag
void updateLedsandreadButtons();
}
}
void handleStartButton() {
if (millis() - lastDebounceTime > debounceDelayStart) {
startButtonPressed = true;
lastDebounceTime = millis();
timerRunning = true;
startButtonPressed = false; // Reset the flag
}
}
void readAndHandleFightButton() {
// start of reading fight button
readingFightMinutes = digitalRead(fightTimeButtonPin);
if (readingFightMinutes != lastFightButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelayInc) {
if (readingFightMinutes != buttonStateFight) {
buttonStateFight = readingFightMinutes;
if (buttonStateFight == LOW) {
fightTimeMinutes ++;
if (fightTimeMinutes > 6) {
fightTimeMinutes = 1; }
updateLedsandreadButtons();
}
}
}
lastFightButtonState = readingFightMinutes;
} // check if needed
void readAndHandleRestButton(){
// start of read rest button
readingRestMinutes = digitalRead(restTimeButtonPin);
if (readingRestMinutes != lastRestButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelayInc) {
if (readingRestMinutes != buttonStateRest) {
buttonStateRest = readingRestMinutes;
if (buttonStateRest == LOW) {
restTimeMinutes ++;
if (restTimeMinutes > 3) {
restTimeMinutes = 1; }
updateLedsandreadButtons();
}
}
}
lastRestButtonState = readingRestMinutes;
}