Hi, I am trying to accomplish something simple and it's driving me nuts. I am building a central control module for a motorcycle and need to control the blinkers, etc. The attached program simply tries to debounce a switch (to limit components) using the millis() function to ignore changes faster than 50ms, and the timer1 interrupt is supposed to blink the turn signal at 1hz. This program merely puts the interrupt in service for timer1, doesn't actually do anything other than reset the timer. It's causing totally erratic behavior of the LED, if I comment out all the interrupt and timer code, it works perfectly, but if I put it back in, it's totally erratic. I thought millis() used timer0 and shouldn't conflict with timer1 interrupts... ??
#define L_Turn_In 0
#define R_Turn_In 1
#define Horn_In 2
#define Key 3
#define Brake_In 4
#define Start_In 5
#define Lights_In 6
#define Kill_In 7
#define L_Turn_Out 8
#define R_Turn_Out 9
#define Horn_Out 10
#define Aux_Out 11
#define Brake_Out 12
#define Start_Out 13
#define Hi_Out A0
#define Lo_Out A1
unsigned long L_Millis=0;
unsigned long R_Millis=0;
unsigned long debounce_time = 50;
bool Signal_Status = false;
void setup() {
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
pinMode(L_Turn_In,INPUT);
pinMode(L_Turn_Out,OUTPUT);
}
void loop() {
if (digitalRead(L_Turn_In)==LOW) //if button pressed
{
if (millis()>debounce_time) //if button wasn't pressed in last 50 milliseconds
{
L_Millis = millis(); //reset current millisecond count
Signal_Status = !Signal_Status; //invert signal status
while (digitalRead(L_Turn_In)==LOW) //wait until button goes back high
delay(1);
}
}
digitalWrite(L_Turn_Out,Signal_Status); //output status to LED
}
ISR(Timer1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
TCNT1 = 34286; // preload timer
}
I thought millis() used timer0 and shouldn't conflict with timer1 interrupts
Correct.
But, I had/have these understandings (I could have misconception):
1.delay(); function uses TC0 based on interrupt scheme. 2.millis(); function uses TC1 based on overflow/polling scheme.
3. Demonstration Codes (I could have misconception)
The following program blinks L at 500 ms interval for about 5-sec. 500 ms delay has been implemented using delay() function, and 5-sec period has been implemented using millis() function.
Thanks, I think it was the misspelling that caused the behavior, I don't think I would have caught that! I saw my error on the debouncing too, I must've been pretty tired to miss that