0

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.

rebatoma
5593 gold badges12 silver badges22 bronze badges
asked Apr 5, 2016 at 19:07
3
  • Please use "code sample" button to display the code. It is very hard to read like you pasted now. Commented Apr 5, 2016 at 19:47
  • thank you Mattia, I am brand new here and appreciate the formatting help. Commented 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) ;-) Commented Apr 5, 2016 at 20:34

1 Answer 1

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);
}
answered Apr 5, 2016 at 23:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.