I am trying to setup a series of lights (traffic lights) that change based on a timer that begins when a button is pressed. The goal is to use the lights to track how long its been since you pressed the button.
The issue I am running into is trying to use the "TimeAlarms" library. I am getting an error 'Timer' was not declared in this scope
I think it may be an issue with the version of Arduino I am using (1.6.8) but I am not sure.
Board: Arduino Leonardo
Here the code:
#include <TimeAlarms.h>
#include <Time.h>
#include <TimeLib.h>
int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int buttonPin = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) ;
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonPin, INPUT);
// set timers in seconds, yellow = 24 hours, red = 48 hours)
// http://www.pjrc.com/teensy/td_libs_TimeAlarms.html
Alarm.timerOnce(86400, YellowTimer);
Alarm.timerOnce(172800, RedTimer);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin))
{
setLights(LOW, LOW, HIGH)
void YellowTimer(){
setLights(LOW, HIGH, LOW)
}
void RedTimer(){
setLights(HIGH, LOW, LOW)
}
}
delay (1000);
}
void setLights(int red, int yellow, int green)
{
digitalWrite(redPin, red);
digitalWrite(yellowPin, yellow);
digitalWrite(greenPin, green);
}
Any help would be greatly appreciated.
-
Please use "code sample" button to display the code. It is very hard to read like you pasted now.Darko– Darko2016年04月05日 19:47:44 +00:00Commented Apr 5, 2016 at 19:47
-
thank you Mattia, I am brand new here and appreciate the formatting help.Chris– Chris2016年04月05日 19:57:21 +00:00Commented Apr 5, 2016 at 19:57
-
@Chris no problem welcome in the community. Also remember that if you want to write something to someone (as you just did in the comment with my name) it's better to mention the name with [at] in order to reach the attention to the particular person (i.e., @Mattia) ;-)rebatoma– rebatoma2016年04月05日 20:34:53 +00:00Commented Apr 5, 2016 at 20:34
1 Answer 1
You must first declare your timer functions. I have placed them above so they are visible when you call the timer. Also in the loop() you want probably to call timer. I have changed the code to reflect what I told above. I do not have ability to test this, so please try. There may be some syntax errors, but I think you can handle it.
#include <TimeAlarms.h>
#include <Time.h>
#include <TimeLib.h>
int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int buttonPin = 5;
void YellowTimer()
{
setLights(LOW, HIGH, LOW)
}
void RedTimer()
{
setLights(HIGH, LOW, LOW)
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) ;
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonPin, INPUT);
// set timers in seconds, yellow = 24 hours, red = 48 hours)
// http://www.pjrc.com/teensy/td_libs_TimeAlarms.html
Alarm.timerOnce(86400, YellowTimer);
Alarm.timerOnce(172800, RedTimer);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin))
{
setLights(LOW, LOW, HIGH)
Alarm.timerOnce(86400, YellowTimer);
Alarm.timerOnce(172800, RedTimer);
}
delay (1000);
}
void setLights(int red, int yellow, int green)
{
digitalWrite(redPin, red);
digitalWrite(yellowPin, yellow);
digitalWrite(greenPin, green);
}
Explore related questions
See similar questions with these tags.