7.1

Dependencies:   mbed

Fork of Oppgave2 by Sølibatgutt

Committer:
andreped
Date:
Tue Oct 17 11:36:30 2017 +0000
Revision:
5:da09c341e484
Parent:
3:cdd8af1b6f39
Child:
6:7c9aad508bf2
Oppgave 7.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andreped 0:fe54cfab4520 1 #include "mbed.h"
andreped 0:fe54cfab4520 2
andreped 5:da09c341e484 3 AnalogIn noise(p19);
andreped 5:da09c341e484 4 DigitalOut Led1(LED1);
andreped 5:da09c341e484 5 DigitalOut Led2(LED2);
andreped 5:da09c341e484 6 DigitalOut Led3(LED3);
andreped 5:da09c341e484 7 DigitalOut Led4(LED4);
andreped 5:da09c341e484 8 DigitalIn bryter(p20);
andreped 5:da09c341e484 9
andreped 5:da09c341e484 10 Timer t1;
andreped 5:da09c341e484 11 BusOut bargraph(p21, p22, p23, p24, p25, p26);
andreped 5:da09c341e484 12 int noiseValue();
andreped 5:da09c341e484 13 int tid;
andreped 5:da09c341e484 14 Timer t2;
andreped 5:da09c341e484 15 int noiseValue(int low, int high);
andreped 0:fe54cfab4520 16
andreped 1:07b498c93e9a 17 int main()
andreped 1:07b498c93e9a 18 {
andreped 5:da09c341e484 19 srand(noiseValue(1, 1000));
andreped 0:fe54cfab4520 20 while(1) {
andreped 5:da09c341e484 21 int tid = 0;
andreped 5:da09c341e484 22 Led1 = 1;
andreped 5:da09c341e484 23 wait(1);
andreped 5:da09c341e484 24 Led2 = 1;
andreped 5:da09c341e484 25 wait(1);
andreped 5:da09c341e484 26 Led3 = 1;
andreped 5:da09c341e484 27 wait(rand()%10 + 1);
andreped 5:da09c341e484 28 Led4 = 1;
andreped 5:da09c341e484 29 t1.start();
andreped 5:da09c341e484 30
andreped 5:da09c341e484 31 while(bryter == 0) {
andreped 5:da09c341e484 32 wait_ms(1);
andreped 1:07b498c93e9a 33 }
andreped 5:da09c341e484 34
andreped 5:da09c341e484 35 t1.stop();
andreped 5:da09c341e484 36 tid = t1.read_ms();
andreped 5:da09c341e484 37 bargraph = tid / 10;
andreped 0:fe54cfab4520 38 }
andreped 0:fe54cfab4520 39 }
andreped 5:da09c341e484 40
andreped 5:da09c341e484 41
andreped 5:da09c341e484 42
andreped 5:da09c341e484 43 int noiseValue(int low, int high)
andreped 5:da09c341e484 44 {
andreped 5:da09c341e484 45 uint16_t reading, rnd;
andreped 5:da09c341e484 46 reading = noise.read_u16() & 0xFFF; // keep 12 lsb
andreped 5:da09c341e484 47 reading *= 13;
andreped 5:da09c341e484 48 rnd = reading % (high - low + 1); // find remainder
andreped 5:da09c341e484 49 rnd += low;
andreped 5:da09c341e484 50 return rnd;
andreped 5:da09c341e484 51 }
andreped 5:da09c341e484 52