Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of using a single counter and the modulo operator (%) to time multiple events.
/*
Natalie Freed, 10-3-2013
Example of using a single counter and the modulo operator (%) to time multiple events.
For LWHS Building and Programming Intelligent Machines Fall 2013.
To use, open Serial Monitor to view output.
*/
int counter = 0;
void setup()
{
//start serial port
Serial.begin(9600);
}
void loop()
{
Serial.println("This line prints every second.");
if(counter % 3 == 0) //if counter is divisible by 3 (% is the remainder operator, called "modulo")
{
Serial.println("This line prints every 3 seconds.");
}
if(counter % 5 == 0) //if counter is divisible by 5 (% is the remainder operator, called "modulo")
{
Serial.println("This line prints every 5 seconds.");
}
delay(1000); //wait 1 second
counter = counter + 1; //increase counter by 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.