First time arduino user , basic program to keep adding one (loop)

Hey guys,

First time arduino user. have prior knowledge of C++ programming.

Im just wondering how would i go about writing a program that would keep adding 1 and display the values until; it reaches lets say 20 then starts back up again.

void setup() {
Serial.begin(9600); // use the serial port for printing the number
}

int count = 0; // the number to print

void loop() {
Serial.println(count++); // print and increment the number
delay(1000); // 1 second delay between printing
}

^^i know it would be something like this but i would like it to stop at 20 and start over again

This is part of a much larger project i must do and i need to get the basics down first.

Thanks in advance

Use a [u]for-loop[/u] inside your main loop().

If you don't want to repeat the main loop, you can put a "do nothing" while(1) loop after the count-to-20 for-loop. That way, the execution will be trapped in the while(1) loop and the main loop will never exit & repeat (until you reset the processor).

void loop() {
  Serial.println(count++);  // print and increment the number
  if (count > 20)
    count = 0;
  delay(1000);              // 1 second delay between printing
}

or

void loop() {
  Serial.println(count);  // print and increment the number
  count = (count+1) % 21;  // count 0-20
  delay(1000);              // 1 second delay between printing
}

or

void loop() {
  for (count = 0; count <= 20; count++) {
    Serial.println(count);  // print and increment the number
    delay(1000);              // 1 second delay between printing
  }
}