Robocon_IPS
/
Nucleo_read_analog_value2
read speed
Fork of Nucleo_read_analog_value by
main.cpp@0:f3b9844205f2, 2017-05-31 (annotated)
- Committer:
- cc061495
- Date:
- Wed May 31 12:15:01 2017 +0000
- Revision:
- 0:f3b9844205f2
- Child:
- 1:f54097bcb8a0
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cc061495 | 0:f3b9844205f2 | 1 | #include "mbed.h" |
cc061495 | 0:f3b9844205f2 | 2 | |
cc061495 | 0:f3b9844205f2 | 3 | AnalogIn analog_value(PA_0); |
cc061495 | 0:f3b9844205f2 | 4 | |
cc061495 | 0:f3b9844205f2 | 5 | DigitalOut led(LED1); |
cc061495 | 0:f3b9844205f2 | 6 | Serial pc(USBTX,USBRX,115200); |
cc061495 | 0:f3b9844205f2 | 7 | Timer t; |
cc061495 | 0:f3b9844205f2 | 8 | |
cc061495 | 0:f3b9844205f2 | 9 | int main() { |
cc061495 | 0:f3b9844205f2 | 10 | pc.format(8,SerialBase::None,1); |
cc061495 | 0:f3b9844205f2 | 11 | float meas; |
cc061495 | 0:f3b9844205f2 | 12 | led = 1; |
cc061495 | 0:f3b9844205f2 | 13 | pc.printf("\nAnalogIn example\n"); |
cc061495 | 0:f3b9844205f2 | 14 | float max, min, mean; |
cc061495 | 0:f3b9844205f2 | 15 | max = analog_value.read()*3300; |
cc061495 | 0:f3b9844205f2 | 16 | min = max; |
cc061495 | 0:f3b9844205f2 | 17 | |
cc061495 | 0:f3b9844205f2 | 18 | |
cc061495 | 0:f3b9844205f2 | 19 | while(1) { |
cc061495 | 0:f3b9844205f2 | 20 | meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
cc061495 | 0:f3b9844205f2 | 21 | meas = meas * 3300; // Change the value to be in the 0 to 3300 range |
cc061495 | 0:f3b9844205f2 | 22 | if (meas>max) |
cc061495 | 0:f3b9844205f2 | 23 | max = meas; |
cc061495 | 0:f3b9844205f2 | 24 | if (meas<min) |
cc061495 | 0:f3b9844205f2 | 25 | min = meas; |
cc061495 | 0:f3b9844205f2 | 26 | mean = (max + min)/2; |
cc061495 | 0:f3b9844205f2 | 27 | pc.printf("measure = %.0f mV, mean = %.0f mV\n", meas, mean); |
cc061495 | 0:f3b9844205f2 | 28 | if (meas < mean) { // If the value is greater than 2V then switch the LED on |
cc061495 | 0:f3b9844205f2 | 29 | t.start(); |
cc061495 | 0:f3b9844205f2 | 30 | while(analog_value.read()*3300 < mean); |
cc061495 | 0:f3b9844205f2 | 31 | t.stop(); |
cc061495 | 0:f3b9844205f2 | 32 | //if(t.read() > 0.00001) |
cc061495 | 0:f3b9844205f2 | 33 | pc.printf("The time taken was %f seconds\n", t.read()); |
cc061495 | 0:f3b9844205f2 | 34 | wait(1); |
cc061495 | 0:f3b9844205f2 | 35 | } |
cc061495 | 0:f3b9844205f2 | 36 | t.reset(); |
cc061495 | 0:f3b9844205f2 | 37 | } |
cc061495 | 0:f3b9844205f2 | 38 | } |