Arduino Programming: Cycle timer

Sometimes you want an operation to repeat periodically. Say you are building a parts washer that circulates cleaning fluid around the dirty parts. The cleaning cycle might run for an hour and in that time you want the circulation pump to run for 10 seconds, stop for 5 seconds for particles to settle, then run for 10 seconds and repeat for an hour.

We need a timer. The type of timer that does this is called a Cycle Timer because it repeats a specific timing cycle and it’s pretty easy to build a cycle timer with an Arduino and a little bit of software programming. We’ll need an Arduino (any kind, from any manufacturer will work), a power supply, the power driver circuit, and the “load” which in this case is our pump.

Let’s get started.

// Which pin to use to control the load const int OUTPUT_PIN = 1; 
// Total number of cycles 
const int NUMBER_OF_CYCLES = 10; 
// On time per cycle in milliseconds 
const int CYCLE_TIME_ON = 500; 
// Off time per cycle in milliseconds 
const int CYCLE_TIME_OFF = 200; 

void setup() 
{
 pinMode(OUTPUT_PIN, OUTPUT);
 digitalWrite(OUTPUT_PIN, LOW);
} 

// Run the timer 
void loop() 
{
 int cycles = NUMBER_OF_CYCLES;
 while(cycles-- > 0)
 {
    // Turned timed output on
   digitalWrite(OUTPUT_PIN, HIGH);
   delay(CYCLE_TIME_ON);
   // Turn timed output off
   digitalWrite(OUTPUT_PIN, LOW);
   delay(CYCLE_TIME_OFF);
 }
 // Hold forever
 while(1);
}