Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
title: "Session 3:  Worksheet 2"
author: "Dan Goldsmith"

Brief

For this task we are going to make use of the MBED API, to improve the code we are writing.

Imagine we have been asked to write code to drive a light bar. there are 4 LEDS that flash in a sequentail pattern.

Led 1 Led 2 Led 3 Led 4
Stage 1 X
Stage 2 X
Stage 3 X
Stage 4 X

\clearpage

Initial Code

We could write code that looks like this

#include "mbed.h"

DigitalOut led(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
int main() {
    while (1) {
        //Blink in an offset pattern
        led = 1;
        led2 = 0;
        led3 = 0;
        led4 = 0;
        wait(0.5);
        led = 0;
        led2 = 1;
        led3 = 0;
        led4 = 0;
		wait(0.5);
		led = 0;
        led2 = 0;
        led3 = 1;
        led4 = 0;
		wait(0.5);
		led = 0;
        led2 = 0;
        led3 = 0;
        led4 = 1;
		wait(0.5);
    }
}

Task

However, its not the cleanest design. If we look at the MBED API https://os.mbed.com/docs/mbed-os/v5.11/apis/index.html , it appears the BUS interface is appropriate to use here.

Read the Bus API, and reimplement the code making use of this functionality.