Hi!!! I'm having a problem using sleep mode and timer2 at the same time. I've tested timer2 and it works, and I've tested sleep mode using a blinking led and it works, is sleeping, but when I add the timer2 config:
MsTimer2::set(5000, flash); // 5s period
MsTimer2::start();
the led is always blinking with a 1s period, so Is no sleeping (led fixed)
I don't know what to do. Can anybody help me??
You have reason. The code is easy, just 2 leds, the timer2 make blink the onboard led, and the loop a led on pin 9, so commenting the two lines of timer2 the led 9 is fixed, but with timer2 working the led 9 is blinking… I think the maximum time on timer2 delay is shorter and there’s several interrupts at one second, so interrupts make wake from sleep in the loop and led on pin 9 is blinking. Here is the code:
There’s no errors at the compiling, and a new MsTimer2.o was created.
I don’t understand what is the meaning of this things, but I thank you your efforts.
I don't understand what is the meaning of this things, but I thank you your efforts
It's to eliminate one obvious potential problem. MsTimer2 is coded for a specific set of processors. If you are using a processor that isn't supported, the library still compiles, the Sketch still runs, but the MsTimer2 stuff does not work at all. The bit I had you add ensures you are using a supported processor.
Thank you for your answer. In always, now sleep is not working good using “set_sleep_mode(SLEEP_MODE_IDLE)”, I tested it using sleem mode and pushbutton interrupt and the fast blinking led in the loop is always blinking!!! I changed it to “set_sleep_mode(SLEEP_MODE_PWR_DOWN)” and now the loop led is sleeping till I press the pushbutton:
#include <avr/sleep.h>
int led = 13;
int interled = 9;
int button = 2;
int state = LOW;
int state2 = LOW;
SLEEP_MODE_PWR_DOWN
This sleep mode essentially shuts off everything except the watchdog timer and very basic I/O. None of the timers (including Timer 2) runs in this mode. In addition, the timers are not automatically restarted when the processor wakes from this mode. So even if the processor wakes back up, Timer 2 will not be running.
SLEEP_MODE_IDLE
In this mode the timers do continue running including the timer that drives millis / delay. The processor will only sleep a very short time before an interrupt from one of the timers wakes it back up.
I currently only have a Teensy for testing and MsTimer2 doesn't work with it. So, I won't be able to provide much more help. Hopefully, if this problem continues, someone else will be able to assist.
I'm pretty sure MsTimer2 isn't doing what you think it's doing. What it actually does is hard code timer 2 to overflow and interrupt at 1ms. It is these 1 ms interrupts that wake up sleep mode. MsTimer2 is just telling you when, for example, 1000 interrupts occur (for 1 second).