Last active
March 22, 2023 06:10
-
-
Save CodeNextAdmin/0828f7f1e3626474b7b1dd025ac40a16 to your computer and use it in GitHub Desktop.
Turning on LED with BT HC-06
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
/* Code Next Arduino Robotics club | |
* Turn on LED via BT example | |
* Pair a device via Bluetooth to HC-06 | |
* Use a Bluetooth Terminal app to send characters | |
*/ | |
#include <SoftwareSerial.h> | |
SoftwareSerial BTserial(10, 11); // RX | TX | |
// Connect the HC-06 TX to the Arduino RX on pin 10. | |
// Connect the HC-06 RX to the Arduino TX on pin 11 | |
int led = 13; | |
void setup() | |
{ | |
Serial.begin(9600); | |
// HC-06 default serial speed is 9600 | |
BTserial.begin(9600); | |
pinMode(led, OUTPUT); | |
digitalWrite(led, HIGH); | |
} | |
void loop() | |
{ | |
// Keep reading from HC-06 and send to Arduino Serial Monitor | |
while (BTserial.available()>0) | |
{ | |
//this code runs if connecting via BT on another device | |
//Serial.write(BTserial.read()); | |
char c = BTserial.read(); | |
if (c == 'a'){ | |
digitalWrite(led, HIGH); | |
//delay(1000); | |
} else if (c == 'b'){ | |
digitalWrite(led, LOW); | |
}else { | |
Serial.println("Enter a or b"); | |
} | |
delay(10); | |
} | |
// Keep reading from Arduino Serial Monitor and send to HC-06 | |
while (Serial.available()>0) | |
{ | |
//This runs while the communication is coming from the Serial USB | |
BTserial.write(Serial.read()); | |
} | |
delay(10); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment