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);
}}
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.