Hi! I'm working on an Arduino remote based on an Arduino Nano, a NRF24L01 and some buttons. The buttons are hooked up to GND and the first button to A2, second to A3 and so forth for A4, A5 as well as Digital pin 2 as this pin is used to wake the Arduino from sleep (interrupt). The thing is that I would like all the buttons to be able to wake the remote from sleep so that I can decrease the sleep timer and as such decrease the power usage (the entire thing runs on a 9v battery). As I have it now one of the button wakes the remote and the others provide the "functionality" e.g sending commands. Is this possible to do using either hardware or software (or both)?
Here is my code if you would like to look at it:
#include <avr/sleep.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
int wake = HIGH;
int speeddec = HIGH;
int speedinc = HIGH;
int right = HIGH;
int left = HIGH;
int wakeReading;
int reading;
int reading2;
int reading3;
int reading4;
int inPin = A5; // speeddec
int inPin2 = A4; // speedinc
int inPin3 = A3; // right
int inPin4 = A2; // left
int wakePin = 2; // pin used for waking up
int led = 4;
long time = 0;
long wakeUpTime = 0;
long debounce = 200;
long awakeTime = 120000; // awake for 2 minutes
int ledState = LOW;
unsigned long previousMillis = 0;
const long blinkOnInterval = 250;
const long blinkOffInterval = 20000;
void wakeUpNow() {
// execute code here after wake-up before returning to the loop() function
// timers and code using timers (serial.print and more...) will not work here.
// we don't really need to execute any special functions here, since we
// just want the thing to wake up
}
void setup() {
pinMode(wakePin, INPUT_PULLUP);
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
pinMode(inPin, INPUT_PULLUP);
pinMode(inPin2, INPUT_PULLUP);
pinMode(inPin3, INPUT_PULLUP);
pinMode(inPin4, INPUT_PULLUP);
pinMode(led, OUTPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
Serial.begin(9600);
}
void sleepNow() {
radio.powerDown();
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, LOW); // use interrupt 0 (pin 2) and run function
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep: disable sleep...
wakeUpTime = millis();
detachInterrupt(0); // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.
radio.powerUp();
}
void loop() {
checkButtons();
blinkLed();
delay(50);
if (millis() - wakeUpTime > awakeTime) {
sleepNow(); // sleep function called here
}
}
void blinkLed() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkOnInterval && ledState == HIGH) {
// save the last time you blinked the LED
previousMillis = currentMillis;
ledState = LOW;
digitalWrite(led, ledState);
}
else if(currentMillis - previousMillis >= blinkOffInterval && ledState == LOW) {
// save the last time you blinked the LED
previousMillis = currentMillis;
ledState = HIGH;
digitalWrite(led, ledState);
}
}
void checkButtons() {
wakeReading = digitalRead(wakePin);
reading = digitalRead(inPin);
reading2 = digitalRead(inPin2);
reading3 = digitalRead(inPin3);
reading4 = digitalRead(inPin4);
if (millis() - time > debounce) {
speeddec = reading;
if (speeddec == HIGH)
speeddec = LOW;
else {
speeddec = HIGH;
const char text[] = "SDEC";
radio.write(&text, sizeof(text));
wakeUpTime = millis();
//Serial.println("Sent Sdec");
}
speedinc = reading2;
if (speedinc == HIGH)
speedinc = LOW;
else {
const char text[] = "SINC";
radio.write(&text, sizeof(text));
speedinc = HIGH;
wakeUpTime = millis();
}
right = reading3;
if (right == HIGH)
right = LOW;
else {
const char text[] = "RIGHT";
radio.write(&text, sizeof(text));
right = HIGH;
wakeUpTime = millis();
}
left = reading4;
if (left == HIGH)
left = LOW;
else {
const char text[] = "LEFT";
radio.write(&text, sizeof(text));
left = HIGH;
wakeUpTime = millis();
}
time = millis();
}
}