Hi, I have this project comprised of a node with 2 sensors connected to a main unit wirelessly using bluetooth. I tried to implement sleeping the arduino and wait for a sensor reading to wake it up. Everything worked great until I introduced the bluetooth module. It keeps getting into sleep and then exiting right away. Thanks in advance.
Here is my code:
#include <avr/sleep.h>
#include <avr/power.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(6, 7); // RX | TX
int pin2 = 2;
int pin3 = 3;
void pin2Interrupt(void)
{
}
void pin3Interrupt (void)
{
}
void enterSleep(void)
{
attachInterrupt(0, pin2Interrupt, LOW);
attachInterrupt(1, pin3Interrupt, HIGH);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
sleep_disable();
detachInterrupt(0);
}
void setup()
{
Serial.begin(9600);
pinMode(pin2, INPUT);
pinMode(pin3, INPUT);
ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC);
Serial.println("Initialisation complete.");
BTserial.begin(9600);
pinMode(12, OUTPUT); //used only to power my sensors//
digitalWrite(12, HIGH);
pinMode(11,OUTPUT);
digitalWrite(11, HIGH);
pinMode(10,OUTPUT);
digitalWrite(10,LOW);
pinMode(9,OUTPUT);
digitalWrite(9,LOW);
}
int seconds=0;
void loop()
{
int ss = digitalRead(2);
int sm = digitalRead(3);
int s1,st;
if (ss == 1)
{
s1 = 0;
}
else
{
s1 = 1;
}
st = s1+sm;
if (st>0)
{
Serial.println("Trigger ALARM");
BTserial.write("2");
}
else
{
Serial.println("ALL CLEAR");
BTserial.write("3");
seconds++;
}
delay(1000);
if(seconds == 2 && st == 0)
{
Serial.println("Entering sleep");
delay(200);
seconds = 0;
BTserial.print("S");
enterSleep();
}
}