Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Introduction

This post shows how to program a timer in a proper way using the Arduino IDE programming language. A timer can be made as increasing or decreasing, to make a countup or a countdown, and we will see both ways. This code can be adapted and applied to any kind of project or idea.


Code Solution

Increasing Timer

This timer is designed to allow to run several tasks at the same time, avoiding common mistakes related to timer creations such as the use of delays, which are a problem because they stop the CPU for a moment and you can't take advantage of this time.

If we take a look at this code, we can see that, first of all, we define the task period, after that, we initialize the serial port in the setup. In the loop, the counter of the task is set to 0 (be sure that, at least, the size of the uint is 16 to provide a good range for the counter), we make a conditional regulated by the condition of the substraction of millis() and lastTime bigger than 1000 ms (1000 ms is the resolution of the timer, you can change it as you want), lastTime is equal to millis(), task counter is increased and, if the task counter has reached the previously settled period, it is restarted and the task is executed. 

The task funtion is only an example to check how many seconds has passed every time a task is executed. This task is supposed to be whatever you want to manage with this timer. As we can see in the example, you can reply this structure for every task needed (in the example code we can see that is made to execute 3 tasks).

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define TASK1PERIOD 2
#define TASK2PERIOD 5
#define TASK3PERIOD 10

void setup() {
  Serial.begin(9600UL);
  Serial.println("CountUp Timer Started");
}  

void loop(){

  static uint32_t task1Counter=0;
  static uint32_t task2Counter=0;
  static uint32_t task3Counter=0;

  static uint32_t lastTime=millis();

  if (millis()-lastTime>1000){
    lastTime=millis();
    ++task1Counter;
    if (task1Counter==TASK1PERIOD){
      task1Counter=0;
      task1();
    }
    ++task2Counter;
    if (task2Counter==TASK2PERIOD){
      task2Counter=0;
      task2();
    }
    ++task3Counter;
    if (task3Counter==TASK3PERIOD){
      task3Counter=0;
      task3();
    }
  }
}

void task1(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task1");
}

void task2(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task2");
}

void task3(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task3");
}
  

Decreasing Timer

This one is the same as the other but making some little changes; the task timer is set to the task period time, the task counter is descreased, the conditional is done when the task counter reaches 0 and the counter resets to task period time.

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define TASK1PERIOD 2
#define TASK2PERIOD 5
#define TASK3PERIOD 10

void setup() {
  Serial.begin(9600UL);
  Serial.println("CountDown Timer Started");
}  

void loop(){

  static uint32_t task1Counter=TASK1PERIOD;
  static uint32_t task2Counter=TASK2PERIOD;
  static uint32_t task3Counter=TASK3PERIOD;

  static uint32_t lastTime=millis();

  if (millis()-lastTime>1000){
    lastTime=millis();
    --task1Counter;
    if (task1Counter==0){
      task1Counter=TASK1PERIOD;
      task1();
    }
    --task2Counter;
    if (task2Counter==0){
      task2Counter=TASK2PERIOD;
      task2();
    }
    --task3Counter;
    if (task3Counter==0){
      task3Counter=TASK3PERIOD;
      task3();
    }
  }
}

void task1(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task1");
}

void task2(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task2");
}

void task3(){
  Serial.print(millis()/1000);
  Serial.print(':');
  Serial.println("task3");
}
  

How to upload this code

If you want to upload this code on your shield you have to follow these steps:
 1.-Select Tools > Board > M-Duino Family (or your working shield).


2.-Select Tools > Industrial Shields > M-Duino 21+ (or your working shield model).3.-Select Tools > Port > COM65 (or the port where you have your device connected).


4.-Upload the program. Click on the arrow located on the left top of the window.

5.- If everything is working right, your timer is ready to be applied to your projects. If you want to check if the program is working right,
click on the Serial Monitor icon located in the right top corner of the window. Make sure that you have configurated the port which you have connected the shield. 

 

6.-Make sure to configurate the baudrate to 9600 to sync up with the serial port. If everything is working right, you will see the name of every task executed on its moment (every period) as you can see in the image below:


Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send