Last active
February 6, 2024 12:57
-
-
Save Lif3line/1c2029fb561d77958b15 to your computer and use it in GitHub Desktop.
Arduino code to wake up upon interrupt from a bluetooth module and toggle a pin each time (here controlling a relay hence relayPin).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/sleep.h> | |
#include <SoftwareSerial.h> | |
SoftwareSerial blueSerial(3, 9); // RX, TX pins | |
int relayActive = false; | |
int relayPin = 7; | |
void setup() { | |
blueSerial.begin(9600); // Set baud rate | |
pinMode(relayPin, OUTPUT); | |
digitalWrite(relayPin, HIGH); | |
attachInterrupt(1, toggleRelay, RISING); // Interupt for wake up | |
blueSerial.print("Initialised\n\r"); | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Lowest power mode | |
sleep_enable(); | |
sleep_mode(); // Go to sleep | |
} | |
void loop() { | |
sleep_mode(); | |
} | |
void toggleRelay() { | |
blueSerial.print("Woah much intteruption!\n\r"); | |
if (!relayActive) { | |
digitalWrite(relayPin, LOW); | |
} else { | |
digitalWrite(relayPin, HIGH); | |
} | |
relayActive = !relayActive; | |
} |
So should this command be used:
"attachInterrupt(digitalPinToInterrupt(RX_PIN)"
instead of ;
"attachInterrupt(1,
also even though the bluetooth connection is established, ("Initialised\n\r") message is not coming to the application screen I use on the phone.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No worries, I had this running with either a HC05 or HC06 module, so that should be fine. I think you'll need tx-rx pins for the serial then the mapping to a digital pin to attach the interrupt to. If software serial already does that then you should be set.