I'm trying to program an Arduino MKR1000 and I am using the millis()
function to emulate a traditional timer. The problem is that any call to millis()
inside the debounceTimer()
function returns the same value. I only added the Serial.print
to debug.
I left out a decent bit of the code to simplify your reading of it. It works by waiting for an interrupt on pin 4 which then calls the runTimers()
function and that calls the debounceTimer()
function and within that the millis()
function is called. The debouncetimer()
function keeps getting called until the 10 ms has elapsed and then should end the timer. The problem is that the millis()
function stops returning a value that goes up.
#define doorIntPin 4
unsigned char sysEnabled = 0;
unsigned long debounceTimerPlace =0;
unsigned char debounceTimerOn =0;
void setup() {
Serial.begin(9600);
attachInterrupt(doorIntPin, doorOpens, LOW);
interrupts();
}
void loop() {
}
void runTimers(){
Serial.print('r');
while(alarmTimerOn || debounceTimerOn || enablingTimerOn){
if(alarmTimerOn)
alarmTimer();
if(enablingTimerOn)
enablingTimer();
if(debounceTimerOn)
debounceTimer();
}
}
void debounceTimer(){
unsigned long time = millis()- debounceTimerPlace;
Serial.println(millis() & 0xFFFF, DEC);
if(sysEnabled && time> 10){
debounceTimerOn=0;
}
}
void doorOpens() {
Serial.print('o');
if(!debounceTimerOn){
debounceTimerPlace = millis();
debounceTimerOn=1;
}
runTimers();
}
I suspect that in the background an interrupt is being called than increments the value millis()
returns and somehow my external interrupt is disabling that. I don't know why this is happening or how to fix it.
Any ideas?