Lab 1 Program B
main.cpp@0:3f29ab39579a, 2015-09-29 (annotated)
- Committer:
- mattsims12
- Date:
- Tue Sep 29 02:57:03 2015 +0000
- Revision:
- 0:3f29ab39579a
Lab 1 Program B
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mattsims12 | 0:3f29ab39579a | 1 | /*Lab 1 Program B |
mattsims12 | 0:3f29ab39579a | 2 | Lights Random LED every second based on a timer's seed |
mattsims12 | 0:3f29ab39579a | 3 | Matthew Sims |
mattsims12 | 0:3f29ab39579a | 4 | Section 1001 |
mattsims12 | 0:3f29ab39579a | 5 | */ |
mattsims12 | 0:3f29ab39579a | 6 | |
mattsims12 | 0:3f29ab39579a | 7 | #include "Timer.h" |
mattsims12 | 0:3f29ab39579a | 8 | #include "mbed.h" |
mattsims12 | 0:3f29ab39579a | 9 | |
mattsims12 | 0:3f29ab39579a | 10 | DigitalIn s1=p16; |
mattsims12 | 0:3f29ab39579a | 11 | DigitalOut out[]= {p26,p27,p28,p29,p30}; //Sets up pins |
mattsims12 | 0:3f29ab39579a | 12 | Timer timer; //Initializes timer object |
mattsims12 | 0:3f29ab39579a | 13 | |
mattsims12 | 0:3f29ab39579a | 14 | int main() |
mattsims12 | 0:3f29ab39579a | 15 | { |
mattsims12 | 0:3f29ab39579a | 16 | |
mattsims12 | 0:3f29ab39579a | 17 | //Timer stopped by user to create a unique number for seed. |
mattsims12 | 0:3f29ab39579a | 18 | timer.start(); //starts timer |
mattsims12 | 0:3f29ab39579a | 19 | printf("Turn switch 1 on"); //prompt user to turn on switch. Only useful is terraterm is on |
mattsims12 | 0:3f29ab39579a | 20 | while(s1!=1) { //waits for user to turn on switch |
mattsims12 | 0:3f29ab39579a | 21 | if(s1==1) { //When switch 1 is on turned on, the timer stops |
mattsims12 | 0:3f29ab39579a | 22 | timer.stop(); //stops the timer |
mattsims12 | 0:3f29ab39579a | 23 | } |
mattsims12 | 0:3f29ab39579a | 24 | } |
mattsims12 | 0:3f29ab39579a | 25 | srand(timer.read_us()); //Sets up rand with a seed based on the timer's value |
mattsims12 | 0:3f29ab39579a | 26 | |
mattsims12 | 0:3f29ab39579a | 27 | int r=-1; |
mattsims12 | 0:3f29ab39579a | 28 | int prev=-1; |
mattsims12 | 0:3f29ab39579a | 29 | while(1) { //Infinite loop |
mattsims12 | 0:3f29ab39579a | 30 | while(r==prev) { //generates randoms until it is different from previously on random |
mattsims12 | 0:3f29ab39579a | 31 | r=rand()%5; //random int 0-4 based on seed |
mattsims12 | 0:3f29ab39579a | 32 | } |
mattsims12 | 0:3f29ab39579a | 33 | out[r]=1; //Turns on corresponding led |
mattsims12 | 0:3f29ab39579a | 34 | wait(1); |
mattsims12 | 0:3f29ab39579a | 35 | out[r]=0; |
mattsims12 | 0:3f29ab39579a | 36 | prev=r; //Wait 1 sec before repeating loop |
mattsims12 | 0:3f29ab39579a | 37 | } |
mattsims12 | 0:3f29ab39579a | 38 | } |