I'm trying to build a custom timer but my code doesn't seem to function properly. There are 5 switches that control 1 LED each. When each is pressed they light the LED for a set period of time and when the time is up, a piezo buzzer goes off until the reset button is pressed. I really new to programming the arduino, but I thought I had a pretty good program until I tried to run it. Any guidance would be greatly appreciated!
Code is as follows:
/* 5 buttons each representing 30, 60, 90, 120, 180 seconds, when pressed, will illuminate their corresponding LED
and after the time duration is complete, a piezo buzzer will pulse until the reset button is pressed.
*/
// constants that will not change. Used to set pin designations.
const int led30Pin = 13;
const int button30Pin = 12;
const int led60Pin = 11;
const int button60Pin = 10;
const int led90Pin = 9;
const int button90Pin = 8;
const int led120Pin = 7;
const int button120Pin = 6;
const int led180Pin = 5;
const int button180Pin = 4;
const int resetPin = 3;
const int buzzerPin = 2;
// variables that will change
int button30State = 0;
int button60State = 0;
int button90State = 0;
int button120State = 0;
int button180State = 0;
int resetState = 0;
int timeState = 0;
void setup()
{
// initialize pins as outputs
pinMode(led30Pin, OUTPUT);
pinMode(led60Pin, OUTPUT);
pinMode(led90Pin, OUTPUT);
pinMode(led120Pin, OUTPUT);
pinMode(led180Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
//initialize pins as inputs
pinMode(button30Pin, INPUT);
pinMode(button60Pin, INPUT);
pinMode(button90Pin, INPUT);
pinMode(button120Pin, INPUT);
pinMode(button180Pin, INPUT);
pinMode(resetPin, INPUT);
}
void loop()
{
// read the state of the pushbutton values:
resetState = digitalRead(resetPin);
button30State = digitalRead(button30Pin);
button60State = digitalRead(button60Pin);
button90State = digitalRead(button90Pin);
button120State = digitalRead(button120Pin);
button180State = digitalRead(button180Pin);
// check if the pushbutton is pressed.
// if it is, set all LEDs and buzzer LOW:
if (resetState == HIGH)
{
// turn LEDs and buzzer off:
digitalWrite(led30Pin, LOW);
digitalWrite(led60Pin, LOW);
digitalWrite(led90Pin, LOW);
digitalWrite(led120Pin, LOW);
digitalWrite(led180Pin, LOW);
digitalWrite(buzzerPin, LOW);
}
// check if pushbutton is pressed.
//if it is, set led##Pin HIGH and start timerState decrements.
//when timeState reaches 0, turn buzzerPin HIGH.
if (button30State == HIGH)
{
//turn led30Pin on
digitalWrite(led30Pin, HIGH);
(timeState == 30);
if(timeState > 0);
delay (1000);
(timeState --);
if(timeState == 0);
digitalWrite(buzzerPin, HIGH);
}
if(button60State ==HIGH)
{
digitalWrite(led60Pin, HIGH);
(timeState == 60);
if(timeState > 0);
delay (1000);
(timeState --);
if(timeState == 0);
digitalWrite(buzzerPin, HIGH);
}
if(button90State ==HIGH)
{
digitalWrite(led90Pin, HIGH);
(timeState == 90);
if(timeState > 0);
delay (1000);
(timeState --);
if(timeState == 0);
digitalWrite(buzzerPin, HIGH);
}
if(button120State ==HIGH)
{
digitalWrite(led120Pin, HIGH);
(timeState == 120);
if(timeState > 0);
delay (1000);
(timeState --);
if(timeState == 0);
digitalWrite(buzzerPin, HIGH);
}
if(button180State ==HIGH)
{
digitalWrite(led180Pin, HIGH);
(timeState == 180);
if(timeState > 0);
delay (1000);
(timeState --);
if(timeState == 0);
digitalWrite(buzzerPin, HIGH);
}
//otherwise turn all output pins LOW.
else {
digitalWrite(buzzerPin, LOW);
digitalWrite(led30Pin, LOW);
digitalWrite(led60Pin, LOW);
digitalWrite(led90Pin, LOW);
digitalWrite(led120Pin, LOW);
digitalWrite(led180Pin, LOW);
}
}