Page 115 - ARM Based Microcontroller Projects Using MBED
P. 115
7.9 PROJECT 6—RANDOM FLASHING LEDs 101
/**********************************************************************
RANDOMLY LIGHTING LEDs
======================
in this project 8 LEDs are connecte dto PORT C of the Nucleo-F411RE
development board. Random numbers are generated between 0 and 255 (all
LEDs OFF or all LEDs ON) and the LEDs then turn ON and OFF by sending
these numbers to PORT C every 250ms. The net effect is that the LEDs
look like Christmas light
Author: Dogan Ibrahim
Date : August 2018
File : Christmas
***********************************************************************/
#include "mbed.h"
BusOut LEDS(PC_0,PC_1,PC_2,PC_3,PC_4,PC_5,PC_6,PC_7);
int main()
{
int Number;
while(1) // Do forever
{
Number = rand() % 256; // GEnerate a random numner
LEDS = Number; // Send the numebr to PORT C
wait(0.25); // 250ms delay
}
}
FIG. 7.36 Program listing.
7.9.6 Program Listing
The program listing is shown in Fig. 7.36 (program: Christmas). At the beginning of the
program BusOut statement is used to group the PORT C lower byte into a variable called
LEDS as in the previous project. An endless loop is created using a while statement. Inside
this loop, random numbers are generated between 0 and 255 using function rand(), and these
numbers are then sent to PORT C every 250ms The net effect is that the Les turn ON and OFF
as Christmas lights.
7.9.7 Modified Program
The program given in Fig. 7.36 can be modified by making the delay between the outputs to
change randomly. This gives the effect of randomly flashing lights with random delays. In the
modified program in Fig. 7.37 (program: Christmas-2) the delay between the outputs changes
randomly between 0 and 300ms.