Countdown issues

Hi...first post so please be caring.
The code above is for a sailing countdown timer. When it runs, you select either a 3 or 5 min countdown. It gives you an extra 25 seconds or so before it starts and also gives warning beeps through a buzzer a few seconds before the full minute. It then sounds a 12v air compressor horn at 5mins, 4mins, 1 min and also at 0. It also sounds the horn 5 times before the 5 min horn, 4 times before the 4 min horn etc.
The code was written by someone else and I have no idea of how to write code. None at all!
I would like the buzzer to sound before the 5, 4, 1 and 0 mark, but only have the horn sound at the 5,4,1 and zero without the pre-emptive horns sounding.
Can anyone help please? Thanks. K

The code was written by someone else and I have no idea of how to write code. None at all!

If you master the examples that come with the IDE you will be able to do this.
Also look at the Fundamentals section on the web site.
.

Thanks but, I am sad to say, I have no interest in coding...hardware is my thing. I am looking for a quick fix to get an automatic starter installed at my sailing club so anyone willing to help with the code would be very welcome.

kh886:
The code above is for a sailing countdown timer. When it runs, you select either a 3 or 5 min countdown. It gives you an extra 25 seconds or so before it starts and also gives warning beeps through a buzzer a few seconds before the full minute.

So the countdown is actually not 3:00 or 5:00 minutes, but it is 3:25 min or 5:25 min?
And before each full minute there are some beeps to happen using a buzzer and after the full minute a horn is providing sound for some time?

kh886:
Can anyone help please?

The linked code is very messy, and even I cannot fully understand it.

If you can make your description a bit more clear about button input options, actions during countdown and the final action when the countdown reaches 0:00, I'd give it a try to create a more modular and understandable code for

  • button input
  • button pressed and countdown time processing
  • output for display, buzzer, horn
    That seems not to be too complicated with a standarized hardware like a LCD Keypad Shield.

Thanks for the reply. Not entirely sure about the structure myself...the dangers of using someone elses code to 'best fit'. Am using an lcd shield. The select button selects either a 3 or 5 min countdown. The left button starts the countdown. What I would like is to start a sequence at, say 5mins and 30 secs, have it countdown to 5mins and 10secs normally and then beep the buzzer every second until 5mins where a relay triggers the air horn for 2seconds. The countdown then needs to continue with the sequence repeated at 4,1 and zero minutes.
To be honest I would prefer to keep just the 5min sequence and forget about the 3. Thanks again

kh886:
What I would like is to start a sequence at, say 5mins and 30 secs,

No problem. If you'd like, you can have the countdown time adjustable, let's say in steps of 1 minute, adjustable setting with UP/DOWN buttons:
3:30
4:30
5:30 (default after power-on/reset)
6:30
7:30
8:30
and so on. I don't think that this would be a big problem.

kh886:
have it countdown to 5mins and 10secs normally and then beep the buzzer every second until 5mins

So from 5:10 down to 5:01 buzz the buzzer earch second for a short time.
How long? 0.5 seconds buzzing, 0.5 seconds pausing?

kh886:
where a relay triggers the air horn for 2seconds. The countdown then needs to continue with the sequence repeated at 4,1 and zero minutes.

That should be no problem.
I'll give it a try.

That sounds awesome! Thankyou. 0.5 second beeps would be great. Could you try to keep pins 2 and 11 as I have already soldered relays to them? Thanks again. K

kh886:
That sounds awesome! Thankyou. 0.5 second beeps would be great. Could you try to keep pins 2 and 11 as I have already soldered relays to them? Thanks again. K

No problem.
I'll use #define declarations for the pin numbers, so they can easily change in the source code.

What type of relays?

Typical "mechanical" 5V relay modules with opto couplers and "active LOW" switching logic?

They're just the usual arduino modules...

kh886:
They're just the usual arduino modules...

Try again. Arduino does not make relays.

kh886:
They're just the usual arduino modules...

OK, if the modules are offered "for Arduino", they should work on 5V logic and most likely the modules should be switching on "OUTPUT/LOW". The "usual" modules also should have signalling LEDs for signalling the switch state.

I think I'll also create a small "hardware test sketch" you can use for manually switching the relays ON/OFF and testing the hardware.

The buzzer and the horn hardware is powered by 12V DC, both?

Sorry...heading to bed. They are 5v relays run from the arduino. The horn is powered externally by a 12v input but I was going to power the buzzer from the arduino....

kh886:
Sorry…heading to bed. They are 5v relays run from the arduino. The horn is powered externally by a 12v input but I was going to power the buzzer from the arduino…

OK, so the buzzer and the horn may use different switching logic.

Buzzer ==> powered directly from Arduino output pin ==> sounds if pin is HIGH
Horn ==> powered by relay module ==> sounds if relay INx control pin is set to LOW

Here is a hardware test sketch for manual switching and hardware testing:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

enum {btnNONE, btnSELECT, btnLEFT, btnUP, btnDOWN, btnRIGHT, NUM_KEYS };
const byte ButtonsPin = A0;

const byte RELAY_HORN_PIN= 11; // horn relay
boolean RELAY_HORN_ACTIVE=LOW;
const byte RELAY_BEEP_PIN= 2;  // beeper 
boolean RELAY_BEEP_ACTIVE=HIGH;

int read_LCD_buttons()
{
  int returnValue;
  // read ADC value from buttons pin
  int adc_key_in =  analogRead (ButtonsPin);
  // read ADC value twice and check for stable value reading
  if (abs(analogRead(ButtonsPin) - adc_key_in) > 1) return btnNONE; // ADC value is not stable ==> return btnNONE
  if (adc_key_in < 50) returnValue = btnRIGHT;
  else if (adc_key_in < 195) returnValue = btnUP;
  else if (adc_key_in < 410) returnValue = btnDOWN;
  else if (adc_key_in < 650) returnValue = btnLEFT;
  else if (adc_key_in < 999) returnValue = btnSELECT;
  else returnValue = btnNONE;
  // beginners code logic: do "busy waiting" until no button is pressed
  while (adc_key_in < 999) adc_key_in = analogRead(ButtonsPin);
  return returnValue;
}

void setup() 
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("***  Relay  ***");
  lcd.setCursor(0, 1);
  lcd.print("*** Testing ***");  
  delay(3000);
  lcd.clear();
  digitalWrite(RELAY_HORN_PIN, !RELAY_HORN_ACTIVE);
  pinMode(RELAY_HORN_PIN, OUTPUT);
  digitalWrite(RELAY_BEEP_PIN, !RELAY_BEEP_ACTIVE);
  pinMode(RELAY_BEEP_PIN, OUTPUT);
}


void loop() 
{
  byte key= read_LCD_buttons();
  if (key== btnLEFT) digitalWrite(RELAY_BEEP_PIN, !digitalRead(RELAY_BEEP_PIN));
  if (key== btnRIGHT) digitalWrite(RELAY_HORN_PIN, !digitalRead(RELAY_HORN_PIN));
  lcd.setCursor(0,0);
  if (digitalRead(RELAY_BEEP_PIN)==RELAY_BEEP_ACTIVE) lcd.print("  ON");
  else lcd.print(" OFF");
  if (digitalRead(RELAY_HORN_PIN)==RELAY_HORN_ACTIVE) lcd.print("  ON");
  else lcd.print(" OFF");
}

Usage:
Button LEFT switches buzzer ON/OFF
Button RIGHT switches horn ON/OFF

For a first test, do not actually connect the horn, but just watch the signalling LED lighting on the relay module:

  • if display says “ON”, the (red) signalling LED on the LED module should light up
  • if display says “OFF”, the (red) signalling LED on the LED module should stay dark
    After that test is OK, connect the horn, so that the horn is ON when the signalling LED on the relay module is also ON.

The buzzer should buzz accordingly to display ON/OFF when connected directly to the pin.

Does the hardware work like that when using the test sketch?

You work quickly! Just back from work so will test it soon. Thanks for your help with this...really appreciated. K

Ok. I cannot get down to the sailing club to wire up the horn but I have tested the relays. With the right button on, pin 11 is triggering the relay. However the left button does not work. Interestingly the relay does turn on when both buttons are off via pin 2. Welcome to my life of confusion?! :slight_smile:

kh886:
Ok. I cannot get down to the sailing club to wire up the horn but I have tested the relays. With the right button on, pin 11 is triggering the relay. However the left button does not work. Interestingly the relay does turn on when both buttons are off via pin 2. Welcome to my life of confusion?! :slight_smile:

The sketch I posted in reply #12 is for this hardware:
Pin-11 ==> mechanical relay
Pin-2 == directly connected to buzzer

If you want to connect two mechanical relays, you will have to use "active LOW" setting for both. Change the code initialization to:

const byte RELAY_HORN_PIN= 11; // horn relay
boolean RELAY_HORN_ACTIVE=LOW;
const byte RELAY_BEEP_PIN= 2;  // beeper 
boolean RELAY_BEEP_ACTIVE=LOW;

Then the code is ready for two mechanical relays on Pin-11 AND on Pin-2.

So what do you actually want to have:

  • horn pin connected with relay module
  • buzzer pin connected directly to Arduino (absolute max. current 40mA)

Or do you want:

  • horn pin connected with relay module
  • buzzer pin connected with relay module

It's different "ON" switching logic:
A directly connected buzzer is "ON" when the pin is OUTPUT/HIGH.
A connected mechanical relay module with opto couplers is "ON" when the pin is OUTPUT/LOW.

Ahhh...before I had a 12v buzzer with separate power source, hence the second relay. I wanted to run a 5v buzzer from the arduino but haven't picked one up yet to test it. I do have a buzzer module (i/o, vcc, grd) but I have a feeling that it needs a square wave for it to work. Have removed the other relay now so any ideas for generating the required signal so I could try this buzzer??

kh886:
Ahhh…before I had a 12v buzzer with separate power source, hence the second relay. I wanted to run a 5v buzzer from the arduino but haven’t picked one up yet to test it. I do have a buzzer module (i/o, vcc, grd) but I have a feeling that it needs a square wave for it to work. Have removed the other relay now so any ideas for generating the required signal so I could try this buzzer??

Buzzers are available in two types:
“active buzzers” ==> you provide HIGH or LOW and the buzzer is creating its own sound or is silent
“passive buzzers” ==> you have to provide a tone frequency and the buzzer plays a frequency you want.

Here is a changed code for the following hardware:

  • pin-11 ==> mechanical relay module (active LOW)
  • pin-2 ==> passive piezo speaker (needs tone frequency driving to play sound)
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

enum {btnNONE, btnSELECT, btnLEFT, btnUP, btnDOWN, btnRIGHT, NUM_KEYS };
const byte ButtonsPin = A0;

const byte RELAY_HORN_PIN= 11; // horn relay
boolean RELAY_HORN_ACTIVE=LOW;
const byte SPEAKER_PIN= 2;  // speaker (passive piezo buzzer)
boolean speakerTone=false;
// boolean RELAY_BEEP_ACTIVE=HIGH;

int read_LCD_buttons()
{
  int returnValue;
  // read ADC value from buttons pin
  int adc_key_in =  analogRead (ButtonsPin);
  // read ADC value twice and check for stable value reading
  if (abs(analogRead(ButtonsPin) - adc_key_in) > 1) return btnNONE; // ADC value is not stable ==> return btnNONE
  if (adc_key_in < 50) returnValue = btnRIGHT;
  else if (adc_key_in < 195) returnValue = btnUP;
  else if (adc_key_in < 410) returnValue = btnDOWN;
  else if (adc_key_in < 650) returnValue = btnLEFT;
  else if (adc_key_in < 999) returnValue = btnSELECT;
  else returnValue = btnNONE;
  // beginners code logic: do "busy waiting" until no button is pressed
  while (adc_key_in < 999) adc_key_in = analogRead(ButtonsPin);
  return returnValue;
}

void setup() 
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("***  Relay  ***");
  lcd.setCursor(0, 1);
  lcd.print("*** Testing ***");  
  delay(3000);
  lcd.clear();
  digitalWrite(RELAY_HORN_PIN, !RELAY_HORN_ACTIVE);
  pinMode(RELAY_HORN_PIN, OUTPUT);
}


void loop() 
{
  byte key= read_LCD_buttons();
  if (key== btnLEFT) 
  {
    speakerTone=!speakerTone;
    if (speakerTone) tone(SPEAKER_PIN, 500);
    else noTone(SPEAKER_PIN);
  }
  if (key== btnRIGHT) digitalWrite(RELAY_HORN_PIN, !digitalRead(RELAY_HORN_PIN));
  lcd.setCursor(0,0);
  if (speakerTone) lcd.print("  ON");
  else lcd.print(" OFF");
  if (digitalRead(RELAY_HORN_PIN)==RELAY_HORN_ACTIVE) lcd.print("  ON");
  else lcd.print(" OFF");
}

LEFT button ==> Piezo speaker plays 500 Hz frequency (ON) or is silent (OFF)
RIGHT button ==> switch relay ON/OFF

For frequency generation I’m using the Arduino tone() function.

Sorry...just managed to lose my reply. Will dig out the passive buzzer from where I hope it might be and will try it tomorrow when I finally break up for Christmas (teacher gets a rest at last). Thanks again for devoting your time to help me.

Found it this morning...both buzzer and relay work a treat on pin 2 and 11. Excellent!