Last active
March 22, 2023 23:07
-
-
Save danielkwood/86644e5db8c7d083ca630c2ef67ead3a to your computer and use it in GitHub Desktop.
Arduino - receive string commands via Bluetooth
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 <SoftwareSerial.h> | |
int led = 2; | |
SoftwareSerial Bluetooth(7,8); // RX, TX | |
void setup() { | |
pinMode(led, OUTPUT); | |
Serial.begin(9600); | |
Bluetooth.begin(9600); | |
while (!Bluetooth) { | |
; // wait for Bluetooth to connect | |
} | |
Bluetooth.println("Bluetooth connected"); | |
} | |
void loop() { // run over and over | |
String command; | |
if (Bluetooth.available()) { | |
// Read command from Bluetooth... | |
command = Bluetooth.readStringUntil('\n'); | |
delay(10); | |
// Display the command in the serial monitor | |
Serial.println(command); | |
if (command.equals("on")) { | |
Serial.println("LED turned on"); | |
digitalWrite(led, HIGH); | |
} | |
else if (command.equals("off")) { | |
Serial.println("LED turned off"); | |
digitalWrite(led, LOW); | |
} | |
} | |
if (Serial.available()) { | |
// You can also send back data to Bluetooth module from the Serial Monitor... | |
Bluetooth.write(Serial.read()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment