IAN LANG ELECTRONICS

The timing capabilities of your Arduino can be used to make a device that times in uniform steps. In this example, we make an interval timer that can do from 10 seconds to five minutes using only two buttons and 10 LEDs. The timer increments in steps of ten seconds, and once you have set the device to the time you want (using one button) you then start it counting down by pressing the other button.

To the schematic then:

Using your Arduino to Make an Interval Timer

Go Back

The code for this circuit is as follows:

 

/////// Countdown Timer ////////

///////// circuit ard02 /////////

//http://ianlangelectronic.webeden.co.uk/

 

int secarray []={2,3,4,5,6};

int minled []={1,2,3,4,5};

int secled []= {10,20,30,40,50};

int minarray []={8,9,10,11,12};/////set arrays for led on/off

int mincount;

int seccount;

int j;

int gostate =0;///set four variables

void setup(){

Serial.begin (9600);//open for computer

}

 

void loop () {

if (gostate==0){//go button not pressed

  if (digitalRead (A5)==HIGH){

   switcher ();} //call setting function

   if (digitalRead(A0)==HIGH){

     go ();}}//call first count

  else {//go button not pressed

    go();}//call subsequent counts

 }

 

 

void switcher (){

  seccount=seccount+10; //increment by 10 seconds

  delay(300);//pause for visible delay

  if (seccount==60){//move seconds back to 0 and minute plus 1

    seccount=0;

    mincount = mincount+1;}

    Serial.print (mincount);

    Serial.print ("  minutes     ");

    Serial.print (seccount);

    Serial.println ();

    if (mincount==5){//move from high parameter to low

      if  (seccount==10){

        mincount=0;

        seccount=0;}}

    for (j=0;j<5;j++){//light second indicators

      if (seccount >= secled [j]){

        digitalWrite (secarray [j],HIGH);}

        else {

    digitalWrite (secarray [j],LOW);}}

   for (j=0;j<5;j++){//light minute indicators

      if (mincount >= minled [j]){

        digitalWrite (minarray [j],HIGH);}

        else {

    digitalWrite (minarray [j],LOW);}

  }

}

 

  void go (){

    gostate=1;//lock

   

   

    if (seccount==0){//reset seconds and decrement 1 minute

      seccount=60;

      mincount=(mincount-1);}

      seccount=(seccount-1);

           Serial.print (mincount);

    Serial.print ("  minutes     ");

    Serial.print (seccount);

    Serial.println ();

    for (j=0;j<5;j++){

      if (seccount>=secled [j]){

        digitalWrite (secarray[j],HIGH);}//check second indicators and light

        else{

          digitalWrite (secarray[j],LOW);}//check second indicators and do not light

    }

    for (j=0;j<5;j++){

      if (mincount>=minled [j]){

        digitalWrite (minarray[j],HIGH);}//check minute indicators and light

        else{

          digitalWrite (minarray[j],LOW);}//check minute indicators and light

    }

   

    if (mincount <0){//check if end of cycle is reached

    gostate =0;

  seccount=0;

mincount=0;

           Serial.print (mincount);

    Serial.print ("  minutes     ");

    Serial.print (seccount);

    Serial.println ();

      tone(7,250,3000);

      for (j=0;j<5;j++){

              digitalWrite (secarray[j],LOW);}

}

 delay (990); //delay for one second:accounting for processing time

  }

 

Set your pushbuttons up like this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The code also has serial printing to the IDE window, if you like you can open it and watch what happens as you press the button.

Set the timer for a minute and forty seconds, which means the first red LED is on and so are the first four green ones. Now press the button you have attached to pin A0. If you have the IDE window open, you will see it counting down in minutes and seconds, and as the count falls, the green LEDs will go out, and after 1 minute the red one will extinguish and all the greens will light. Time it against a stopwatch. It is quite accurate.

Once the countdown reaches 0 minutes and 0 seconds, the buzzer will sound, and the device will reset itself ready for input. Try it with different time values.

 

Over the page we will look at the code chunk by chunk and discuss how it works.

5V

GND

Pin 11

This keeps your pins reliably at default of low, and so all commands are ignored until a button is pressed. Once you have built the circuit and uploaded the above code, begin by pressing the button you have attached to A5 briefly. The first green LED should light up. This signifies that you have set the timer for ten seconds. Press it again, and the next LED should light up. Once all five green LEDs have lit up, signifying a time of fifty seconds, if you press again the green LEDs should go off and the first red LED should come on. This indicates a time of one minute. Press again, and the first green LED comes on, indicating a minute and ten seconds; it carries on in the same manner until five minutes are indicated. If you carry on, it reverts first back to zero seconds and then on the next press to 10.

More >

The large coloured numbers on the left indicate the pins on your Arduino board to which you should attach components.

GND