Dependencies: mbed
Fork of Analog_input by
main.cpp@0:bf01ecc78e6e, 2019-01-08 (annotated)
- Committer:
- jimbaud
- Date:
- Tue Jan 08 08:08:40 2019 +0000
- Revision:
- 0:bf01ecc78e6e
Exemple of analog input using
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimbaud | 0:bf01ecc78e6e | 1 | // https://os.mbed.com/docs/v5.7/reference/analogin.html |
jimbaud | 0:bf01ecc78e6e | 2 | |
jimbaud | 0:bf01ecc78e6e | 3 | #include "mbed.h" |
jimbaud | 0:bf01ecc78e6e | 4 | AnalogIn analog_value(A0); |
jimbaud | 0:bf01ecc78e6e | 5 | DigitalOut led(LED1); |
jimbaud | 0:bf01ecc78e6e | 6 | |
jimbaud | 0:bf01ecc78e6e | 7 | int main() { |
jimbaud | 0:bf01ecc78e6e | 8 | float meas; |
jimbaud | 0:bf01ecc78e6e | 9 | printf("\nAnalogIn example\n"); |
jimbaud | 0:bf01ecc78e6e | 10 | |
jimbaud | 0:bf01ecc78e6e | 11 | while(1) { |
jimbaud | 0:bf01ecc78e6e | 12 | meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
jimbaud | 0:bf01ecc78e6e | 13 | meas = meas * 3300; // Change the value to be in the 0 to 3300 range |
jimbaud | 0:bf01ecc78e6e | 14 | printf("measure = %.0f mV\n", meas); |
jimbaud | 0:bf01ecc78e6e | 15 | |
jimbaud | 0:bf01ecc78e6e | 16 | if (meas > 2000) { // If the value is greater than 2V then switch the LED on |
jimbaud | 0:bf01ecc78e6e | 17 | led = 1; |
jimbaud | 0:bf01ecc78e6e | 18 | } |
jimbaud | 0:bf01ecc78e6e | 19 | else { |
jimbaud | 0:bf01ecc78e6e | 20 | led = 0; |
jimbaud | 0:bf01ecc78e6e | 21 | } |
jimbaud | 0:bf01ecc78e6e | 22 | wait(0.2); // 200 ms |
jimbaud | 0:bf01ecc78e6e | 23 | } |
jimbaud | 0:bf01ecc78e6e | 24 | } |