Making a Timer With Arduino

Introduction: Making a Timer With Arduino

I feel like in my phone, one of my most used features is my timer feature. I use it to set limits when I'm studying, cooking, keep my breaks on track and for so many other places. That's why I thought it would be a great project to start off with. Since a timer would be too simple, I added some sound and some lights to let your other senses get a little alarm as well.

Step 1: Getting Your Materials

The materials needed for this project are:

  • Wire
  • a 10k Potentiometer
  • Red LEDs x9
  • Arduino UNO
  • 560ohms Resister x9
  • Piezo Element
  • Breadboard

Step 2: Setting Up the Potentiometer

Hardware:

A potentiometer is a voltage divider. That is like a knob where you can control the amount of resistance in. The potentiometer has three pins that we connected to the breadboard. We connect wires from the breadboard to the points they need to go to. One of the wires goes to the GND(ground) and another one goes to the 5V. The one in the middle is connected to one of the analog pins. We connect it to analog instead of digital because it is giving information to the board.

Software:

val = analogRead(pot);

This is setting up the information coming out of the potentiometer into a variable we can use later.

Step 3: Setting Up the Piezo Elements

Hardware:

One pin of the piezo element is connected to GND and then the other one is connected to the board as a pin. Fortunately, this was one of the easier ones set up in the hardware, unlike the LEDs.

Software:

int piezo = 11;

As always, we have to make a variable for the pin number.

pinMode(piezo,HIGH)

After that, we have to connect the piezo element to the board using pinMode and that is all the set up for the piezo elements.

Step 4: Setting Up the LEDs

Hardware:

You connect the LEDs to the resistors that are connected to the pins. One of the LED pins does to the Arduino board pins and the other one will be connected to the resistor. There are 8 so you would repeat this 8 times. Since the LEDs aren't connected to the breadboard, you can organize them however you like.

Software:

int ledPins[] = {2,3,4,5,6,7,8,9,10};


We created an array to hold the pin names. This makes it easier for us so we don't have to individually set up each.

for(int i = 0; i < 9; i++)
  {        
      pinMode(ledPins[i],OUTPUT); 
  }  

We made a for loop to go through the list and assign each name to a pin. This is a lot easier than writing a code for every single one.

Step 5: The Software Explained Step-By-Step

void timeChooser(){
  if (val > 0 && val < 204){
    min = 0.5;
  }
  else if(val > 205 && val < 409){
    min = 1;
  }
  else if(val > 410 && val < 615){
    min = 3;
  }
  else if(val > 614 && val < 819){
    min = 5;
  }
  else if(val > 820 && val < 1023){
    min = 10;
  }
}

I made a lot of functions outside of the "void loop()" to make it neater and to help explain it better. This function aligns the potentiometer value with the number of minutes to set the timer. Depending on where you turn the knob, a different amount of voltage will come out. The Arduino can read the output and depend on what is it, we are setting up a different amount of time. For example, if the sensor value is 700, it is a value between 614 and 819 setting the timer to 5 minutes.

void optionPrinter(){
  Serial.print("time chosen = ");
  Serial.println(min);
}

After we figured out how many minutes are set, we print it in the serial monitor to let the user know how many minutes were chosen.

void timer(){
  for(min = min*60;min==0;min--){
    delay(1000);
    Serial.print(min);
    Serial.println('secs');
  }
}<br>


After we figure out how many minutes are chosen, we make a function to start the countdown. We multiply the number of minutes with 60 to turn it into a second form and then every second, subtract one from the "min" variable. When it reaches 0, we have a function that plays the song and the lights.

if (min = 0.00){
      //first bar
      tone(piezo,330);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,349);
      delay(250);<br>

The beginning of this code says, if the variable min is equal to 0, play the song and turn on the light pattern. To play the actual song, I made another function and in it, I used the tone() function. Here is how the tone() function works:

tone(pin, frequency, duration); // the duration is optional

Instead of using the duration, I used the delay function which is something a lot of people do. I repeated this for the entire ode to joy song(link to song).

for (int i=0; i < 4; i++){
        digitalWrite(ledPins[0], HIGH); 
        delay(500);
        digitalWrite(ledPins[0], LOW);<br>

To turn on the light pattern, I used the very simple digitalWrite() function. This function takes the pin number and just gives that pin power. I gave some LEDs power sometimes and no power at some time to create many different patterns that would be repeated using the for loop. The for loop made sure that the code would repeat 3 times.

All of these different functions are compiled in the void loop() in the order they were explained.

Step 6: The Full Software

Here is the full software:

//potentiometer setup
int pot = 0;
int val;
float min = 1;

//LED setup
int ledPins[] = {2,3,4,5,6,7,8,9,10};

//piezo elements setup
int piezo = 11;


//arduino setup
void setup() {
  Serial.begin(1000);
  for(int i = 0; i < 9; i++)
  {        
      pinMode(ledPins[i],OUTPUT); 
  }  
  pinMode(piezo,HIGH);
}


void loop() {
  //puts how much the potientometer is outputting in a variable
  val = analogRead(pot);

  //making that choose the time
  timeChooser();
//prints your timer option
  optionPrinter();
//prints how much time is left
  timer();
//plays ode to joy
  odeToJoy();
              
}

//making a function to print your chosen time
void optionPrinter(){
  Serial.print("time chosen = ");
  Serial.println(min);
}

/*alligns the potentiometer value with the 
number of mins to set the timer to */
void timeChooser(){
  if (val > 0 && val < 204){
    min = 0.5;
  }
  else if(val > 205 && val < 409){
    min = 1;
  }
  else if(val > 410 && val < 615){
    min = 3;
  }
  else if(val > 614 && val < 819){
    min = 5;
  }
  else if(val > 820 && val < 1023){
    min = 10;
  }
}

//setting up the actual timer countdown
void timer(){
  for(min = min*60;min==0;min--){
    delay(1000);
    Serial.print(min);
    Serial.println('secs');
  }
}

//ode to joy
void odeToJoy(){
  //when the timer runs out it will play the song
  //ode to joy 
    if (min = 0.00){
      //first bar
      tone(piezo,330);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,349);
      delay(250);
      tone(piezo,392);
      delay(250);
      tone(piezo,392);
      delay(250);
      tone(piezo,349);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,294);
      delay(500);
      //second bar
      tone(piezo,330);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,349);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,349);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,330);
      delay(250);
      tone(piezo,294);
      delay(250);
      tone(piezo,261);
      delay(250);
      tone(piezo,261);
      delay(500);
      noTone(piezo);
      
      //does a light pattern
      //for loop there to repeat the same
      //pattern 3 times
      for (int i=0; i < 4; i++){

        digitalWrite(ledPins[0], HIGH); 
        delay(500);
        digitalWrite(ledPins[0], LOW);
        
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[0], HIGH); 
        delay(500); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[0], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH); 
		delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH); 
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        digitalWrite(ledPins[8], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[8], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        digitalWrite(ledPins[8], HIGH);
        digitalWrite(ledPins[9], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[8], LOW);
        digitalWrite(ledPins[9], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        digitalWrite(ledPins[8], HIGH);
        digitalWrite(ledPins[9], HIGH);
        digitalWrite(ledPins[10], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[8], LOW);
        digitalWrite(ledPins[9], LOW);
        digitalWrite(ledPins[10], LOW);
        
        digitalWrite(ledPins[0], HIGH); 
        digitalWrite(ledPins[1], HIGH); 
        digitalWrite(ledPins[2], HIGH); 
        digitalWrite(ledPins[3], HIGH); 
        digitalWrite(ledPins[4], HIGH);
        digitalWrite(ledPins[5], HIGH);
        digitalWrite(ledPins[6], HIGH);
        digitalWrite(ledPins[7], HIGH);
        digitalWrite(ledPins[8], HIGH);
        digitalWrite(ledPins[9], HIGH);
        digitalWrite(ledPins[10], HIGH);
        delay(500);
        digitalWrite(ledPins[0], LOW); 
        digitalWrite(ledPins[1], LOW); 
        digitalWrite(ledPins[2], LOW); 
        digitalWrite(ledPins[3], LOW); 
        digitalWrite(ledPins[4], LOW);
        digitalWrite(ledPins[5], LOW);
        digitalWrite(ledPins[6], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[7], LOW);
        digitalWrite(ledPins[8], LOW);
        digitalWrite(ledPins[9], LOW);
        digitalWrite(ledPins[10], LOW);
        //prints the end of the timer
        Serial.print('Timer Over');
      }        
  }
}

Be the First to Share

    Recommendations

    • Fix It Speed Challenge

      Fix It Speed Challenge
    • Anything Goes Contest 2021

      Anything Goes Contest 2021
    • New Year, New Skill Student Design Challenge

      New Year, New Skill Student Design Challenge

    Comments