Lab 3: Tommy Maly and Corwin Stites / Mbed 2 deprecated lab2programb

Dependencies:   mbed

Committer:
tmaly45
Date:
Wed Sep 26 00:41:15 2018 +0000
Revision:
1:0a1e7d18b69c
Parent:
0:09571003fc2e
Updated 9-25

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tmaly45 1:0a1e7d18b69c 1 //Program B
tmaly45 1:0a1e7d18b69c 2 // This program randomly turns on a new LED every second.
tmaly45 1:0a1e7d18b69c 3 // Corey Stites & Tommy Maly
tmaly45 1:0a1e7d18b69c 4
tmaly45 1:0a1e7d18b69c 5
tmaly45 1:0a1e7d18b69c 6
tmaly45 0:09571003fc2e 7 #include "mbed.h"
tmaly45 0:09571003fc2e 8 #include "time.h"
tmaly45 0:09571003fc2e 9
tmaly45 1:0a1e7d18b69c 10 DigitalOut myled[5] = {p25, p26, p27, p28, p29}; // Sets an array of Digital Outputs that fall in line with the LEDs on the MBED.
tmaly45 0:09571003fc2e 11
tmaly45 0:09571003fc2e 12
tmaly45 0:09571003fc2e 13
tmaly45 0:09571003fc2e 14 int main() {
tmaly45 0:09571003fc2e 15
tmaly45 1:0a1e7d18b69c 16 int i = 0; // initializes the integer i
tmaly45 0:09571003fc2e 17
tmaly45 0:09571003fc2e 18 char enter_key;
tmaly45 0:09571003fc2e 19
tmaly45 0:09571003fc2e 20 Timer t;
tmaly45 1:0a1e7d18b69c 21 t.start(); // The previous 3 lines were just to ensure randomness every time the program is ran.
tmaly45 0:09571003fc2e 22
tmaly45 0:09571003fc2e 23 printf("Ready?\n\r");
tmaly45 0:09571003fc2e 24 scanf("%c", &enter_key);
tmaly45 1:0a1e7d18b69c 25 srand(t.read_ms()); // Again, used to ensure randomness. Also prompts the user to start the light show.
tmaly45 0:09571003fc2e 26
tmaly45 1:0a1e7d18b69c 27 while(1) { // Starts the never-ending while-loop.
tmaly45 1:0a1e7d18b69c 28 i = (rand()%4); // Sets i to be equal to a random integer between 0 and 4, the components for the 'myled' array.
tmaly45 1:0a1e7d18b69c 29 myled[i] = 1; // Turns on the LED associated with the random integer generated in the step before.
tmaly45 1:0a1e7d18b69c 30 wait(1); // Keeps it on for one second.
tmaly45 1:0a1e7d18b69c 31 myled[i] = 0; // Turns off the LED associated with the random integer generated.
tmaly45 1:0a1e7d18b69c 32 printf("%d\n\r", i); // Prints the integer created, in case you wanted to know.
tmaly45 1:0a1e7d18b69c 33 } //This while loop will continue to generate a new integer 'i' and the part of the 'myled' array associated with that 'i' will turn on and off.
tmaly45 0:09571003fc2e 34
tmaly45 0:09571003fc2e 35
tmaly45 0:09571003fc2e 36 }