Unexpected Characters with println

Hi,

Got a problem with unwanted characters in the seriell monitor before serial.println.
Project contains an Arduino Nano (clone with CHU430 USB chip, MAC Sierra and IDE 1.8.4.
Worte a script that gets the RX/TX led to blink and switches in-between back to serial function and goes to sleep after 5 blinks.
The code works. The first time after uploading you get before the message “Start” one or two random characters. After reset there are no unwanted characters. Works all the time this way, even unplugging the Nano or restarting the IDE. There is approximately half a second between the characters and “Start”.

How can I get the 2 unwanted characters not to appear?

Regards
Sven

Add a short delay in setup before the first print; I have no idea why it exactly works, but it has always solved it for me.

You might benefit from posting your sketch (and not in .png format). Be sure to use code tags (read the sticky post at the top of the forums about how to use this forum). It could be something simple in your code.

Hi,

Adding the delay did not change it.

Please find code attached.

Sven

My code:
int ledPin0 = 0, count = 0, ledPin1 = 1, sleepMS; boolean running = true;
#include <avr/sleep.h>

void setup() {
WriteUSB(“Start”);
SetOutput();
}

void SetOutput() {
pinMode(ledPin0,OUTPUT);
pinMode(ledPin1,OUTPUT);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin0, HIGH);
}

void WriteUSB(String a){
Serial.begin(57600);
delay(100);
while(!Serial){ }
delay(50);
Serial.println(a);
Serial.end();
delay(50);
}

void loop() {
digitalWrite(ledPin1, HIGH);
delay(600);
digitalWrite(ledPin1, LOW);
delay(600);
if (count >=5){
WriteUSB(String (count) + " - Sleeptime: " + String (sleepMS));
WriteUSB(“End”);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin0, HIGH);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu ();
}
++count;
WriteUSB(String(count));
}

sketch_sep24av1.2.ino (843 Bytes)

It might not solve it because you keep on using serial.begin and serial.end.

For that code, it might have to be done after each serial.begin.

Why do you use serial.begin and serial.end all the time? Usually one sets up the serial port once in setup and does not use serial.end.

Hi sterretje,

The delay is after serial.begin. I want to try code that uses the D0 and D1 pins which are used for TX/RX.

Therefor I switch between serial mode and pin output mode back and for. The unwanted character appears only after uploading the sketch once. After repeating the script with the reset button, no unwanted characters appear.

Regards
Sven

Show your updated code
2)
Do not use pins 0 and 1 for anything else if you use it for serial communication.