Robocon_IPS
/
Nucleo_read_analog_value2
read speed
Fork of Nucleo_read_analog_value by
main.cpp@2:5c2afc8d7c28, 2017-06-02 (annotated)
- Committer:
- cc061495
- Date:
- Fri Jun 02 08:12:02 2017 +0000
- Revision:
- 2:5c2afc8d7c28
- Parent:
- 1:f54097bcb8a0
version 2.0 (2 sensors)
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 | 2:5c2afc8d7c28 | 5 | Serial pc(USBTX,USBRX,115200); |
cc061495 | 0:f3b9844205f2 | 6 | AnalogIn analog_value(PA_0); |
cc061495 | 2:5c2afc8d7c28 | 7 | AnalogIn analog_value2(PA_1); |
cc061495 | 0:f3b9844205f2 | 8 | Timer t; |
cc061495 | 0:f3b9844205f2 | 9 | |
cc061495 | 0:f3b9844205f2 | 10 | int main() { |
cc061495 | 2:5c2afc8d7c28 | 11 | pc.format(8,SerialBase::None,1); |
cc061495 | 2:5c2afc8d7c28 | 12 | double meas, meas2, sum, sum2,count =0, range, range2, highscore = 0; |
cc061495 | 2:5c2afc8d7c28 | 13 | bool lock = false; |
cc061495 | 1:f54097bcb8a0 | 14 | lcd.cls(); |
cc061495 | 1:f54097bcb8a0 | 15 | lcd.printf("Welcome to Speed Testing!"); |
cc061495 | 0:f3b9844205f2 | 16 | |
cc061495 | 0:f3b9844205f2 | 17 | while(1) { |
cc061495 | 2:5c2afc8d7c28 | 18 | t.reset(); |
cc061495 | 1:f54097bcb8a0 | 19 | meas = analog_value.read() * 3300; // Converts and read the analog input value (value from 0.0 to 1.0) |
cc061495 | 1:f54097bcb8a0 | 20 | // Change the value to be in the 0 to 3300 range |
cc061495 | 2:5c2afc8d7c28 | 21 | meas2 = analog_value2.read() * 3300; |
cc061495 | 2:5c2afc8d7c28 | 22 | |
cc061495 | 1:f54097bcb8a0 | 23 | count++; |
cc061495 | 1:f54097bcb8a0 | 24 | sum+=meas; |
cc061495 | 1:f54097bcb8a0 | 25 | range=sum/count; |
cc061495 | 2:5c2afc8d7c28 | 26 | sum2+=meas2; |
cc061495 | 2:5c2afc8d7c28 | 27 | range2=sum2/count; |
cc061495 | 1:f54097bcb8a0 | 28 | |
cc061495 | 1:f54097bcb8a0 | 29 | if(range > meas) |
cc061495 | 1:f54097bcb8a0 | 30 | range -= 100.0; |
cc061495 | 2:5c2afc8d7c28 | 31 | if(range2 > meas2) |
cc061495 | 2:5c2afc8d7c28 | 32 | range2 -= 100.0; |
cc061495 | 2:5c2afc8d7c28 | 33 | |
cc061495 | 2:5c2afc8d7c28 | 34 | while(analog_value.read() * 3300 < range && analog_value2.read() * 3300 < range2); |
cc061495 | 2:5c2afc8d7c28 | 35 | if (meas < range && meas2 > range2 && !lock) { // If the value is greater than 2V then switch the LED on |
cc061495 | 2:5c2afc8d7c28 | 36 | lock = true; |
cc061495 | 2:5c2afc8d7c28 | 37 | t.start(); |
cc061495 | 2:5c2afc8d7c28 | 38 | while(analog_value2.read() * 3300 > range2); |
cc061495 | 2:5c2afc8d7c28 | 39 | t.stop(); |
cc061495 | 2:5c2afc8d7c28 | 40 | lcd.cls(); |
cc061495 | 1:f54097bcb8a0 | 41 | double time = t.read(); |
cc061495 | 2:5c2afc8d7c28 | 42 | double speed = 7 / time; |
cc061495 | 2:5c2afc8d7c28 | 43 | /* |
cc061495 | 1:f54097bcb8a0 | 44 | if(speed > highscore){ |
cc061495 | 1:f54097bcb8a0 | 45 | highscore = speed; |
cc061495 | 1:f54097bcb8a0 | 46 | lcd.printf("!!!New record!!!"); |
cc061495 | 2:5c2afc8d7c28 | 47 | |
cc061495 | 2:5c2afc8d7c28 | 48 | }*/ |
cc061495 | 2:5c2afc8d7c28 | 49 | pc.printf("%f s\n", time); |
cc061495 | 2:5c2afc8d7c28 | 50 | pc.printf("%f cm/s\n", speed); |
cc061495 | 2:5c2afc8d7c28 | 51 | |
cc061495 | 2:5c2afc8d7c28 | 52 | pc.printf("measure1 = %.0f mV, range1 = %.0f mV\n", meas, range); |
cc061495 | 2:5c2afc8d7c28 | 53 | pc.printf("measure2 = %.0f mV, range2 = %.0f mV\n", meas2, range2); |
cc061495 | 2:5c2afc8d7c28 | 54 | |
cc061495 | 1:f54097bcb8a0 | 55 | lcd.locate(0,0); |
cc061495 | 1:f54097bcb8a0 | 56 | lcd.printf("%f s", time); |
cc061495 | 1:f54097bcb8a0 | 57 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 58 | lcd.printf("%f cm/s", speed); |
cc061495 | 2:5c2afc8d7c28 | 59 | lock = false; |
cc061495 | 0:f3b9844205f2 | 60 | } |
cc061495 | 0:f3b9844205f2 | 61 | } |
cc061495 | 0:f3b9844205f2 | 62 | } |
cc061495 | 2:5c2afc8d7c28 | 63 | |
cc061495 | 2:5c2afc8d7c28 | 64 | void animation(){ |
cc061495 | 2:5c2afc8d7c28 | 65 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 66 | lcd.printf("!*!*!*!*!*!*!*!*"); |
cc061495 | 2:5c2afc8d7c28 | 67 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 68 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 69 | lcd.printf("~!~!~!~!~!~!~!~!"); |
cc061495 | 2:5c2afc8d7c28 | 70 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 71 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 72 | lcd.printf("!~!~!~!~!~!~!~!~"); |
cc061495 | 2:5c2afc8d7c28 | 73 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 74 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 75 | lcd.printf("~!~!~!~!~!~!~!~!"); |
cc061495 | 2:5c2afc8d7c28 | 76 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 77 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 78 | lcd.printf("!~!~!~!~!~!~!~!~"); |
cc061495 | 2:5c2afc8d7c28 | 79 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 80 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 81 | lcd.printf("~!~!~!~!~!~!~!~!"); |
cc061495 | 2:5c2afc8d7c28 | 82 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 83 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 84 | lcd.printf("!~!~!~!~!~!~!~!~"); |
cc061495 | 2:5c2afc8d7c28 | 85 | wait(0.5); |
cc061495 | 2:5c2afc8d7c28 | 86 | lcd.locate(0,1); |
cc061495 | 2:5c2afc8d7c28 | 87 | lcd.printf("~!~!~!~!~!~!~!~!"); |
cc061495 | 2:5c2afc8d7c28 | 88 | lcd.cls(); |
cc061495 | 2:5c2afc8d7c28 | 89 | } |