Skip to content

Instantly share code, notes, and snippets.

@cedriczirtacic
Created July 25, 2015 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedriczirtacic/54e6c777f123401c355d to your computer and use it in GitHub Desktop.
Save cedriczirtacic/54e6c777f123401c355d to your computer and use it in GitHub Desktop.
// Simple boxing timer
// turn on debug strings
#define DEBUG true
// leds
int pingreen = 13;
int pinred = 11;
// buzzer
int pinbuzz = 12;
int rounds = 3;
int rests = 0;
int round_t = 1; //minutes
int rest_t = 1; //minutes
enum {
IN_ROUND = 0,
START_ROUND,
OFF
};
int timer_status = OFF;
void setup(){
Serial.begin(9600);
#ifdef DEBUG
Serial.println("Timer starts.");
#endif
pinMode(pingreen, OUTPUT);
pinMode(pinred, OUTPUT);
timer_status = START_ROUND;
}
void loop(){
unsigned long int time_total_min = round( (millis()/1000)/60 );
if(rests > 0) time_total_min += rest_t * rests;
// turn off the timer
if(rounds == 0 && timer_status < OFF){
off();
}else if(rounds > 0){
// timer is ON!
// if we still have more rounds then start another one
if(rounds > 0 && timer_status == START_ROUND ){
start_round(rounds);
}
// stop the round and rest
if( timer_status == IN_ROUND && time_total_min == round_t
|| time_total_min >= ( (round_t + rest_t) * (rests+1) ) ){
stop_round(rounds);
}
}
}
void start_round(int r){
int beep_times = 2;
#ifdef DEBUG
Serial.print("Starts round: ");
Serial.println(r);
#endif
digitalWrite(pingreen, HIGH);
digitalWrite(pinred, LOW);
for(;beep_times > 0;beep_times--){
tone(pinbuzz, 33, (1000/8));
delay(250);
}
timer_status = IN_ROUND;
}
void stop_round(int r){
Serial.print("Stops round: ");
Serial.println(r);
digitalWrite(pingreen, LOW);
digitalWrite(pinred, HIGH);
tone(pinbuzz, 33, (1000/2));
delay( min2mil(rest_t) );
timer_status = START_ROUND;
rounds--;
rests++;
}
void off(){
int beep_times = 3;
for(;beep_times > 0;beep_times--){
digitalWrite(pingreen, HIGH);
digitalWrite(pinred, HIGH);
tone(pinbuzz, 33);
delay(250);
noTone(pinbuzz);
digitalWrite(pingreen, LOW);
digitalWrite(pinred, LOW);
}
timer_status = OFF;
#ifdef DEBUG
Serial.println("Timer end.");
#endif
}
/*
* from minutes (int) to milliseconds (unsigned int)
*/
unsigned min2mil(int t){
return ((t*60)*1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment