The code you see is a very simple Arduino timer that can be used by two bodybuilders, such as a couple working out in their home gym. The timer is used to time the rest period between sets of resistance training exercises.
Here's how the code works for this specific use case:
First, the pins for the buttons and the buzzer are set with the following lines of code:
const int athlete1ButtonPin = 2;
const int athlete2ButtonPin = 3;
const int buzzerPin = 9;
pinMode(athlete1ButtonPin, INPUT_PULLUP);
The two buttons are connected to pins 2 and 3 and are configured to use the internal pull-up resistors. This means that when the buttons are not pressed, the input pins will read HIGH, and when the buttons are pressed, they will read LOW.
The buzzer is connected to pin 9 and is configured as an output.
The next step is to set the rest time in milliseconds:
const unsigned long restTime = 120000;
In this case, the rest time is set to 2 minutes (120 seconds) which is the recommended rest period for most resistance training exercises.
The start time for each athlete is also set to 0:
unsigned long athlete1StartTime = 0;
unsigned long athlete2StartTime = 0;
This is because the timer starts when the athlete presses their respective button.
In the loop() function, the code checks if either of the two buttons is pressed. If athlete 1 presses their button, the timer for athlete 1 starts counting:
if (digitalRead(athlete1ButtonPin) == LOW)
{ athlete1StartTime = millis();}
The same thing happens for athlete 2 if they press their button.
After the timer has started, the code checks if the two minutes rest period has passed for each athlete:
if (athlete1StartTime > 0 && millis() - athlete1StartTime >= restTime) { tone(buzzerPin, 440, 2000); // set long tone for athlete 1 delay(500);
noTone(buzzerPin);
athlete1StartTime = 0;
} if (athlete2StartTime > 0 && millis() - athlete2StartTime >= restTime) { tone(buzzerPin, 880, 250); // set double tone for athlete 2 delay(250);
tone(buzzerPin, 880, 250);
delay(500);
noTone(buzzerPin);
athlete2StartTime = 0;
If the rest period has passed, the buzzer sounds a specific tone depending on which athlete's rest period has ended. For athlete 1, the buzzer sounds a long tone for 2 seconds, and for athlete 2, it sounds a double beep.
The benefit of this timer is that it provides an accurate way to time rest periods between resistance training sets. It is also a convenient and reliable alternative to using a phone app timer, which could be damaged in the gym.
// set the pins for the buttons and the buzzer
const int athlete1ButtonPin = 2;
const int athlete2ButtonPin = 3;
const int buzzerPin = 9;
// set the rest time in milliseconds
const unsigned long restTime = 120000;
// set the start time for each athlete
unsigned long athlete1StartTime = 0;
unsigned long athlete2StartTime = 0;
void setup() {
// enable internal pull-up resistors for the button pins
pinMode(athlete1ButtonPin, INPUT_PULLUP);
pinMode(athlete2ButtonPin, INPUT_PULLUP);
// set the buzzer pin as an output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// check if athlete 1 button is pressed
if (digitalRead(athlete1ButtonPin) == LOW) {
athlete1StartTime = millis();
}
// check if athlete 2 button is pressed
if (digitalRead(athlete2ButtonPin) == LOW) {
athlete2StartTime = millis();
}
// check if 2 minutes have passed for athlete 1
if (athlete1StartTime > 0 && millis() - athlete1StartTime >= restTime) {
digitalWrite(buzzerPin, HIGH); // turn on buzzer for athlete 1
delay(2000); // wait for 2 seconds
digitalWrite(buzzerPin, LOW); // turn off buzzer
athlete1StartTime = 0;
}
// check if 2 minutes have passed for athlete 2
if (athlete2StartTime > 0 && millis() - athlete2StartTime >= restTime) {
digitalWrite(buzzerPin, HIGH); // turn on buzzer for athlete 2
delay(100); // wait for 100 milliseconds
digitalWrite(buzzerPin, LOW); // turn off buzzer
delay(100); // wait for another 100 milliseconds
digitalWrite(buzzerPin, HIGH); // turn on buzzer again
delay(100); // wait for 100 milliseconds
digitalWrite(buzzerPin, LOW); // turn off buzzer
athlete2StartTime = 0;
}
}
In the embedded video below, the timer was set for 30 seconds just for the video purposes.