multiple commands Serial.print

I have trouble, with a countdown clock.

There must be a countdown from 60 to 0 seconds.

In the serial monitor there must be a text. "Er zijn nog .. seconden te gaan"

/* Aftellende klok
Terug tellen van 60 - 0
*/
void setup () {
// Stel seriële communicatie in:
Serial.begin(115200);
}
void loop() {
// Print titel op Seriële monitor:
Serial.print(" Er zijn nog: "),("%d"),(" seconden te gaan ");
// Print het aantal seconden sinds de start:
// Serial.print(" ");
// Serial.print("seconden te gaan");
Serial.println((60000 - millis()) / 1000);
// Wacht 1 seconde (= 1000 milliseconden):
delay (1000);
}

What does that code actually do? How is what it does different from what you want?

Read the forum guidelines to see how to properly post code.

int counter = 60;

void setup () 
{
  // Stel seriële communicatie in:
  Serial.begin(115200);
}

void loop() 
{
  // Print titel op Seriële monitor:
  Serial.print(" Er zijn nog: ");
  Serial.print(counter);
  Serial.println(" seconden te gaan ");
  if (--counter < 0) counter = 60;
  delay (1000);
}

A couple of things here:

Serial.print(" Er zijn nog: "),("%d"),(" seconden te gaan ");

The format of this is off, 'print' is a function, and any function takes one set of parameters. Parameters are comma separated inside a single bracket pair.

So, that would become:

Serial.print(" Er zijn nog: ", "%d", " seconden te gaan ");

But that's still wrong... If you look at the reference for 'print', it takes a single argument (or a second that is the formatter): Serial.print() - Arduino Reference
So - what I think you really mean is:

Serial.print(" Er zijn nog: ");
Serial.print("%d");
Serial.print(" seconden te gaan ");

But that will output '"Er zijn nog %d seconden te gaan" (i.e. literally '%d'), which is still not quite what you want.

So, what we need is 'd' as a variable for the seconds, lets just throw something in to hard-code it:

int d = 60;
Serial.print(" Er zijn nog: ");
Serial.print(d);
Serial.print(" seconden te gaan ");

That should print "Er zijn nog 60 seconden te gaan"

Which is a start.

Now we want to decrement 'd' with every loop.

The issue with the current setup is scope (i.e. 'd' only EXISTS within the {} brackets, once the loop ends and starts again, 'd' disappears before being recreated from scratch)
You can do that in 2 ways, and

  • Declare 'd' outside 'loop' and decrement it at the end of every loop (after waiting 1 second).
  • Declare 'd' inside 'loop', but then run your print-code in an inner-loop, which waits 1 second each time.

Lets do the second one:

int d = 60;
while (d >= 0)
{
  Serial.print(" Er zijn nog: ");
  Serial.print(d);
  Serial.print(" seconden te gaan ");
  d = d-1;
  delay(1000);
}

This should count from 60 to 0, then repeat, forever.

Note: This isn't a perfect 1-second timer, but that's out of scope I think.

Thanks for all the help. It works fine now, and I am happy.