Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Created October 3, 2013 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataliefreed/6817899 to your computer and use it in GitHub Desktop.
Save nataliefreed/6817899 to your computer and use it in GitHub Desktop.
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