I've made a arduino clone which has a PIR sensor, I intend to run it on 2AA, I'm using LT1302 boost converter, because the PIR sensor works at 5V, if motion is detected the message will be sent to my website using cc3000 breakout board from adafruit. All is working, but now I'm adding the sleep code block to save power, for testing purpose I've written the following code.
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
//PIR sensor,led and disconnect button
#define PIR 2 //INT 0 PIN 4
#define DISCONNECT 19//A5 pulled up, not used for now
#define LED 16 //A2 high on
volatile int pirState = LOW; // we start, assuming no motion detected
volatile char sleep;
int val = 0; // variable for reading the pin status
void static inline pwrDown(void);
void setup() {
ADCSRA = 0; //Disable ADC
// turn off various modules
//PRR =0x81; //0b10000001; TWI and ADC off
// 7 1 0
//TWI uart ADC
pinMode(LED, OUTPUT); // declare LED as output
pinMode(PIR, INPUT); // declare sensor as input
Serial.begin(9600);
attachInterrupt(0, motion, RISING);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep = 1;
// cli();
// pwrDown();
}
void loop(){
delay(500);
Serial.println(pirState);
digitalWrite(LED,pirState);
cli();
pwrDown();
}
//ISR for INT0
void motion() {
pirState = !pirState;
}
void static inline pwrDown(void)
{
//PRR =0xFF; //turn off all modules
MCUCR = MCUCR | bit(BODSE) | bit(BODS); //timed sequence
MCUCR = MCUCR & ~bit(BODSE) | bit(BODS);//to turn off BOD in sleep
sei(); //enable int
sleep_mode(); //sleeping set SE got sleep and clear SE when waking up
//sleep_enable(); //slee_mode() does all three.
//sleep_cpu();
//sleep_disable();
// PRR =0x81; //0b10000011; turn off all except uart and spi
// sleep = 0;
}
This working, the LED is lighting on and off as its supposed to, the loop is not running continuously means the AVR is going in power down mode , but for Serial.println I'm getting garbage value. eg j for 1 and Lc for 0