Robocon_IPS
/
Nucleo_read_analog_value2
read speed
Fork of Nucleo_read_analog_value by
main.cpp@1:f54097bcb8a0, 2017-06-02 (annotated)
- Committer:
- cc061495
- Date:
- Fri Jun 02 05:19:17 2017 +0000
- Revision:
- 1:f54097bcb8a0
- Parent:
- 0:f3b9844205f2
- Child:
- 2:5c2afc8d7c28
New version(read speed with LCD)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cc061495 | 0:f3b9844205f2 | 1 | #include "mbed.h" |
cc061495 | 1:f54097bcb8a0 | 2 | #include "TextLCD.h" |
cc061495 | 1:f54097bcb8a0 | 3 | |
cc061495 | 1:f54097bcb8a0 | 4 | TextLCD lcd(PC_11,PC_10,PC_0,PC_3,PC_1,PC_2); // RS, E, D4-D7 |
cc061495 | 0:f3b9844205f2 | 5 | AnalogIn analog_value(PA_0); |
cc061495 | 0:f3b9844205f2 | 6 | Timer t; |
cc061495 | 0:f3b9844205f2 | 7 | |
cc061495 | 0:f3b9844205f2 | 8 | int main() { |
cc061495 | 1:f54097bcb8a0 | 9 | double meas, sum, count = 0, range, highscore = 0; |
cc061495 | 1:f54097bcb8a0 | 10 | lcd.cls(); |
cc061495 | 1:f54097bcb8a0 | 11 | lcd.printf("Welcome to Speed Testing!"); |
cc061495 | 0:f3b9844205f2 | 12 | |
cc061495 | 0:f3b9844205f2 | 13 | while(1) { |
cc061495 | 1:f54097bcb8a0 | 14 | meas = analog_value.read() * 3300; // Converts and read the analog input value (value from 0.0 to 1.0) |
cc061495 | 1:f54097bcb8a0 | 15 | // Change the value to be in the 0 to 3300 range |
cc061495 | 1:f54097bcb8a0 | 16 | count++; |
cc061495 | 1:f54097bcb8a0 | 17 | sum+=meas; |
cc061495 | 1:f54097bcb8a0 | 18 | range=sum/count; |
cc061495 | 1:f54097bcb8a0 | 19 | |
cc061495 | 1:f54097bcb8a0 | 20 | if(range > meas) |
cc061495 | 1:f54097bcb8a0 | 21 | range -= 100.0; |
cc061495 | 1:f54097bcb8a0 | 22 | |
cc061495 | 1:f54097bcb8a0 | 23 | //pc.printf("measure = %.0f mV, mean = %.0f mV\n", meas, range); |
cc061495 | 1:f54097bcb8a0 | 24 | if (meas < range) { // If the value is greater than 2V then switch the LED on |
cc061495 | 1:f54097bcb8a0 | 25 | lcd.cls(); |
cc061495 | 0:f3b9844205f2 | 26 | t.start(); |
cc061495 | 1:f54097bcb8a0 | 27 | while(analog_value.read()*3300 < range); |
cc061495 | 0:f3b9844205f2 | 28 | t.stop(); |
cc061495 | 1:f54097bcb8a0 | 29 | if(t.read() > 0.0001){ |
cc061495 | 1:f54097bcb8a0 | 30 | double time = t.read(); |
cc061495 | 1:f54097bcb8a0 | 31 | double speed = 0.25 / time; |
cc061495 | 1:f54097bcb8a0 | 32 | if(speed > highscore){ |
cc061495 | 1:f54097bcb8a0 | 33 | highscore = speed; |
cc061495 | 1:f54097bcb8a0 | 34 | lcd.printf("!!!New record!!!"); |
cc061495 | 1:f54097bcb8a0 | 35 | lcd.locate(0,1); |
cc061495 | 1:f54097bcb8a0 | 36 | lcd.printf("!!!!!!!!!!!!!!!!"); |
cc061495 | 1:f54097bcb8a0 | 37 | wait(3); |
cc061495 | 1:f54097bcb8a0 | 38 | } |
cc061495 | 1:f54097bcb8a0 | 39 | lcd.locate(0,0); |
cc061495 | 1:f54097bcb8a0 | 40 | lcd.printf("%f s", time); |
cc061495 | 1:f54097bcb8a0 | 41 | lcd.locate(0,1); |
cc061495 | 1:f54097bcb8a0 | 42 | lcd.printf("%f m/s", speed); |
cc061495 | 1:f54097bcb8a0 | 43 | } |
cc061495 | 1:f54097bcb8a0 | 44 | else{ |
cc061495 | 1:f54097bcb8a0 | 45 | lcd.locate(0,0); |
cc061495 | 1:f54097bcb8a0 | 46 | lcd.printf("Too fast, didn't read =.="); |
cc061495 | 1:f54097bcb8a0 | 47 | } |
cc061495 | 0:f3b9844205f2 | 48 | } |
cc061495 | 0:f3b9844205f2 | 49 | t.reset(); |
cc061495 | 0:f3b9844205f2 | 50 | } |
cc061495 | 0:f3b9844205f2 | 51 | } |