Blinky LED for STM32F401 - replacement candidate for application shipped with board - uses <Timer.h> to seed pseudo-random number generator so that the blink sequence varies between incarnations.

Dependencies:   mbed

Committer:
nucleo
Date:
Tue May 20 00:47:38 2014 +0000
Revision:
0:42458802c36f
Blinky LED for STM32F401 - replacement candidate for application shipped with board - uses <Timer.h> to seed pseudo-random number generator so that the blink sequence varies between incarnations.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucleo 0:42458802c36f 1 #include "mbed.h"
nucleo 0:42458802c36f 2 #include "stdlib.h"
nucleo 0:42458802c36f 3 #include <Timer.h>
nucleo 0:42458802c36f 4
nucleo 0:42458802c36f 5 InterruptIn mybutton(PC_13); // B1
nucleo 0:42458802c36f 6
nucleo 0:42458802c36f 7 DigitalOut myled(LED1);
nucleo 0:42458802c36f 8
nucleo 0:42458802c36f 9 double multiplier = 500.0; // maximum on-off time in milliseconds
nucleo 0:42458802c36f 10 Timer timer;
nucleo 0:42458802c36f 11
nucleo 0:42458802c36f 12
nucleo 0:42458802c36f 13 int delay = 500; // initial on-off time in milliseconds
nucleo 0:42458802c36f 14
nucleo 0:42458802c36f 15 void random_on_off() {
nucleo 0:42458802c36f 16 srand(unsigned(timer.read_ms()% RAND_MAX));
nucleo 0:42458802c36f 17 delay = int(multiplier * float(rand()) / RAND_MAX);
nucleo 0:42458802c36f 18 }
nucleo 0:42458802c36f 19
nucleo 0:42458802c36f 20 int main() {
nucleo 0:42458802c36f 21 timer.start();
nucleo 0:42458802c36f 22 while(1) {
nucleo 0:42458802c36f 23 myled = !myled;
nucleo 0:42458802c36f 24 mybutton.fall(&random_on_off);
nucleo 0:42458802c36f 25 wait_ms(delay);
nucleo 0:42458802c36f 26 }
nucleo 0:42458802c36f 27 }