Hi All,
As the subject line says, I'm trying to use an IR sensor in order to wake up (and actually put to sleep as well) the Arduino UNO on which it is running.
This particular project is simply a motorized door opener that runs on a 12V battery. I'd like to put it in sleep mode in order to conserve as much power as possible, and wake it up on command. The sleep and wake would have to be done with an IR remote control and sensor. The remote control is currently being used to activate the DC motor that opens and closes the door, but now I'm trying to integrate a sleep mode into the code.
I haven't actually run this code yet (I will when I get back from work). But I was hoping for someone to give me a sanity check if possible. Please be gentle...I'm still learning. Would this be the best way of doing it:
#include <avr/sleep.h>
#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "pins_arduino.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
float Direction;
void wakeUpNow() // here the interrupt is handled after wakeup
{
myMotor->setSpeed(100); // rpm is set
}
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(100); // rpm is set
}
void loop()
{
if (irrecv.decode(&results)) {
switch(results.value)
{
case 0xff22dd:
delay(1000);
Direction = FORWARD;
break; // Button 1
case 0xFF02FD:
delay(1000);
Direction = BACKWARD;
break; // Button 2
case 0xFFA25D:
delay(1000);
sleepNow();
break; // Button 3
}
irrecv.resume(); // Receive the next value
}
myMotor->step(2, Direction, DOUBLE);
}
void sleepNow() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function
sleep_mode(); // here the device is actually put to sleep!!
sleep_disable(); // first thing after waking from sleep:
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
}