arben sutter
Dependencies: mbed
Fork of Oppgave7_1 by
main.cpp@6:7c9aad508bf2, 2017-10-17 (annotated)
- Committer:
- olemh88
- Date:
- Tue Oct 17 11:41:48 2017 +0000
- Revision:
- 6:7c9aad508bf2
- Parent:
- 5:da09c341e484
- Child:
- 7:cd3eaa4ef912
oppgave 7.1
Who changed what in which revision?
User | Revision | Line number | New 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; |
olemh88 | 6:7c9aad508bf2 | 29 | Led1 = 0; |
olemh88 | 6:7c9aad508bf2 | 30 | Led2 = 0; |
olemh88 | 6:7c9aad508bf2 | 31 | Led3 = 0; |
andreped | 5:da09c341e484 | 32 | t1.start(); |
andreped | 5:da09c341e484 | 33 | |
andreped | 5:da09c341e484 | 34 | while(bryter == 0) { |
andreped | 5:da09c341e484 | 35 | wait_ms(1); |
andreped | 1:07b498c93e9a | 36 | } |
andreped | 5:da09c341e484 | 37 | |
andreped | 5:da09c341e484 | 38 | t1.stop(); |
andreped | 5:da09c341e484 | 39 | tid = t1.read_ms(); |
andreped | 5:da09c341e484 | 40 | bargraph = tid / 10; |
andreped | 0:fe54cfab4520 | 41 | } |
andreped | 0:fe54cfab4520 | 42 | } |
andreped | 5:da09c341e484 | 43 | |
andreped | 5:da09c341e484 | 44 | |
andreped | 5:da09c341e484 | 45 | |
andreped | 5:da09c341e484 | 46 | int noiseValue(int low, int high) |
andreped | 5:da09c341e484 | 47 | { |
andreped | 5:da09c341e484 | 48 | uint16_t reading, rnd; |
andreped | 5:da09c341e484 | 49 | reading = noise.read_u16() & 0xFFF; // keep 12 lsb |
andreped | 5:da09c341e484 | 50 | reading *= 13; |
andreped | 5:da09c341e484 | 51 | rnd = reading % (high - low + 1); // find remainder |
andreped | 5:da09c341e484 | 52 | rnd += low; |
andreped | 5:da09c341e484 | 53 | return rnd; |
andreped | 5:da09c341e484 | 54 | } |
andreped | 5:da09c341e484 | 55 |