[SOLVED] What is the correct format for this instruction?

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.

/* Serial Loop */

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

SoftwareSerial mySerial(rxPin, txPin); // RX, TX
char myChar ;

void setup() {
  Serial.begin(9600);   
  Serial.println("AT");

  mySerial.begin(38400);
  mySerial.println("AT");
}

void loop() {
  while (mySerial.available()) {
    myChar = mySerial.read();
    Serial.print(myChar);//phone to computer
    if (myChar = (led_on))
      {
        digitalWrite (LED_BUILTIN, HIGH);
      }
    else if (myChar = (led_off))
      {
        digitalWrite (LED_BUILTIN, LOW);
      }
  }

  while (Serial.available()) {
    myChar = Serial.read();
    Serial.print(myChar); //echo
    mySerial.print(myChar);//computer to phone
  }
}

without this section 4 lines into the loop

 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.

= is for assignment, use == for comparison

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

OK let me try that ....

no if I change the top few lines to

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?

Try:

if (myChar == 'x')

and

else if (myChar == 'l')

because character literals need single quotes. Without the quotes, x and l are treated as variable names.

Observation

If you want to exercise multiple outputs for what you're doing, you could use 'A' to set input1, 'a' to reset it, and so on with b, c, d, etc.

YMMV

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.

Not going any further until you provide a complete sketch, not just loop().

Thanks

see post 1

@matelot Have you omitted

pinMode(LED_BUILTIN, OUTPUT);

from setup()? It's not shown in the sketch in post #1. Without it, digitalWrite(LED_BUILTIN, HIGH/LOW) won't control the LED.

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.

/* Serial Loop */

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9


SoftwareSerial mySerial(rxPin, txPin); // RX, TX
char myChar ;

void setup() {
  Serial.begin(9600);   
  Serial.println("AT");
  pinMode(LED_BUILTIN, OUTPUT);

  mySerial.begin(38400);
  mySerial.println("AT");
}

void loop() {
  while (mySerial.available()) {
    myChar = mySerial.read();
    Serial.print(myChar);//phone to computer
    if (myChar == ('led_on'))
      {
        digitalWrite (LED_BUILTIN, HIGH);
      }
    else if (myChar == 'led_off')
      {
        digitalWrite (LED_BUILTIN, LOW);
      }
  }

  while (Serial.available()) {
    myChar = Serial.read();
    Serial.print(myChar); //echo
    mySerial.print(myChar);//computer to phone
  }
}

Why are you back to 'led_on'? myChar is a char, so please first try with a single character, e.g. 'x', as @J-M-L mentioned.

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'

That's why I was asking. Works with it.

nope

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').

are you sure? I type a word and press send , it sends the word.

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.

Yes

ok well as I said above I have altered it to 'x' and 'l' and it still fails