Hi,
Im building a 5 minute countdown timer for starting sailing races. I have been able to get the timer working perfectly however i would like to have a warning buzzer buzz once a second for the last 10 seconds of minute 4,1 and 0.
I have managed to get the buzzer to buzz once a second on the following code (using mills so not to affect the countdown timer)
int buzzerPin = 9; // the number of the LED pin
int buzzerState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 500; // milliseconds of on-time
long OffTime = 500; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((buzzerState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
buzzerState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(buzzerPin, buzzerState); // Update the actual LED
}
else if ((buzzerState == LOW) && (currentMillis - previousMillis >= OffTime))
{
buzzerState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(buzzerPin, buzzerState); // Update the actual LED
}
}
However when i add this to the countdown timer code the buzzer will only sound once every other second in the last 10 seconds of minute 4,1 and 0.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
int S = 00; //default timer start times
int M = 5;
int buzzer = 9; // the number of the buzzer pin
int buzzerState = LOW; // buzzerState used to set the buzzer
unsigned long previousMillis = 0; // will store last time buzzer was updated
long OnTime = 200; // milliseconds of on-time
long OffTime = 800; // milliseconds of off-time
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
void setup()
{
// activate LCD module
lcd.begin (16,2); // for 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
pinMode(buzzer, OUTPUT); //set buzzer pin as output
}
void loop()
{
lcd.home (); // set cursor to 0,0
lcd.print(" TIMER 5-4-1-GO");
lcd.setCursor (8,1); // go to start of 2nd line
lcd.print(":");
S--;
delay(1000);
if(S<0)
{
M--;
S=59;
}
if(M<0)
{
M=4;
}
if(M>9)
{
lcd.setCursor(6,1);
lcd.print(M);
}
else
{
lcd.setCursor(6,1);
lcd.print("0");
lcd.setCursor(7,1);
lcd.print(M);
lcd.setCursor(8,1);
lcd.print(":");
}
if(S>9)
{
lcd.setCursor(9,1);
lcd.print(S);
}
else
{
lcd.setCursor(9,1);
lcd.print("0");
lcd.setCursor(10,1);
lcd.print(S);
lcd.setCursor(11,1);
lcd.print(" ");
}
if((M==4 || M==1 || M==0) && (S<10)) //check to see if its time to activate warning buzzer
//(1 buzz every second in last 10 of min 4,1&0)
{
unsigned long currentMillis = millis();
if((buzzerState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
buzzerState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(buzzer, buzzerState); // Update the actual LED
}
else if ((buzzerState == LOW) && (currentMillis - previousMillis >= OffTime))
{
buzzerState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(buzzer, buzzerState); // Update the actual LED
}
}
}
I have tried changing the "long OnTime = 200" and "long OffTime = 800" however this doesn't seem to have any effect once the buzzer code was added to the countdown timer code.
All my circuitry seems to be working find so im hoping its just something simple I've missed in the code.
Any help and suggestions appreciated
Cheers