Hi guys, anyone knows how to code for the following requirement?
My code below only able to on and off. Always waiting for new input from Bluetooth. Anyway to continue the loop if there is no input as the value remains as 1?
- If there is no input from Bluetooth, continue the while loop
- If there is input from Bluetooth, grab the value and check for condition
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth (3, 4); //Setting 3 and 4 as TX & RX
char Incoming_value;
char New_Incoming_value;
void setup()
{
Serial.begin(9600);
Bluetooth.begin(9600);
if (Bluetooth.available() > 0) {
Incoming_value = Bluetooth.read();
}
}
void loop() {
readBluetooth();
if (Incoming_value == '1') {
onMode();
}
if (Incoming_value == '0') {
offMode();
}
}
void readBluetooth() {
while (Bluetooth.available() > 0) {
New_Incoming_value = Bluetooth.read();
if (New_Incoming_value != Incoming_value) {
if (Incoming_value == '1') {
Incoming_value = 0;
}
else {
Incoming_value = 1;
}
}
}
Incoming_value = New_Incoming_value;
}
void onMode() {
while (Incoming_value == '1') {
Serial.print("ON")
readBluetooth();
}
}
void offMode() {
Serial.println("OFF");
}