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
#include "mbed.h"
//First We define the Flasher Class
class Flasher{
/* Flasher. A Demonstration Class creating an object that flashes
LEDs.
*/
//Classes can have Public and Private Attributes and Methods.
// - Private are only available within the class.
// - Public are available to all
private:
//Define our Class Attributes
int delay; //How Long to Pause between Flashes
DigitalOut pin; //The Pin our LED is attached to
public:
//Constructor Function, Used to Create the class.
//NOTE: In this case we use an Initialisation List.
Flasher(PinName thePin, int delayTime) : pin(thePin) {
//PinName thePin: The Digital Pin that the LED is attached to.
//This comes from a global Lookup table This is then passed to the
//constructor of the Class Attribute "pin" (of type DigitalOut)
//in ```: pin(thePin)``` of the function definition
delay = delayTime; //Set the Attribute delay, to the value of delay time
}
void flash(int times){
/* Here we define a function available to the class */
for (int x = 0; x< times; x++){
pin= !pin;
wait_ms(delay);
}
}
};
//Our Main Loop, as per all MBED programs.
int main() {
//Setup some Flasher Objects
Flasher ledOne = Flasher(LED1, 500);
Flasher ledTwo = Flasher(LED2, 250);
//Loop
while(1){
ledOne.flash(5);
ledTwo.flash(10);
}
}