Lab 1 Program B

Dependencies:   mbed

main.cpp

Committer:
mattsims12
Date:
2015-09-29
Revision:
0:3f29ab39579a

File content as of revision 0:3f29ab39579a:

/*Lab 1 Program B
  Lights Random LED every second based on a timer's seed
  Matthew Sims
  Section 1001
*/

#include "Timer.h"
#include "mbed.h"

DigitalIn s1=p16;
DigitalOut out[]= {p26,p27,p28,p29,p30};        //Sets up pins
Timer timer;                                    //Initializes timer object

int main()
{

    //Timer stopped by user to create a unique number for seed.
    timer.start();              //starts timer
    printf("Turn switch 1 on"); //prompt user to turn on switch. Only useful is terraterm is on
    while(s1!=1) {              //waits for user to turn on switch
        if(s1==1) {             //When switch 1 is on turned on, the timer stops
            timer.stop();       //stops the timer
        }
    }
    srand(timer.read_us());     //Sets up rand with a seed based on the timer's value

    int r=-1;
    int prev=-1;                   
    while(1) {                  //Infinite loop
        while(r==prev) {        //generates randoms until it is different from previously on random   
            r=rand()%5;         //random int 0-4 based on seed
        }                           
        out[r]=1;               //Turns on corresponding led
        wait(1);
        out[r]=0;
        prev=r;                //Wait 1 sec before repeating loop
    }
}