Hello all,
I am puzzled about different behaviour of 'delay()', perhaps anyone has an idea what is wrong.
I'm using Arduino UNO.
delay(2000) shall be used to switch the LED on pin 13 on and off so I have it 2 seconds on, 2 seconds off.
It works with this simple code:
void setup() { // put your setup code here, to run once:
pinMode(13, OUTPUT);
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(2000);
}
But if I try to use an interrupt to let the LED be on for 2 seconds, I only see a short flash (not even a second).
int LedOnTime = 2000; // LED shall be on for 2 seconds
int LoopDelay = 500; // something for the loop to do
void setup() { // put your setup code here, to run once:
cli(); //stop interrupts
//set timer1 interrupt at 4 Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// set entire TCCR1B register to 0
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 4 Hz increments
OCR1A = 65535;// = (16*10^6) / (4*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interrupts
pinMode(13, OUTPUT);
}
ISR(TIMER1_COMPA_vect) { //timer1 interrupt 4 Hz toggles pin 13 (LED)
digitalWrite(13, HIGH);
delay(LedOnTime);
digitalWrite(13, LOW);
}
void loop() { // put your main code here, to run repeatedly:
delay(LoopDelay);
}
Increasing the value of LoopDelay doesn't help.
Increasing the value of LedOnTime doesn't do a visible change (5000 still only gives a flash).
Using 'delay(2000)' instead of 'delay(LedOnTime)' makes no difference.
Any idea what I have done wrong?
Thanks in advance for you help,
Volker