Go Down

Topic: Serial.print in void setup (Read 81 times) previous topic - next topic

ottooo

Aug 04, 2020, 10:34 am Last Edit: Aug 04, 2020, 10:35 am by ottooo
Hey,
I try to run this easy code example. Opening the serial monitor I can see the "prints" from the void loop, but not the "prints" from the void setup. I don't understand why.

Code: [Select]

float Adata;
float Bdata;
float Cdata;

void setup() {
  Serial.begin(9600); // the bigger number the better
  Serial.println("CLEARDATA"); //clears up any data left from previous projects
  Serial.println("LABEL,Acolumn,Bcolumn,Ccolumn"); //always write LABEL, so excel knows the next things will be the names of the columns (instead of Acolumn you could write Time for instance)
  Serial.println("RESETTIMER"); //resets timer to 0

}

void loop() {

  Adata = 1;
  Bdata = 2;
  Cdata = 3;
  
  Serial.print("DATA,TIME,TIMER,"); //writes the time in the first column A and the time since the measurements started in column B
  Serial.print(Adata);
  Serial.print(Bdata);
  Serial.println(Cdata); //be sure to add println to the last command so it knows to go into the next row on the second run
  delay(1000); //add a delay

}



Thanks!

pert

Which Arduino board are you using?

ottooo

Adafruit Feather M0 Adalogger

pert

OK, so you need to add this after the Serial.begin(9600) line:
Code: [Select]
while (!Serial) {}

The classic Arduino boards like the Uno, Nano, and Mega have a special circuit that causes them to reset when you open the Serial Monitor. This means you will see all the serial output from the moment your sketch starts running.

The native USB boards like your Feather M0 don't have this auto-reset behavior. So what happens is the sketch starts running before you get the Serial Monitor started and all the serial output from before it's open is lost.

That while (!Serial) {} line causes the sketch to wait in an infinite loop until the Serial Monitor is open, so you don't miss anything.

One thing you should note is that you should not add while (!Serial) {} to sketches that you want to run when Serial Monitor is not open, because they will just be stuck in that infinite loop. So you might want to add it while debugging the sketch with Serial Monitor, then comment it out when everything is ready to work without Serial Monitor.

ottooo


pert

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

Go Up