I'm having an issue with a simple task, and I'm sure I'm just missing something small. The goal is simply to have an LED turn on when the arduino receives a "Z" from the bluetooth HC-05 module paired to my phone.
When I run the sketch and check the serial monitor, I can see that it is successfully getting the "Z" from the phone, but the LED is not turning on.
I double checked the LED connections and I ran the code removing the if statement to turn on the LED, to see if there were any issues there and there weren't, without the if statement before it and just setting the LED pin to HiGH, the LED does turn on.
Here is the code, any help would be appreciated.
#include <SoftwareSerial.h>
SoftwareSerial EEBlue(10, 11); // RX | TX
char val;
void setup()
{
pinMode (6,OUTPUT);
Serial.begin(9600);
EEBlue.begin(9600); //Default Baud for comm, it may be different for your Module.
Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");
}
void loop()
{
// Feed any data from bluetooth to Terminal.
if (EEBlue.available())
Serial.write(EEBlue.read());
// Feed all data from termial to bluetooth
if (Serial.available())
EEBlue.write(Serial.read());
val = Serial.read();
if (val=='Z'){
digitalWrite (6,HIGH);}
}

void loop()
{
// Feed any data from bluetooth to Terminal.
if (EEBlue.available()) // if a byte is available from BT
Serial.write(EEBlue.read()); // read a byte from BT, send to serial monitor
// Feed all data from termial to bluetooth
if (Serial.available()) // if a byte is available from serial monitor
EEBlue.write(Serial.read()); // read a byte from serial monitor and send to BT
val = Serial.read(); // val = byte read from serial monitor, but there is noting there as you already
//read =the byte and sent it to BT
if (val == 'Z') // compare byte from serial monitor to Z
{
digitalWrite (6, HIGH);
}
}
You cannot read back the byte that you sent to serial monitor. Make val = EEBlue.read() and try it.
void loop()
{
if (EEBlue.available()) // if a byte is available from BT
{
val = (EEBlue.read()); // val = byte read from BT
Serial.print(val);
if (val == 'Z') // compare byte from BT to Z
{
digitalWrite (6, HIGH);
}
}
Are you sure that the Z is all that the app sends. There is not line feed or anything?
The serial input basics tutorial may help you to understand reading serial data.
groundFungus:
void loop()
{
// Feed any data from bluetooth to Terminal.
if (EEBlue.available()) // if a byte is available from BT
Serial.write(EEBlue.read()); // read a byte from BT, send to serial monitor
// Feed all data from termial to bluetooth
if (Serial.available()) // if a byte is available from serial monitor
EEBlue.write(Serial.read()); // read a byte from serial monitor and send to BT
val = Serial.read(); // val = byte read from serial monitor, but there is noting there as you already
//read =the byte and sent it to BT
if (val == 'Z') // compare byte from serial monitor to Z
{
digitalWrite (6, HIGH);
}
}
You cannot read back the byte that you sent to serial monitor. Make val = EEBlue.read() and try it.
void loop()
{
if (EEBlue.available()) // if a byte is available from BT
{
val = (EEBlue.read()); // val = byte read from BT
Serial.print(val);
if (val == 'Z') // compare byte from BT to Z
{
digitalWrite (6, HIGH);
}
}
Are you sure that the Z is all that the app sends. There is not line feed or anything?
I made your suggested changes, however, still nothing happens. I attached the Serial Monitor, so you can see that on the Z is being sent, no line feed or anthing else. Here is the edit I made, perhaps I missed something:
val = (EEBlue.read()); // val = byte read from BT
Serial.print(val);
if (val == 'Z') // compare byte from BT to Z
{
digitalWrite (6, HIGH);
}
To confirm that nothing but the Z is sent, try this after the val = EEBlue read line:
Serial.print("[");
Serial.print(val);
Serial.print("]");
What is printed to serial monitor?
Line feeds and carriage returns are invisible in serial monitor so won't show right up.
Interesting, it prints a non-stop picture of a square in the brackets. Any thoughts?
Here is your code modified to my setup. Change soft serial RX and TX pins and LED pin as necessary.
This works on my setup with sending from my tablet, using a Arduino Bluetooth Terminal app, to Uno with HC05
'Z' turns the LED on, 'A' turns the LED off.
#include <SoftwareSerial.h>
SoftwareSerial EEBlue(4, 7); // RX | TX
const byte ledPin = 13; // built in led
char val;
void setup()
{
pinMode (ledPin, OUTPUT);
Serial.begin(9600);
EEBlue.begin(9600); //Default Baud for comm, it may be different for your Module.
Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");
}
void loop()
{
if (EEBlue.available()) // if a byte is available from BT
{
val = (EEBlue.read()); // val = byte read from BT
Serial.print("[");
Serial.print(val);
Serial.println("]");
if (val == 'Z') // compare byte from BT to Z
{
digitalWrite (ledPin, HIGH); // led on
}
if(val == 'A')
{
digitalWrite(ledPin, LOW); // led off
}
}
}
What is printed in serial monitor, now? Can you control the LED?
SUCCESS!! Thank you so much for your help. The Serial Monitor is now just printing out the [Z].