Brendan Kelly
/
mDot_AnalogIn_LDR_Example
reading analogue LDR
Fork of mDot_AnalogIn_Example by
Diff: main.cpp
- Revision:
- 2:55d40722ec9d
- Parent:
- 1:ceddded7137c
--- a/main.cpp Fri Oct 02 16:46:53 2015 +0000 +++ b/main.cpp Sun Aug 06 00:24:22 2017 +0000 @@ -13,10 +13,20 @@ #include "mbed.h" int main() { - AnalogIn in(PB_1); + AnalogIn a0(PB_1); while (true) { - printf("analog value: 0x%04X\r\n", in.read_u16()); - wait_ms(500); + //read as float (0.0-1.0, multiply by 1000) + float ldr1 = a0.read() * 1000; + wait_ms(100); + float ldr2 = a0.read() * 1000; + wait_ms(100); + float ldr3 = a0.read() * 1000; + wait_ms(100); + //average + float ldr = (ldr1 + ldr2 + ldr3) / 3; + //output 3 digits . 3 decimal places + printf("LDR: %3.3f\r\n", ldr); + wait(1); } } \ No newline at end of file