0

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?

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Aug 18, 2016 at 19:54

3 Answers 3

3

You never exit the ISR, hence all interrupts are disabled. Do the minimum work possible within an ISR, and have the main loop outside.

answered Aug 18, 2016 at 20:01
1
  • Thanks. The thing is I'm trying to get the mkr1000 to sleep when not in use. I've tried to execute the wait for interrupt function but i dont see how to. Do you know how i could do this in the Arduino IDE? Commented Aug 18, 2016 at 20:08
0

To answer your question about millis(), it uses interrupts to function, and so you can't call it within an ISR. According to this: https://www.arduino.cc/en/Reference/AttachInterrupt, you should be able to use delayMicrosconds() within an ISR, since it (reportedly) doesn't use interrupts.

answered Oct 22, 2016 at 4:44
0

The problem is that the millis() function stops returning a value that goes up.

That's right. The value returned by millis() is incremented by an ISR which won't be called inside your other ISR.

You can use micros() which will increment. However that will only be good up to approximately 1 ms. The main rule of ISRs is that you don't attempt to do delays inside them.

answered Oct 22, 2016 at 8:25

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.