TVZ Mechatronics Team


Zagreb University of Applied Sciences, Professional Study in Mechatronics

Timers interrupts and tasks

In the following exercises you will learn to use Timer, Timeout, Ticker and InterruptIn classes from the standard mbed library. The program codes are from the following excellent book: Toulson, R. & Wilmshurst, T. (2016). Fast and Effective Embedded Systems Design - Applying the ARM mbed (2nd edition), Newnes, Oxford, ISBN: 978-0-08-100880-5.

Using the mbed Timer

Study the API documentation of the Timer class.

Examine the following code and try to figure out what will happen with the LEDs. Then test the program on an actual mbed and see if your predictions were correct.

#include "mbed.h"
Timer timer_fast;
Timer timer_slow;
DigitalOut ledA(LED1);
DigitalOut ledB(LED4);

void task_fast(void);
void task_slow(void);

int main() {
  timer_fast.start();
  timer_slow.start(); 
  while(true){
    if (timer_fast.read() > 0.2) {
      task_fast();
      timer_fast.reset();
    }
    if (timer_slow.read() > 1) {
      task_slow();
      timer_slow.reset();
    }
  }
}

void task_fast(void) {
  ledA = !ledA;
}
void task_slow(void) {
  ledB = !ledB;
}

Using the mbed Timeout

Study the API documentation of the Timeout class.

Examine the following code and try to figure out what will happen with the LEDs. Then carefully test the program on the mbed and see if your predictions were correct.

#include "mbed.h"
Timeout response;
DigitalIn button (p14);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

void blink() {
  led2 = 1;
  wait(0.5);
  led2 = 0;
}

int main() {
  while(true) {
    if(button == 1){
      response.attach(&blink, 3.0);
      led3=1;
    } else {
      led3=0;
    }
    led1=!led1;     
    wait(0.2);
  }
}

Modify the above code by adding the second button, which will detach the blink() function from the response object if pressed.

Using the mbed Ticker

Study the API documentation of the Ticker class.

Examine the following code and try to figure out what will happen with the LED. Test the program on the mbed and see if your predictions were correct.

#include "mbed.h"
void led_switch(void);
Ticker time_up;
DigitalOut myled(LED1);

void led_switch() {
    myled=!myled;       
}

int main(){
    time_up.attach(&led_switch, 0.2);
    while(true) {
        wait(1);
    }
}

Modify the above code to flash another LED every 1 second.

Using the mbed InterruptIn

Study the API documentation of the InterruptIn class.

Examine the following code and try to figure out what will happen with the LEDs. Test the program on the mbed and see if your predictions were correct.

#include "mbed.h"
InterruptIn button(p14);
DigitalOut led(LED1);       
DigitalOut flash(LED4);

void isr1() {
  led = !led;
}

int main() {
  button.rise(&isr1);                              
  while(true) {
    flash = !flash;
    wait(0.2);
  }
}

Press the button multiple times and carefully observe the behavior of the LED1 when the button is pressed. Did you remember the bouncing effect?

Switch debouncing

Modify the program from Exercise 4 to remove the bouncing effect, e.g. to debounce the button. Possible solution is given in the following code:

#include "mbed.h"
InterruptIn button(p14);
DigitalOut led(LED1);       
DigitalOut flash(LED4);
Timer debounce;

void isr1() {
  if (debounce.read_ms() > 200) {
    led = !led;
    debounce.reset();
  }
}

int main() {
  debounce.start();
  button.rise(&isr1);                              
  while(true) {
    flash = !flash;
    wait(0.2);
  }
}

Also read about a DebounceIn and PinDetect classes, which are written for the purpose of switch debouncing. A very nice tutorial about pushbutton and switches can be found here.

Measuring distance using ultrasonic sensor HC-SR04

Write the simple program for measuring distance using an ultrasonic sensor HC-SR04.

Congratulations!

You have completed all the exercises in the Timers interrupts and tasks topic.

Return to TVZ Mechatronics Team Homepage.


All wikipages