Multiple Countdown Timers Based on Switches

I'm trying to create a device that will take 1 of 4 buttons and either start a 40 sec timer, 25 sec timer, 60 sec timer, or "reset" the timer. LED will illuminate once button is pushed until the timer expires. At 30/25 sec remaining in the 60/40 sec timer it will trigger a vibration buzzer for a short amount of time/bursts. When all timers expire the buzzer will go off for 3 sec to let you know it has expired. I am coming from (very basic, 1 semester of college) Java and I think I have everything software wise worked out, except I am getting "expected unqualified-id before" errors and "note: in expansion of macro HIGH" Any advice/help that could be provided would be greatly appreciated.


int fortyButton = 4;       // pin for forty second clock
int twentyfiveButton = 5;  // pin for 25 second clock
int resetButton = 6;       // pin for reset button
int sixtyButton = 3;       // pin for reset button
int controlPin = 2;        // pin to control LED
int buzzerPin = 1;         // pin to control buzzer
unsigned long timerCount = 0;

void setup() {

  pinMode(fortyButton, INPUT);       //set pin as receiving pin
  pinMode(twentyfiveButton, INPUT);  //set pin as receiving pin
  pinMode(resetButton, INPUT);       //set pin as receiving pin
  pinMode(sixtyButton, INPUT);       //set pin as receiving pin
  digitalWrite(controlPin, LOW);     //starting the control power as off
  digitalWrite(buzzerPin, LOW);      //starting the buzzer power as off
}

void loop() {

  //40 second button code
  if (digitalRead(fortyButton) = HIGH)  //if forty button pushed
  {
    timerCount = 40 * 10;  // set timer to 40 seconds

    {
      if (timerCount != 0)
        ;
      digitalWrite(controlPin, HIGH);  //power LED
      timerCount = timerCount - 1;
      delay(100);  //1 decisecond delay
    }
    {
      if (timerCount = 2500)
        ;                             // if timer hits 25 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
    }
    {
      if (timerCount = 500)
        ;                             // if timer hits 35 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
    }
  } else {                          //if timer has run out
    digitalWrite(controlPin, LOW);  //Shutoff LED
    digitalWrite(buzzerPin, HIGH);  //Start Buzzer
    delay(3000);                    // power buzzer for 3 sec
    digitalWrite(buzzerPin, LOW);   //turn buzzer off
  }
}
//25 second button code
if (digitalRead(twentyfiveButton) = HIGH)  // if 25 sec button is pushed
{
  timerCount = 25 * 10;  //set timer to 25 seconds

  {
    if (timerCount != 0)
      ;
    digitalWrite(controlPin, HIGH);  //power LED
    timerCount = timerCount - 1;
    delay(100);  //1 decisecond delay
  }
  {
    if (timerCount = 500)
      ;                             // if timer hits 35 seconds
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
  }
} else {                          //if timer has run out
  digitalWrite(controlPin, LOW);  //Shutoff LED
  digitalWrite(buzzerPin, HIGH);  //Start Buzzer
  delay(3000);                    // power buzzer for 3 sec
  digitalWrite(buzzerPin, LOW);   //turn buzzer off
}

//60 second code
if (digitalRead(sixtyButton) = HIGH)  // if sixty button pushed
{
  timerCount = 60 * 10;  // set timer to 60 seconds

  {
    if (timerCount != 0)
      ;
    digitalWrite(controlPin, HIGH);  //power LED
    timerCount = timerCount - 1;
    delay(100);  //1 decisecond delay
  }
  {
    if (timerCount = 3000)
      ;                             // if timer hits 30 seconds
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(300);                     //wait 3/10 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(300);                     //wait 3/10 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(300);                     //wait 3/10 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(300);                     //wait 3/10 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(300);                     //wait 3/10 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(300);                     //wait 3/10 of a sec
  }
  {
    if (timerCount = 500)
      ;                             // if timer hits 35 seconds
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, HIGH);  //turn on buzzer
    delay(500);                     //wait 1/2 of a sec
    digitalWrite(buzzerPin, LOW);   //turn off buzzer
    delay(500);                     //wait 1/2 of a sec
  }
  else {                            //if timer has run out
    digitalWrite(controlPin, LOW);  //Shutoff LED
    digitalWrite(buzzerPin, HIGH);  //Start Buzzer
    delay(3000);                    // power buzzer for 3 sec
    digitalWrite(buzzerPin, LOW);   //turn buzzer off
  }
}
if (digitalRead(resetButton) == HIGH)  //if reset button is pushed
  timerCount = 0;
digitalWrite(controlPin, LOW);  //turn off the LED
}

Your if statements look badly constructed... in quite a few places.

Try...

if (timerCount != 0)
  {
    digitalWrite(controlPin, HIGH);  //power LED
    timerCount = timerCount - 1;
    delay(100);  //1 decisecond delay
  }

You also have a few extra } throughout the code.

And when doing a comparison, be sure to use == instead of =, for example...

I've fixed the compiler issues in the version below... so it now compiles. Will leave it to you to figure out whether it actually does what you want...

int fortyButton = 4;       // pin for forty second clock
int twentyfiveButton = 5;  // pin for 25 second clock
int resetButton = 6;       // pin for reset button
int sixtyButton = 3;       // pin for reset button
int controlPin = 2;        // pin to control LED
int buzzerPin = 1;         // pin to control buzzer
unsigned long timerCount = 0;

void setup()
{

  pinMode(fortyButton, INPUT);       //set pin as receiving pin
  pinMode(twentyfiveButton, INPUT);  //set pin as receiving pin
  pinMode(resetButton, INPUT);       //set pin as receiving pin
  pinMode(sixtyButton, INPUT);       //set pin as receiving pin
  digitalWrite(controlPin, LOW);     //starting the control power as off
  digitalWrite(buzzerPin, LOW);      //starting the buzzer power as off
}

void loop() {

  //40 second button code
  if (digitalRead(fortyButton) == HIGH)  //if forty button pushed
  {
    timerCount = 40 * 10;  // set timer to 40 seconds


    if (timerCount != 0)
    {
      digitalWrite(controlPin, HIGH);  //power LED
      timerCount = timerCount - 1;
      delay(100);  //1 decisecond delay
    }

    if (timerCount == 2500)
    { // if timer hits 25 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
    }

    if (timerCount == 500)
    { // if timer hits 35 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
    }
  } else {                          //if timer has run out
    digitalWrite(controlPin, LOW);  //Shutoff LED
    digitalWrite(buzzerPin, HIGH);  //Start Buzzer
    delay(3000);                    // power buzzer for 3 sec
    digitalWrite(buzzerPin, LOW);   //turn buzzer off
  }
  // }
  //25 second button code
  if (digitalRead(twentyfiveButton) == HIGH)  // if 25 sec button is pushed
  {
    timerCount = 25 * 10;  //set timer to 25 seconds


    if (timerCount != 0)
    {
      digitalWrite(controlPin, HIGH);  //power LED
      timerCount = timerCount - 1;
      delay(100);  //1 decisecond delay
    }

    if (timerCount == 500)
    { // if timer hits 35 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
    }
  } else {                          //if timer has run out
    digitalWrite(controlPin, LOW);  //Shutoff LED
    digitalWrite(buzzerPin, HIGH);  //Start Buzzer
    delay(3000);                    // power buzzer for 3 sec
    digitalWrite(buzzerPin, LOW);   //turn buzzer off
  }

  //60 second code
  if (digitalRead(sixtyButton) == HIGH)  // if sixty button pushed
  {
    timerCount = 60 * 10;  // set timer to 60 seconds


    if (timerCount != 0)
    {
      digitalWrite(controlPin, HIGH);  //power LED
      timerCount = timerCount - 1;
      delay(100);  //1 decisecond delay
    }

    if (timerCount == 3000)
    { // if timer hits 30 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(300);                     //wait 3/10 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(300);                     //wait 3/10 of a sec
    }

    if (timerCount == 500)
    { // if timer hits 35 seconds
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, HIGH);  //turn on buzzer
      delay(500);                     //wait 1/2 of a sec
      digitalWrite(buzzerPin, LOW);   //turn off buzzer
      delay(500);                     //wait 1/2 of a sec
    }
    else
    { //if timer has run out
      digitalWrite(controlPin, LOW);  //Shutoff LED
      digitalWrite(buzzerPin, HIGH);  //Start Buzzer
      delay(3000);                    // power buzzer for 3 sec
      digitalWrite(buzzerPin, LOW);   //turn buzzer off
    }

    if (digitalRead(resetButton) == HIGH)  //if reset button is pushed
      timerCount = 0;

    digitalWrite(controlPin, LOW);  //turn off the LED
  }
}

with all the explicit timing loops, the reset button won't be tested until the end of each. using millis() allows other code to run rather than just waiting for some time to expire

consider the following which attempts to parameterize what you're trying to do using a table of values for each case rather than explicit code for each.

#undef MyHW
#ifdef MyHW
# include "simLib.hh"
byte PinRst  = A1;
byte Pin25   = A2;
byte Pin40   = A3;
byte Pin60   = A3;
byte PinCtrl = 12;
byte PinBuz  = 13;

#else
byte PinRst  = 6;
byte Pin25   = 5;
byte Pin40   = 4;
byte Pin60   = 3;

byte PinCtrl = 12;
byte PinBuz  = 13;
#endif

unsigned long timerCount = 0;

struct Timer {
    byte            PinBut;
    unsigned long   MsecPeriod;     // total length of time
    unsigned long   MsecBuzzer;     // time buzzer alternates
};

Timer tmrs [] = {
    { Pin25,  5000, 0 },        // times short for testing
    { Pin40,  9000, 5000  },
    { Pin60, 12000, 5000  },
};
const unsigned Ntmr = sizeof(tmrs) / sizeof(Timer);

const unsigned long MsecBuzPeriod = 250;;

unsigned long msecPeriod;
unsigned long msecLst;
unsigned long msecPeriod2;
unsigned long msecLst2;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void off (void)
{
    Serial.println (" off");

    digitalWrite (PinCtrl, Off); 
    digitalWrite (PinBuz,  Off);

    msecPeriod = msecPeriod2 = 0;
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    // check timer
    if (0 < msecPeriod && msec - msecLst >= msecPeriod)  {
        msecPeriod2 = 0;

        digitalWrite (PinBuz, On); 
        delay (1000);
        digitalWrite (PinBuz, Off); 

        off ();
    }

    // check 2nd  timer
    if (0 < msecPeriod2 && msec - msecLst2 >= msecPeriod2)  {
        Serial.println (" timer2");
        msecLst2    = msec;
        msecPeriod2 = MsecBuzPeriod;
        digitalWrite (PinBuz, ! digitalRead (PinBuz));
    }

    // check reset
    if (LOW == digitalRead (PinRst))
        off ();

    // check buttons
    for (unsigned n = 0; n <Ntmr; n++)  {
        if (LOW == digitalRead (tmrs [n].PinBut))  {
            msecPeriod = tmrs [n].MsecPeriod;
            msecLst    = msec;

            if (tmrs [n].MsecBuzzer)  {
                msecPeriod2 = tmrs [n].MsecPeriod - tmrs [n].MsecBuzzer;
                msecLst2    = msec;
                Serial.print   (" timer2 set ");
                Serial.println (msecPeriod2);
            }

            digitalWrite (PinCtrl, On); 
        }
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (PinRst, INPUT_PULLUP);
    pinMode (Pin25,  INPUT_PULLUP);
    pinMode (Pin40,  INPUT_PULLUP);
    pinMode (Pin60,  INPUT_PULLUP);

    digitalWrite (PinCtrl, Off); 
    digitalWrite (PinBuz,  Off);
    pinMode      (PinCtrl, OUTPUT); 
    pinMode      (PinBuz,  OUTPUT);
}

another issue is recognizing button actions.

you configured the buttons as INPUT and tested for HIGH. but this is unreliable without anything pulling the pin LOW.

the pins on the processors used in Arduinos can be configured with internal pullup resistors which, of course will pull the pin HIGH. the switch needs to be connected between the pin and ground and will pull the pin LOW.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.