Led serial help

Hello, while I am quarantined, I want to take this time to get better at programming.
I have a sketch below where i am trying to blink the onboard LED on my Nano and have this info be printed to the serial monitor where it will say LED: on, LED: off at the same time my onboard led is blinking.
Right now normally the when i upload and power the board nothing happens.
But when i open the serial monitor and click send it will say LED: 13 and repeat this message every half second and the led would blink only when i clicked send on the serial monitor.

Any suggestions thanks

int ledPin= 13;
int delayPeriod = 500;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}
void loop()
{
  if (Serial.available() > 0) {
    
   Serial.print("LED: ");
    Serial.println(ledPin, DEC);
  
  digitalWrite(ledPin,HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
  }}

This

Serial.println(ledPin, DEC);

is printing the pin number rather than the state of the LED.

With this code

digitalWrite(ledPin,HIGH);

you are changing the state but not recording it.

Try it like this

  if (ledState == LOW) {
     ledState = HIGH;
  }
  else {
    ledState  = LOW;
  }
  digitalWrite(ledPin,ledState);
  Serial.println(ledState);
  delay(delayPeriod);

...R

if (Serial.available() > 0)

All of the code after this will only run if something is available, ie only if you enter something into the Serial monitor and press return

Take out that line and the associated opening and closing braces round its dependant code block

Note too that this

   Serial.println(ledPin, DEC);

prints the pin number, not the state of that pin. You can read the state of the pin using digitalRead() but at that point it will always be LOW because of what you do at the end of loop()

To do what you say you want you need to print a message after each time you write to the pin.. You never need to read its state as you already know it.

Robin2:
This

Serial.println(ledPin, DEC);

is printing the pin number rather than the state of the LED.

With this code

digitalWrite(ledPin,HIGH);

you are changing the state but not recording it.

Try it like this

  if (ledState == LOW) {

ledState = HIGH;
 }
 else {
   ledState  = LOW;
 }
 digitalWrite(ledPin,ledState);
 Serial.println(ledState);
 delay(delayPeriod);




...R

Thank your for the response, i implemented the code and it works but it says"1" and "0"
How do i get it to say "high" and "low"

int ledPin= 13;
int delayPeriod = 500;
int ledState;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}
void loop()
{
if (ledState == LOW) {
     ledState = HIGH;
  }
  else {
    ledState  = LOW;
  }
  digitalWrite(ledPin,ledState);
  Serial.println(ledState);
  delay(delayPeriod);
 
  }

How do i get it to say "high" and "low"

The long, but easy to understand option is to test the value (0 or 1) and print the appropriate text depending on the value

There are cleverer ways but I don't think that you are ready for them yet. Get the simple version working first

if (this is true) //test for 0 or 1
{
  print "on" if true
}
else
{
  print "off"
}

Over to you to turn it into real code

int ledPin = 13;
int delayPeriod = 1000;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if (Serial.available() == 0)
  {
    Serial.println("LED: On");
    // Serial.println(ledPin, DEC);
    digitalWrite(ledPin, HIGH);
    delay(delayPeriod);
    //-------------------------
    Serial.println("LED: Off");
    digitalWrite(ledPin, LOW);
    delay(delayPeriod);
  }
}