I am trying to turn an LED on and off by sending 1 or 0 (respectively) to the Arduino from Andoird Application via Bluetooth Module HC-06.
I have tried several Arduino codes and Android App Codes from the internet but the same error occurs.
I printed the data to Serial Monitor and it shows -1 (in int) or 255 (in byte) for any input.
I am very new to using the Arduino ( 2 days actually) and don't know how to troubleshoot this. Is it a problem with the Module or the code?
Please help ASAP!
Thanks in advance.
Code:
#include<SoftwareSerial.h>
char incomingByte; // incoming data
int LED = 13; // LED pin
#define Tx 6
#define Rx 5
SoftwareSerial BTSerial(Rx, Tx);
void setup() {
Serial.begin(9600); // initialization
pinMode(LED, OUTPUT);
pinMode(5,INPUT);
pinMode(6, OUTPUT);
Serial.println("Press 1 to LED ON or 0 to LED OFF...");
BTSerial.begin(9600);
}
void loop() {
if (BTSerial.available() > 0) { // if the data came
incomingByte = BTSerial.read(); // read byte
Serial.println(int(incomingByte));
if(incomingByte == '0') {
digitalWrite(LED, LOW); // if 1, switch LED Off
Serial.println("LED OFF. Press 1 to LED ON!"); // print message
}
if(incomingByte == '1') {
digitalWrite(LED, HIGH); // if 0, switch LED on
Serial.println("LED ON. Press 0 to LED OFF!");
}
}
}
Serial Monitor:
Press 1 to LED ON or 0 to LED OFF...
-1
-1
-1
-1
-1
-1
-1
-1
^ Each -1 appears on pressing the On and Off buttons on the Android app
The Android app Sends '1' and '0' for on and off respectively:
btnOn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("1");
Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
}
})