0
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Just a shot in the dark, but do you need to test and make sure all of the character is transmitted before telling the processor to sleep?

\$\endgroup\$
  • \$\begingroup\$ The system will work like this,If motion is detected, uC wakes up, sends a GET request via cc3000(ToDo implement WDT during this),gets a response 'ok' and goes back to sleep, if not it will retry, if its fails couple of times it'll go back to sleep again. \$\endgroup\$ – Hemal Chevli Sep 15 '13 at 4:08
  • 1
    \$\begingroup\$ None of that describes the Serial.println(pirState) function call. The problem could be that the three lines following are executed and the processor put to sleep long before the character is finished transmitting. Try adding delay(100) immediately after the Serial.println() to confirm. \$\endgroup\$ – Jeff Sep 15 '13 at 21:09
  • \$\begingroup\$ That's good to hear. What I would do now is remove the two delay() calls and add a test for the transmit complete flag(TXC0). delay(100) is a delay so long that I knew the 9600 baud character would be transmitted. \$\endgroup\$ – Jeff Sep 16 '13 at 18:36
  • \$\begingroup\$ That is a great idea, adding a delay was a bit crude way of resolving the issue, I'll definitely add it, I'll be adding WDT next, lets see how it goes, thanks for you help Jeff. :) \$\endgroup\$ – Hemal Chevli Sep 17 '13 at 5:02
  • \$\begingroup\$ Right, I just suggested the delay to confirm my suspicions. Glad to assist. \$\endgroup\$ – Jeff Sep 17 '13 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.