Hi,
I'm trying to work with the TimerOne library (Arduino Playground - Timer1) but there are some instructions that doesn't work. I hope you can give me a hint
The serial port is used as a debugger and it's toggling all the time between cases 1 and 2 even though I don't press the buttom. In other words, the program is entering in the ISRs all the time and I don't have any remote idea why.
Thank you, coummunity.
// Every time the pushbuttom is pressed, the LED should turn on during 3 seconds
// After this time, the ISR of the timer turn off the LED.
#include <TimerOne.h> // http://playground.arduino.cc/Code/Timer1
//Variables
const int pushButton = 2; // digital pin 2 has a pushbutton attached to it. Give it a name:
volatile int ledPin = 13; // select the pin for the LED
volatile int writeSerial = 0;
void setup(void)
{
pinMode(pushButton, INPUT); // make the pushbutton's pin an input:
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pushButton),turnon_LED,FALLING);
Timer1.initialize(3000000); // timer = 3 seconds
Timer1.stop(); //stop the counter
Timer1.restart(); //set the clock to zero
Timer1.attachInterrupt(ISR_turnoff_LED); // ISR to turn off the led
Serial.begin(9600);
}
void ISR_turnoff_LED()
{
digitalWrite(ledPin, LOW);
Timer1.stop(); //stop the counter
Timer1.restart(); //set the clock to zero
writeSerial = 1; // flag for the serial port
}
void turnon_LED()
{
delay(10);
Timer1.restart(); //set our stopwatch to 0
Timer1.start(); //and start it up
digitalWrite(ledPin, HIGH);
//Timer1.initialize(3000000);
writeSerial = 2; // flag
}
// the loop function runs over and over again forever
void loop() {
switch(writeSerial) // simulating debugger
{
case 0:
{
Serial.print("Waiting for first interaction\n");
delay(1); // delay in between reads for stability
}
case 1:
{
Serial.print("led off\n");
delay(1); // delay in between reads for stability
}
case 2:
{
Serial.print("led on\n");
delay(1); // delay in between reads for stability
}
} // end of switch
}