Dear people,
In learning about home automation, I'm trying to write a sketch to wake my arduino up when it receives bluetooth input to turn my RF-sockets on/off and stay awake for 10 seconds for more commands.
To asses the feasability of this, I wrote a sketch that prints a number once every second for 10 seconds, then goes to sleep. When any BT input is received on pin 3, an interupt is called which increases the number and sets the timer for 10 seconds of printing.
wiring: BT module (TXD) connected to pin 3, that's all...
#include <avr/sleep.h>
int number = 1; //will increase every time BT input is received
int flag = 0;
unsigned long timerset = 0;
void setup() {
Serial.begin(9600); // Set baud rate
pinMode(3, INPUT);
digitalWrite(3, HIGH);
Serial.println("Initialised");
attachInterrupt(1, wakeup, LOW); // Interupt for wake up
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Lowest power mode
}
void loop() {
Serial.println(number);
flag = 0;
delay(1000);
if (millis()-timerset > 10000) {
sleep_enable();
sleep_mode();
}
}
void wakeup() {
sleep_disable();
if (flag == 0) {
number = 1 + number;
}
flag = 1;
timerset = millis();
}
Initializing goes well, it prints "1" 10 times, then goes to sleep.
Upon input is behaves unpredictable: sometimes as it's supposed to by increasing the number and printing for 10s. But most of the time, it only prints the previous number (no increase) only once; this for several times input, then suddenly again an increase and 10s print.
Can anyone tell me why it behaves like this?
Any other tips on using BT as wake-up are highly appreciated. I read through a lot of posts on the forum already, but can't seem to find the answer.
Thanks alot!