Sketch only works after serial monitor is activated (SOLVED)

Hi
I have what I deem to be a simple sketch that receives a letter from a pc and turns an LED on or off. the Sketch works perfectly, BUT I must first activate the serial monitor from within the arduino IDE on the PC
I would like to NOT have to do the serial monitor activation step, so that the sketch is automatic.
after i once call the serial monitor and then close it all works fine. Here is the command line from the pc
set /p x="a" \.\COM20
set /p x="b" \.\COM20
and here is the sketch

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

char mychar = 0;

void loop() {
  while (Serial.available()) {
    mychar = Serial.read();
    if (mychar == 'a') {
      digitalWrite(LED_BUILTIN, HIGH);
    }
    //delay(3000);
    if (mychar == 'b')    {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Also I have a 10uf cap from reset to ground on the arduino uno
Any Ideas.....Thanks

All of the loop() code is inside the while (Serial.available()) loop. Nothing will happen until that while is true (their is 1 or more bytes available in the receive buffer).

void loop()
{
   while (Serial.available())
   {
      mychar = Serial.read();
      if (mychar == 'a')
      {
         digitalWrite(LED_BUILTIN, HIGH);
      }
      //delay(3000);
      if (mychar == 'b')
      {
         digitalWrite(LED_BUILTIN, LOW);
      }
   }
}

That's the way i like it. nothing will happen until i once open and then close the serial monitor. after that it works perfectly.

what does automatic mean? some program needs to be connected to the Serial port so that the command can be sent.

Automatic = without first having to open and then close the serial monitor.

but if you don't open the Serial monitor, nothing will happen on the sketch side since it only reacts to Serial commands... The arduino is happily running doing nothing but testing for a byte to arrive on the Serial port.

I can see why you think that is true but test it it lights the led and turns it off as many times as I wish. but only after calling the serial monitor and closing it. this works with the serial monitor closed and the ide closed

I don't understand what it is you can test. The code does nothing until you get a command

How do you close the serial port? How do you send characters when the serial port is closed?

set /p x="a" .\COM20
set /p x="b" .\COM20
I send the commands via batch file one to light the LED and another to turn off the LED a = on b=off

how is the baud rate set to 9600?

Baud rate is 9600 Yes and don't forget the cap between reset and ground

I open and close the serial port via the arduino IDE button upper left
once closed I close the IDE completely

I'm on a Mac, can't test :slight_smile: - have really vague memory of suffering through windows stuff :man_shrugging: :cold_face:

did you issue a
mode COM20 BAUD=9600 PARITY=n DATA=8
before hand?

try with : set /p x="a" <nul >\\.\COM20

I suppose the cap is to prevent rebooting upon opening the Serial connexion

yes the cap is to prevent reset. the other I will try, Im old so it will take some time:)

JML genus. Works perfectly now! Thank YOU.

great news. Enjoy

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.