Hello!
I'd like to hear some tips about this weird problem. I'm using external interrupt, but it only works once. After that it completely stops working. I manually add Vcc to the interrupt pin and it doesn't react at all. I'm using Atmega328 3,8V Vcc @ 16MHz.
I can wake it up with UART though...
I'm using 1K pulldown and interrupt is set after 10 seconds idle to RISING mode.
Code is mostly from the examples
Loop:
if (millis()-sleeptimer>10000 && digitalRead(switchPin)==0)
{
enterSleep();
}
Functions:
void pin2Interrupt(void)
{
detachInterrupt(0);
}
void enterSleep(void)
{
/* Setup pin2 as an interrupt and attach handler. */
attachInterrupt(0, pin2Interrupt, RISING);
delay(100);
//digitalWrite(5,HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
/* First thing to do is disable sleep. */
sleep_disable();
sleeptimer=millis();
//digitalWrite(5,LOW);
}
I'm baffled by this problem, do you have any suggestions what might cause this? I've tried changing the MCU, using bypass capacitors, changing the code for the last 5 hours, but I'm stuck.
The external interrupt stops working. It doesn't wake up the second time. Sorry for being unclear.
That's all of the code that has anything to do with the interrupts. If it isn't there, then it's a hardware problem.
I've about 400 lines of code of keyword processing, serial interfacing with GSM module etc. It's not relevant to this problem, I've disabled all of the rest of the code and and it doesn't affect at all.
Thanks, I've read through that few times. I've about 10 hours spent on this problem, this forum is always the last resort. I can't find the problem myself. If I could, I wouldn't be here.
I set sleeptimer to volatile variable, no effect.
I did discover something, if I I clear my main serial I'm able to get 7 interrupts and then it stops working.
while(Serial.available()) Serial.read(); //clear remaining serial
EDIT: Nope. It was just a fluke. It got back to 1 interrupt again.
Is this a RAM problem? Why does clearing out serial affect this? Why it only can do 7 interrupts, and then stop? In my experience, this kind of erratic behavior is a RAM issue.
Btw. I'm using Software Serial too. I tried clearing it too, but it only worsens the problem back to 1 interrupt. I might be running out of ram?
EDIT: I tried disabling Software serial completely. It still gets stuck. I'm going crazy. This is unbeliveable. It just doesn't wake up.
As I said, I disabled all of the rest of the code.
I thought that more experienced guys could maybe spot a obvious mistake straight from that code.
Well, here is the stripped version and it has the same problem:
I get max 7 interrupts before it stops responding.
#include <avr/sleep.h>
#include <avr/power.h>
//-----------------DECLARE VARIABLES-------------------//
volatile long unsigned sleeptimer;
//-----------------SETUP-------------------//
void setup()
{
//Serial.begin(9600);
pinMode(2,INPUT);
pinMode(4,OUTPUT);
sleeptimer=millis();
}
//-----------------FUNCTIONS-------------------//
void pin2Interrupt(void)
{
detachInterrupt(0);
}
void enterSleep(void)
{
/* Setup pin2 as an interrupt and attach handler. */
attachInterrupt(0, pin2Interrupt, RISING);
delay(100);
digitalWrite(4,HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
/* First thing to do is disable sleep. */
sleep_disable();
digitalWrite(4,LOW);
}
//-----------------MAIN LOOP-------------------//
void loop()
{
if (millis()-sleeptimer>3000) //if webasto not activated after 10sec, go to sleep
{
enterSleep();
sleeptimer=millis();
}
}
The problem has to be in the timed sleeping or those timing variables getting messed up somehow...
The sleep code I got from online here:
EDIT: I tried the exact code from that site. Same problem. It has worked before(with LOW interrupt). I'm going insane.
There has to be a glitch within the RISING interrupt. Has anyone gotten it to work?