Go Down

Topic: On waking up and interrupts (Read 80 times) previous topic - next topic

Aeien

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...

Code: [Select]
#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!

el_supremo

Code: [Select]
attachInterrupt(1, wakeup, LOW);
That will cause interrupts while the pin is LOW, which can be quite a long time.
Try this instead:
Code: [Select]
attachInterrupt(1, wakeup, FALLING);

Pete
Don't send me technical questions via Private Message.

Aeien

Awesome, that did the trick, thanks!

Interestingly enough, it seems to be receiving random BT input now, causing it to progress without me telling it to.
Is this due to my phone and the BT receiver communicating every now and then by default, or is it caused by the app I use on my phone sending unnecessary data?


thanks again!

el_supremo

There's a problem with the variables that are accessed in the interrupt routine. They should be declared volatile.
Code: [Select]
volatile int number = 1;    //will increase every time BT input is received
volatile int flag = 0;
volatile unsigned long timerset = 0;


Pete
Don't send me technical questions via Private Message.

Aeien

Thanks, Pete!
But unfortunately, that doesn't seem to make it behave differently. But as for now, I think I can work around it.


Another question, though: is it possible to use the same BT input to wake the arduino up and perform a specific command, depending on what the BT input was (so processing it's content)?

I (naively) tried to connect the BT TXD pin to both the interrupt pin (3) to wake up and the TX pin on the arduino to process the input normally, but this doesn't seem to work.

Any ideas?  Again, your help is highly appreciated :)

el_supremo

The BT TXD pin should be connected to the Arduino RX pin. Connecting BT TXD to the interrupt pin will generate a new interrupt on every FALLING bit transition.
I've never used BT so I can't help much with that but you can read the BT message and act upon it as long as you figure out a way to ignore the extra interrupts.

Pete
Don't send me technical questions via Private Message.

Aeien

Great, thanks Pete,  you are a great help :)

Go Up