Hello,
I have a ATMEGA328P-PU on a bread board. It runs the 3.3V 8MHz boot loader for the pro/promini. The sketch below puts the microcontroller in sleep power down mode. It wakes up when it sees HIGH on Interrupt #1, or pin #5 on the microcontroller, and turns on an LED for 1/2 second, then goes back to sleep. This bread board circuit worked wonderfully for 3 weeks, then all of a sudden I get weird behavior. Sometimes it keeps waking itself up when I power it up or when I set off the interrupt. It's behaving like I'm putting 3.3V on the interrupt pin all the time, so the LED keeps blinking every half second, even when the switch is open.
The "switch" in the drawing is me touching the 5V pin to 3.3V rail on the bread board, so it's not a mechanical contact malfuntion. The pins are physically separated, I know there's not 3.3V on the interrupt pin.
What might be going on? I'm not sure if it's a hardware issue or software issue.
#define SERIAL_BAUD 9600 //must be 9600 for GPS, use whatever if no GPS
//analog read
int analogPin = 0;
byte adcsra_save;
float batteryvolt;
//-------------------------
#include <avr/sleep.h>
const byte LED = 8;
void wake ()
{
// must do this as the pin will probably stay low for a while
detachInterrupt (0);
// cancel sleep as a precaution
sleep_disable();
} // end of wake
void setup ()
{
digitalWrite (2, HIGH); // enable pull-up
Serial.begin(SERIAL_BAUD); //Begin serial communcation
adcsra_save = ADCSRA; //save ADCSRA to re-enable later.
Serial.println("starting");
pinMode (LED, OUTPUT);
Serial.println("finished setup");
} // end of setup
void loop ()
{
//turn on LED
digitalWrite (LED, HIGH);
//re-enable analog:
ADCSRA = adcsra_save;
delay(500);
batteryvolt = (analogRead(A0))*3.30/1023.00*2.00;
serial.println(batteryvolt);
digitalWrite (LED, LOW);
// disable ADC
ADCSRA = 0;
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
noInterrupts ();
// will be called when pin D2 goes HIGH
attachInterrupt (1, wake, HIGH);
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts (); // one cycle
sleep_cpu (); // one cycle
} // end of loop