Help reviewing test code for Xbee S2C radio sleep.

Hi All,

Could you help me to check my code for a simple Xbee sleep test.
I am not sure I fully understand the process of putting the radio to sleep.
Currently with this code the power consumption goes from 130mA to 70mA.

My bug is that when I press the button power goes down to 70mA but back up to 130mA after i released it.
Then if I leave everything alone for a few seconds power overs between 70mA and 110mA.

What I am missing ?? Is the fluctuation because of the Arduino Nano still working, so needing power ?

Is my code reversed to put the Xbee in sleep - I thought the pin needs to be low to trugger sleep and High to wake it.

Thanks.

#include <SoftwareSerial.h>
// RX: Arduino pin 2, XBee pin DOUT.  TX:  Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3); 


// Declare pin numbers for feedback LEDs 1 thru 4
const int LED1_pin = 12;


// Declare pin numbers for bell and light buttons
const int BTN_Light_ON = 7;
int BTN_Light_state = LOW;   // low = not pressed
int wake_pin_state = LOW;
// Declare sleep pin on arduino for DTR (pin 9) pin of Xbee
const int XBee_wake = 5;   // pin number


void setup()
{
  pinMode(LED1_pin, OUTPUT);
  pinMode(XBee_wake, OUTPUT);
  pinMode(BTN_Light_ON, INPUT);

  
// Initialize XBee Software Serial port. Make sure the baud
// rate matches your XBee setting (9600 is default).
  XBee.begin(9600); 
  Serial.begin(9600);
}


void loop()
{
 
  BTN_Light_state = digitalRead(BTN_Light_ON);
  if (BTN_Light_state == HIGH) { 
       digitalWrite(XBee_wake, LOW);            // wake xbee to send
       wake_pin_state = LOW;
       delay(10);                               // pause to give it time to wake up
       XBee.write('L');                         // light btn is pressed
       Serial.println("Xbee is awake, just sent L");
       delay(3000);                              // long delay to allow me to read the monitor
  }     


     // put the XBee to sleep 

 if (wake_pin_state == LOW){
      Serial.println("Wake pin state was low so entered if - about to pause 5 seconds and sleep Xbee");
      digitalWrite(XBee_wake, HIGH);
      delay(5000);
      Serial.println("Xbee should be sleeping");
      wake_pin_state = HIGH;
  }   
}
  pinMode(BTN_Light_ON, INPUT);

How is your switch wired? INPUT_PULLUP makes the wiring so much simpler.

with a 10k resistor, i never tried the input_pullup.

when i build my actual remote control for this i will invert my logic and try pull_up to save space on external components for a tiny remote.

Me again,

I have revised code to include both Xbee radio & Arduino Nano sleep test.

I know I am doing something wrong cause my meter tells me at best I get down to 80mA when I try to sleep everything.

Per Paul's comment above I have now used input pullup for the button.

Code is below as well as basic schematics of connections.
I am using an explorer board for the Xbee, so inputs for VCC, GND, TX, RX are regulated but I am using the 3.3v from the Nano for the DTR pin on the Xbee.

What I am trying to do is simply sleep the Xbee and Nano all the time except when a button is pressed. My thought was to call the sleep routine from the start of the loop, if a button is pressed the loop will execute after the sleep routine. in the sleep routine I setup the interrupt. It gets disabled in the wake up routine.

I have used these sites for reference :

http://www.fiz-ix.com/2012/11/low-power-xbee-sleep-mode-with-arduino-and-pin-hibernation/

My code :

#include <avr/sleep.h>
#include <SoftwareSerial.h>

// RX: Arduino pin 3, XBee pin DOUT.  TX:  Arduino pin 4, XBee pin DIN
SoftwareSerial XBee(3, 4); 


const int LED1_pin = 12;
const int BTN_Light = 2;
const int XBee_wake = 5;    
int BTN_press = LOW;    // low = light is off


void setup()
{
  pinMode(LED1_pin, OUTPUT);
  pinMode(XBee_wake, OUTPUT);
  pinMode(BTN_Light, INPUT_PULLUP);    // logic inverted, up = off
  
// Initialize XBee Software Serial port. Make sure the baud
// rate matches your XBee setting (9600 is default).
  XBee.begin(9600); 
  Serial.begin(9600);
}


void loop()
{
  Serial.println("Just before sleepNow call");
  delay(100);                              // small delay to eliminate garbage in the serial print or let print finish before going to sleep
  sleepNow(); 
  Serial.println("Interrupt fired just after sleepNow, resuming loop");
  BTN_press = digitalRead(BTN_Light);
  if (BTN_press == LOW) {                 // low so button is pressed
       delay(50);                         // delay to make sure Xbee radio is fully awake
       XBee.write('L');                   // light btn is pressed, send message
       digitalWrite(LED1_pin, HIGH);      // turn on LED
       Serial.println("Nano & Xbee awake, just sent L");
       delay(3000);                       // long delay to wait out the bounce and see mAmp variation
       digitalWrite(LED1_pin, LOW);
  }     
}


void sleepNow()         
{
  pinMode(XBee_wake, INPUT);  // going to INPUT makes it work, if not I am at 100mA
  digitalWrite(XBee_wake, HIGH);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  attachInterrupt(0, wakeUp, LOW);
  sleep_mode();
}


void wakeUp()
{
  sleep_disable();
  detachInterrupt(0);
  pinMode(XBee_wake, OUTPUT);
  digitalWrite(XBee_wake, LOW);
}