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
# Task 1: Modifing Code to use Functions
Consider the following code
~~~c
DigitalOut led(LED1);
int main() {
//Flash LED 5 times
for (int x=0; x<5; x++){
led != led;
wait(0.5)
}
//Flash LED 10 times
for (int x=0; x<5; x++){
led != led;
wait(0.5)
}
~~~
First lets break this into a function.
This would let us reuse the code in different places in the program.
The questions we need to ask:
- What **chunks** of code are reused
- What **variables** are in the reused code?
- What **parameters** would we want to pass to the function?
The Reused code is the part in the loop
~~~c
for (int x=0; x<5; x++){
led != led;
wait(0.5)
}
~~~
In this example, the number of times we blink the LED is a good candidate for a parameter
## Function Definitions
Take for form **Return Type** **Funtion Name** (**parameters**)
- **Return Type** we dont need to return anything, so *void* is good
- **Function Name** Something sensible that descibes the functionaly
- **Parameters** Number of times to **repeat** (an integer)
~~~c
void flash(int repeat){
// Function Body goes here
}
~~~
Therefore our function code ends up as
~~~
void flash(int repeat){
for (int x=0; x< repeat; x++){
led != led;
wait(0.5);
}
~~~
## Calling the Function
We can now call the function by using its name and supplying any parameters.
~~~c
int main() {
//Flash LED 5 times
flash(5);
//Flash LED 10 times
flash(10);
}
~~~
## Your Task
- Extend the function to take the delay time as a parameter.
# Task 2: Getting Classy
To turn the function into a class we need to consider the attributes and functions.
I this case we have a couple of potential pataeters
- **pin** electronic 'pin' the LED is connected to
- **delay** how long to wait betwen toggling modes.
There are also candidates for functions
- **flash** Actually do the blinking.
## Implementing the class
Standard CPP class definition, note the use of initialisation list for the pin as it is an object.
~~~ c
#include "mbed.h"
class Flasher{
private:
float delay;
DigitalOut pin;
public:
//Constructor
Flasher(PinName thePin, float delayTime) : pin(thePin) {
delay = delayTime;
}
void flash(int times){
for (int x = 0; x< times; x++){
pin= !pin;
wait(delay);
}
}
};
int main() {
//Setup
Flasher ledOne = Flasher(LED1, 0.5);
Flasher ledTwo = Flasher(LED2, 0.25);
//Loop
while(1){
ledOne.flash(5);
ledTwo.flash(10);
}
}
~~~
## Task
- Modify the Flasher class to take account of Multiple Lights.
- Add functions for the State to the Flasher Class
- If we have time, implement the button press to change state.
> NOTE: Each set of lights will work independently. We need some form
> of multi tasking for both sets of lights to work together. We will
> be looking at Concurrency over the next couple of days.