Go Down

Topic: Sometimes Atmega does not wake up (Read 790 times) previous topic - next topic

ale8484

Hallo

I have an Atmega328, with 16MHz crystal oscillator, 5V stable supply. I want Atmega wakes up when something happens (one of the two sleep pins goes low), but the problem is: after about 1 hour of normal running, Atmega does not wake up anymore. 5V supply is ok, oscillator too.. but no wake up. If I reset it, everything goes well. This is the code:

#include <avr/sleep.h>
#include <avr/power.h>
#include <XBee.h>

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

uint8_t payload[] = {
  0, 0 };

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000FFFF);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int pin2 = 2; //Interrupt Pin from Motion Detector
int pin3 = 3; //Interrupt Pin from UART (Antenna)
int pin4 = 4; //ON-OFF Pin for 3.3V
int pin8 = 8; //ON-OFF Pin for Motion Detector
int pin9 = 9; //ON-OFF LEDS
int batterypin = A0;
int batteryvoltage=0;
int tmax=0;
int ledpin=13;
boolean flag=0;
boolean mdstatus=0;
String stat="IDLE";
unsigned long timer=0;
unsigned long alarmstart=0;





/***************************************************
*  Name:        pin2Interrupt
*
*  Returns:     Nothing.
*
*  Parameters:  None.
*
*  Description: Service routine for pin2 interrupt 
*
***************************************************/
void pin2Interrupt(void)
{
  /* This will bring us back from sleep. */

  /* We detach the interrupt to stop it from
   * continuously firing while the interrupt pin
   * is low.
   */
  detachInterrupt(0);
  detachInterrupt(1);
}


/***************************************************
*  Name:        enterSleep
*
*  Returns:     Nothing.
*
*  Parameters:  None.
*
*  Description: Enters the arduino into sleep mode.
*
***************************************************/
void enterSleep(void)
{

  /* Setup pin2 as an interrupt and attach handler. */
  attachInterrupt(0, pin2Interrupt, LOW);
  attachInterrupt(1, pin2Interrupt, LOW);

  delay(100);

  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();
}


/***************************************************
*  Name:        setup
*
*  Returns:     Nothing.
*
*  Parameters:  None.
*
*  Description: Setup for the Arduino.           
*
***************************************************/
void setup()
{
  //  Serial.begin(9600);
  xbee.begin(9600);

  /* Setup the pin direction. */
  pinMode(pin2, INPUT);
  digitalWrite(pin2, HIGH);

  pinMode(pin3, INPUT);
  digitalWrite(pin3, HIGH);

  pinMode(pin4, OUTPUT);
  digitalWrite(pin4, LOW);

  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, LOW);

  pinMode(pin9, OUTPUT);
  digitalWrite(pin9, HIGH); //Schmitt Trigger will set to LOW

  payload[0] = 0;
  payload[1] = 1;

  Serial.println("Initialisation complete.");
}



/***************************************************
*  Name:        loop
*
*  Returns:     Nothing.
*
*  Parameters:  None.
*
*  Description: Main application loop.
*
***************************************************/
int seconds=0;
void loop()
{
  //check Motion Detector Status
  mdstatus=digitalRead(pin2);

  //check Battery status
  batteryvoltage=analogRead(batterypin);
  //  Serial.println(stat);
  if((batteryvoltage>=0))
  {
    if(stat=="OK")
    {
      flag=0;
    }
    else
    {
      stat="OK";
      flag=1;
    }
  }


  if((batteryvoltage<0))
  {
    stat="LV";
    // Turn Off Antenna and Led
    digitalWrite(pin4, HIGH);
    // Turn Off Sensor
    digitalWrite(pin8, LOW);
    delay(60000);
    flag=1;
  }

  if(stat=="OK")
  {
    if(flag)
    {   
      // Turn on antenna and sensor
      //     Serial.println("Turn on Antenna and Sensor");
      //     Serial.println("Wait for Sensor Init...");
      // Turn ON Antenna and Led
      digitalWrite(pin4, LOW);
      // Turn ON Sensor
      digitalWrite(pin8, HIGH);
      // Wait Initialization
      // delay(120000);
    }

    //    Serial.println("Check if alarm from sensor");
    delay(1000);

    if(!mdstatus)
    {
      xbee.send(zbTx);   
      //      Serial.println("Send RF Data");
    }

    //    Serial.println("Reset and Start Timer");

    timer=millis();
    alarmstart=millis();
    do
    { 
      tmax=15000;

      digitalWrite(pin9, HIGH); //if there is Schmitt Trigger will set HIGH and turn on LED
      digitalWrite(ledpin, HIGH); //Cancellare
      delay(1000);

      //  Serial.println("Check New Alarm from Motion Detector");
      mdstatus=digitalRead(pin2);
      //  Serial.println((millis()-timer),DEC);
      if(!mdstatus)
      {
        if((millis()-alarmstart)>5000)
        {
          alarmstart=millis();
          timer=millis();
          xbee.send(zbTx);
          //     Serial.println("Send RF Data");
        }

      }
      else
      { 
        xbee.readPacket();
        if (xbee.getResponse().isAvailable())
        {
          alarmstart=millis();
          timer=millis();

        }
        //     Serial.println("Check UART alarm");
      }
      // statement block
    }
    while ((millis()-timer)<tmax);

    // Serial.println("Enter Sleep mode...");
      digitalWrite(pin9, LOW); //if there is Schmitt Trigger will set HIGH and turn on LED
      digitalWrite(ledpin, LOW); //Cancellare
    enterSleep();
  }



}




Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy