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: 'Lab 2: Worksheet'
author: 'Dan Goldsmith'
email: 'aa9863@coventry.ac.uk'
---
# Introduction
For this set of tasks we will build on the *hello world* LED code from the first practical.
Setup a new workspace and add the following code
~~~ c
#include <mbed.h>
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);
int main() {
// put your setup code here, to run once:
while(1) {
ledOne = 1;
ledTwo = 1;
wait(0.5);
ledOne = 0;
ledTwo = 0;
wait(0.5);
}
}
~~~
## Getting Digital input
As well as the on-board LED'S the development board has other sensors
connected to its pins. One of these is the **user button**
Like the LED's the first stage of getting input from the button is to initialise the pin to accept digital input.
For this we can use the digitaIn class in the MBED API.
We create an object called *button*, linked the the **USER_BUTTON** pin, and set up for digital input.
~~~ c
digitalIn button(BUTTON1);
~~~
## Reading from the button
The **button** object now represents the state of the digital pin it is connected to.
We can query *button* to get the state of the pin:
- 0 off
- 1 on
This means we can change the behaviour of our program depending on whether the button is pressed or not.
~~~c
#include <mbed.h>
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);
DigitalIn button(BUTTON1);
int main() {
// put your setup code here, to run once:
while(1) {
//Read the Button State and change behaviour.
if (button){
ledOne = 1;
wait(0.5);
ledOne = 0;
wait(0.5);
}
else{
ledTwo = 1;
wait(0.5);
ledTwo = 0;
wait(0.5);
}
}
~~~
\clearpage
## Adding some state
We have a bit of an issue here. The LED state only changes when the
button is pressed. Lets add some state to keep track of when the
button is pressed and change behaviour accordingly.
~~~ c
#include <mbed.h>
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);
DigitalIn button(USER_BUTTON);
int main() {
// put your setup code here, to run once:
int buttonstate =0;
while(1) {
//Read the Button and change the state
if (button){
buttonstate = !buttonstate;
}
//Read the Button State and change behaviour.
if (buttonstate){
ledOne = 1;
wait(0.5);
ledOne = 0;
wait(0.5);
}
else{
ledTwo = 1;
wait(0.5);
ledTwo = 0;
wait(0.5);
}
}
}
~~~
However we still have a problem here.
The state is recorded, but is only updated each cycle. Therefore
- The button has to be pressed at the time a reading is taken
- If the button is pressed over one cycle, we end up "flipping" between states
## Adding an Interrupt.
Interrupts allow us to trigger an event when something happens in a
system. For example if we link a button press to an interrupt
routine, then each time the button is pressed the code in the function
gets triggered
> NOTE: While interrupts are great in the event loop style systems, we need to consider the code that runs, for example if the interrupt code is blocking, it means we the program could hang and become unresponsive. Hiving n interrupt trigger some state update, which is then handled in another part of the code is preferable.
Examining the API for interrupts
https://os.mbed.com/docs/mbed-os/v5.11/apis/interruptin.html
we can see that an interrupt is triggered on one of two occasions
- Rising. The value moves from *Low* to *High*
- Falling. The value moves from *High* to *Low*
### Registering a pin as an interrupt
Like before, when we set a pin up for digital input, we need to let
the OS know that the pin attached to the button will be used for an
Interrupt.
~~~ c
InterruptIn button(BUTTON1);
~~~
### Creating a handler
Next we function to *catch* the interrupt.
In this case we can move the code that flips the button state to its own function.
~~~ c
void handler(){
buttonState = !buttonState;
}
~~~
### Wiring the Interrupt to the handler function
Finally we need to connect the interrupt to the handler function.
~~~ c
button.rise(&handler);
~~~
\clearpage
## Final Interrupt based Code
~~~ c
#include <mbed.h>
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);
InterruptIn button(BUTTON1);
int buttonState =0;
void handler(){
buttonState = !buttonState;
}
int main() {
//Wire handler function to the Interrupt/
button.rise(&handler);
while(1) {
//Main Loop
//Flash LED based on buttonstate.
if (buttonState){
ledOne = 1;
wait(0.5);
ledOne = 0;
wait(0.5);
}
else
{
ledTwo = 1;
wait(0.5);
ledTwo = 0;
wait(0.5);
}
}
}
~~~
\clearpage
# Task: Traffic Light Program
## Design Brief
You have been asked to develop an embedded driver for a pedestrian
crossing traffic light system. You can develop the code either in the
simulator or for the embedded board.
> NOTE: Unfortunately our board only has 4 LED's available (rather than
> the 5 we need), In this case we can use the following traffic light
> sequence. IF you are creating the program in the simulator, we can
> add extra LEDS, to create the full sequence
| State | Driver | Pedestrian |
|--------------------------|-----------|-------------|
| Default | Green | Red |
| Button Pressed (Waiting) | Green | Red |
| Wait for stop | Red | Red |
| Crossing | Red | Green |
| End Crossing | Flash Red | Flash Green |
## Design Guidence
We need:
- Something to keep track of state
- Function for each state
- Function for Button press
- Method for state to evolve.
## Hardware
- 4 LEDS.
- LED1,3 (The Vertical ones) Driver
- LED2,4 Pedestrian
- BUTTON1 Crossing button.
\clearpage
## Code to get you started.
~~~ c
#include <mbed.h>
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut driverGreen(LED3);
DigitalOut pedRed(LED2);
int lightSequence = 0; //Default sequence
int main(){
while(1){
//If we are in the Default state.
if (lightSequence == 0){
driverGreen = 1;
pedRed = 0;
}
}
wait(1.0);
}
~~~