Beating led (a simple example of function call by reference argument)

Dependencies:   mbed

Committer:
jose_23991
Date:
Mon Sep 08 10:42:36 2014 +0000
Revision:
0:f3fc2277e34d
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jose_23991 0:f3fc2277e34d 1 #include "mbed.h"
jose_23991 0:f3fc2277e34d 2
jose_23991 0:f3fc2277e34d 3 void beat(DigitalOut* led, double time); // Prototype of the function beat
jose_23991 0:f3fc2277e34d 4
jose_23991 0:f3fc2277e34d 5 int main()
jose_23991 0:f3fc2277e34d 6 {
jose_23991 0:f3fc2277e34d 7 DigitalOut led(LED1, 0); // Create the LED object and setup OFF
jose_23991 0:f3fc2277e34d 8
jose_23991 0:f3fc2277e34d 9 while(1)
jose_23991 0:f3fc2277e34d 10 {
jose_23991 0:f3fc2277e34d 11 beat(&led, 1); // Beat the LED during 1s
jose_23991 0:f3fc2277e34d 12 }
jose_23991 0:f3fc2277e34d 13 }
jose_23991 0:f3fc2277e34d 14
jose_23991 0:f3fc2277e34d 15 void beat(DigitalOut* led, double time)
jose_23991 0:f3fc2277e34d 16 {
jose_23991 0:f3fc2277e34d 17 double portion = (0.33*time)/3; // Calculate the third portion of the 33% of the time
jose_23991 0:f3fc2277e34d 18
jose_23991 0:f3fc2277e34d 19 *led = 1; // LED ON
jose_23991 0:f3fc2277e34d 20 wait(portion); // Wait a third portion of the 33% of the time
jose_23991 0:f3fc2277e34d 21 *led = 0; // LED OFF
jose_23991 0:f3fc2277e34d 22 wait(portion); // Wait a third portion of the 33% of the time
jose_23991 0:f3fc2277e34d 23 *led = 1; // LED ON
jose_23991 0:f3fc2277e34d 24 wait(portion); // Wait a third portion of the 33% of the time
jose_23991 0:f3fc2277e34d 25 *led = 0; // LED OFF
jose_23991 0:f3fc2277e34d 26 wait(time-(3*portion)); // Wait for the 66% of the time
jose_23991 0:f3fc2277e34d 27 }