This is the sketch I got from somewhere to send a character from a serial bluetooth terminal app on my phone via an HC_05 to the serial monitor on my computer and back.
if (myChar = (led_on))
{
digitalWrite (LED_BUILTIN, HIGH);
}
else if (myChar = (led_off))
{
digitalWrite (LED_BUILTIN, LOW);
}
it works fine if I just put a character on the monitor or the app, it shows up on both units.
If I press one of the buttons on the phone app (there are 6 on the screen) button 1 sends the characters 'led_on'which shows up on the Serial Monitor, I am trying to get this to just light the led connected to pin 13 on my nano but can't find the correct format (the second button sends 'led_off' so I could add this as well).
The above addition to the programme fails to verify with 'led_on was not declared in this scope' .
What is the correct format for it to recognise button 1's characters and allow me to take action from it?
Thanks.
thanks JML I should have seen that and I have changed it but it still says 'led_on was not declared in this scope' ?
led_on is the character coming from the app?
mychar has read mySerial.read and seen led_on should mychar not now contain led_on ?
You would have to define led_on somewhere, the compiler has to know what you mean…
Do you mean you are sending the text « led_on » ? Then it’s not just one character… you need to receive a string and perform a string comparison…
The easiest way is to change the phone app to only send one char…like X for off and I for on or whatever
void loop() {
while (mySerial.available()) {
myChar = mySerial.read();
Serial.print(myChar);//phone to computer
if (myChar == (x))
{
digitalWrite (LED_BUILTIN, HIGH);
}
else if (myChar == (l))
{
digitalWrite (LED_BUILTIN, LOW);
it still say 'x was not declared in this scope ?
I think it is still treating 'x' as a variable as opposed to an incoming character?
I think I need it to see the contents of myChar?
verify accepts (myChar == ('led_on'))
and (myChar == 'led_on')
it also accepts (myChar == 'x')
but none of them light the led.
I can change the characters sent to either led_on or x and alter the sketch to suit but still the light doesn't come on. If I load the blink sketch into the nano the light blinks so I know the instruction
digitalWrite (LED_BUILTIN, HIGH);
works.
Yes I did omit the pinmode instruction. I have now added it into the setup and tried
if (myChar == ('led_on'))
and
if (myChar == 'led_on')
both compile and load but the light still doesn't come on?
this is the full sketch now.
I type a number of characters on either my phone or the Serial monitor and they show up on both, I assume this is because myChar reads the Serial.read and prints the characters, in this case led_on, i.e. myChar contains 'led_on'.
I have assigned two buttons on the phone app to send 'led_on' and 'led_off' if I press one of them both the phone and the serial monitor show 'led_on' so I am assuming that the char 'myChar' contains 'led_on'. I also have 2 buttons on the app that send 'x' and 'l' as before, if I alter the sketch to
if (myChar == 'x') or if (myChar ==(x)) and press those buttons the led still stays off even though both the monitor and my phone show 'x'
your while statement causes the program to inhale and print the string "led_on" one character at a time. That's all myChar ever contains, one character.
You're also being tripped up, likely, by the presence of some form of line ending, either CR, LF, or CRLF which may be what's in your myChar by the time you look at it
Change your phone, send a single character ('X') or ('x').
Serial.read() reads one character at a time. If the app sends led_on, then myChar will contain 'l', then 'e', then 'd', etc. It never contains the whole text led_on.