I found a countdown timer i tested from Arduino Playground - CountDownTimer.
The code from the example worked (code 1) but my spin on the code (buzzer-prototype-v.1) by adding buttons, lcd screen and a relay as output.
The project is a system for paintball for the gamemode speedball.
4 min countdown per paintball game.
3 buttons to ref box. Start, pause and stop/restart.
2 buttons for players to stop the time onfield to win the game.
/*The project is a system for paintball for the gamemode speedball.
* 4 min countdown per paintball game.
* 3 buttons to ref box. Start, pause and stop/restart.
* 2 buttons for players to stop the time onfield to win the game.
*/
unsigned long Watch, _micro, time = micros();
unsigned int Clock = 0, R_clock;
boolean Reset = false, Stop = false, Paused = false;
volatile boolean timeFlag = false;
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified backpack
//Gi arduino pinnene navn
int start_button 9;
int pause_button 8;
int stop_button 7;
int button_a 6;
int button_b 5;
int buzzer 4;
void setup() {
Serial.begin(115200);
// activate LCD module
lcd.begin (16, 2); // for 16 x 2 LCD module
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
//Set timer (h,m,s)
SetTimer(0, 4, 0); //4 min
//Setter arduino pinne modusen til ut- og innganger
pinMode(start_button, INPUT);
pinMode(pause_button, INPUT);
pinMode(stop_button, INPUT);
pinMode(button_a, INPUT);
pinMode(button_b, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
//Leser statusen til knappene
buttonState_start = digitalRead(start_button);
buttonState_pause = digitalRead(pause_button);
buttonState_stop = digitalRead(stop_button);
buttonState_a = digitalRead(button_a);
buttonState_b = digitalRead(button_b);
//Starter timer biblioteket
CountDownTimer();
//Skriver første linje på lcd skjerm
lcd.home (); // set cursor to 0,0
lcd.print("Orklandpaintball");
if (buttonState_start == LOW) {
StartTimer();
lcd.setCursor (4, 1);
lcd.print(ShowMinutes());
lcd.print(":");
lcd.print(ShowSeconds());
}
if (buttonState_pause == LOW) {
PauseTimer();
lcd.setCursor (2, 1);
lcd.print("Dommer pause");
delay(1500);
lcd.setCursor (4, 1);
lcd.print(ShowMinutes());
lcd.print(":");
lcd.print(ShowSeconds());
}
if (buttonState_stop == LOW) {
StopTimer()
lcd.setCursor (4, 1);
lcd.print("Stop");
delay(1500);
lcd.setCursor (0, 1);
lcd.print("Reset knapp A&B");
ResetTimer()
}
if (buttonState_a == LOW) {
PauseTimer();
lcd.setCursor (4, 1);
lcd.print(ShowMinutes());
lcd.print(":");
lcd.print(ShowSeconds());
lcd.setCursor (2, 0);
lcd.print("Seier side B");
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
}
if (buttonState_b == LOW) {
PauseTimer();
lcd.setCursor (4, 1);
lcd.print(ShowMinutes());
lcd.print(":");
lcd.print(ShowSeconds());
lcd.setCursor (2, 0);
lcd.print("Seier side A");
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
delay(1500);
}
}
boolean CountDownTimer()
{
static unsigned long duration = 1000000; // 1 second
timeFlag = false;
if (!Stop && !Paused) // if not Stopped or Paused, run timer
{
// check the time difference and see if 1 second has elapsed
if ((_micro = micros()) - time > duration )
{
Clock--;
timeFlag = true;
if (Clock == 0) // check to see if the clock is 0
Stop = true; // If so, stop the timer
// check to see if micros() has rolled over, if not,
// then increment "time" by duration
_micro < time ? time = _micro : time += duration;
}
}
return !Stop; // return the state of the timer
}
void ResetTimer()
{
SetTimer(R_clock);
Stop = false;
}
void StartTimer()
{
Watch = micros(); // get the initial microseconds at the start of the timer
time = micros(); // hwd added so timer will reset if stopped and then started
Stop = false;
Paused = false;
}
void StopTimer()
{
Stop = true;
}
void StopTimerAt(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
if (TimeCheck(hours, minutes, seconds) )
Stop = true;
}
void PauseTimer()
{
Paused = true;
}
void ResumeTimer() // You can resume the timer if you ever stop it.
{
Paused = false;
}
void SetTimer(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
// This handles invalid time overflow ie 1(H), 0(M), 120(S) -> 1, 2, 0
unsigned int _S = (seconds / 60), _M = (minutes / 60);
if(_S) minutes += _S;
if(_M) hours += _M;
Clock = (hours * 3600) + (minutes * 60) + (seconds % 60);
R_clock = Clock;
Stop = false;
}
void SetTimer(unsigned int seconds)
{
// StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60);
Clock = seconds;
R_clock = Clock;
Stop = false;
}
int ShowHours()
{
return Clock / 3600;
}
int ShowMinutes()
{
return (Clock / 60) % 60;
}
int ShowSeconds()
{
return Clock % 60;
}
unsigned long ShowMilliSeconds()
{
return (_micro - Watch)/ 1000.0;
}
unsigned long ShowMicroSeconds()
{
return _micro - Watch;
}
boolean TimeHasChanged()
{
return timeFlag;
}
// output true if timer equals requested time
boolean TimeCheck(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
return (hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds());
}
code_1.ino (1.3 KB)