Serial connection as option for code

Hello all,

I am trying to use the serial port as a switch between 2 code options but is not working as I would like to...
The idea is, if supplied using USB connected to a PC it sould activate the serial and perform some code and if the arduino is just supplied with a battery it should do some other tasks.
Here is an example code of what I would like to do.

void setup() {
    Serial.begin(57600);
}

void loop() {
  
  while (Serial.available() == true)
  {
    Serial.println("parameter");
  }

  Serial.println("logger");
}

Thanks for your help
Tim

An Arduino is not aware that it is connected to a USB port unless some data is sent over the USB connection,

You could add some extra components so that, when connected to a battery, the battery causes one of the I/O pins to be pulled HIGH and your code in setup() could check for that.

...R

Hello Robin2,

That I was trying to avoid...
But have decided to use 2 digital, D8, as a fake gnd and D9 as tester with a jumper.

void setup() {
  pinMode(8, OUTPUT);//fake gnd
  digitalWrite(8, LOW);
  pinMode(9, INPUT_PULLUP);
}

void loop() {
    if (digitalRead(9) == LOW){
      config();
    }
    else{
      logger();
    }
}

Maibe some one want to use this also.

Tim