Using potenciometers as analog inputs.
Dependencies: mbed
main.cpp@0:9f57b77b5be5, 2014-12-17 (annotated)
- Committer:
- tbjazic
- Date:
- Wed Dec 17 13:21:36 2014 +0000
- Revision:
- 0:9f57b77b5be5
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:9f57b77b5be5 | 1 | #include "mbed.h" |
tbjazic | 0:9f57b77b5be5 | 2 | |
tbjazic | 0:9f57b77b5be5 | 3 | DigitalOut led1(LED1); |
tbjazic | 0:9f57b77b5be5 | 4 | DigitalOut led2(LED2); |
tbjazic | 0:9f57b77b5be5 | 5 | DigitalOut led3(LED3); |
tbjazic | 0:9f57b77b5be5 | 6 | DigitalOut led4(LED4); |
tbjazic | 0:9f57b77b5be5 | 7 | |
tbjazic | 0:9f57b77b5be5 | 8 | AnalogIn pot1(p19); |
tbjazic | 0:9f57b77b5be5 | 9 | AnalogIn pot2(p20); |
tbjazic | 0:9f57b77b5be5 | 10 | |
tbjazic | 0:9f57b77b5be5 | 11 | int main() { |
tbjazic | 0:9f57b77b5be5 | 12 | float valuePot1, valuePot2, valueAverage; |
tbjazic | 0:9f57b77b5be5 | 13 | while(1) { |
tbjazic | 0:9f57b77b5be5 | 14 | valuePot1 = pot1.read(); // the first way of reading from analog input |
tbjazic | 0:9f57b77b5be5 | 15 | valuePot2 = pot2; // the second (simpler) way of reading from analog input |
tbjazic | 0:9f57b77b5be5 | 16 | valueAverage = (valuePot1 + valuePot2) / 2; // calculate average |
tbjazic | 0:9f57b77b5be5 | 17 | if (valueAverage <= 0.2) { // turn off all LEDs |
tbjazic | 0:9f57b77b5be5 | 18 | led1 = 0; |
tbjazic | 0:9f57b77b5be5 | 19 | led2 = 0; |
tbjazic | 0:9f57b77b5be5 | 20 | led3 = 0; |
tbjazic | 0:9f57b77b5be5 | 21 | led4 = 0; |
tbjazic | 0:9f57b77b5be5 | 22 | } else if (valueAverage > 0.2 && valueAverage <= 0.4) { // turn LED1 on |
tbjazic | 0:9f57b77b5be5 | 23 | led1 = 1; |
tbjazic | 0:9f57b77b5be5 | 24 | led2 = 0; |
tbjazic | 0:9f57b77b5be5 | 25 | led3 = 0; |
tbjazic | 0:9f57b77b5be5 | 26 | led4 = 0; |
tbjazic | 0:9f57b77b5be5 | 27 | } else if (valueAverage > 0.4 && valueAverage <= 0.6) { // turn LED1 and LED2 on |
tbjazic | 0:9f57b77b5be5 | 28 | led1 = 1; |
tbjazic | 0:9f57b77b5be5 | 29 | led2 = 1; |
tbjazic | 0:9f57b77b5be5 | 30 | led3 = 0; |
tbjazic | 0:9f57b77b5be5 | 31 | led4 = 0; |
tbjazic | 0:9f57b77b5be5 | 32 | } else if (valueAverage > 0.6 && valueAverage <= 0.8) { // turn LED1, LED2 and LED3 on |
tbjazic | 0:9f57b77b5be5 | 33 | led1 = 1; |
tbjazic | 0:9f57b77b5be5 | 34 | led2 = 1; |
tbjazic | 0:9f57b77b5be5 | 35 | led3 = 1; |
tbjazic | 0:9f57b77b5be5 | 36 | led4 = 0; |
tbjazic | 0:9f57b77b5be5 | 37 | } else { // turn on all LEDs |
tbjazic | 0:9f57b77b5be5 | 38 | led1 = 1; |
tbjazic | 0:9f57b77b5be5 | 39 | led2 = 1; |
tbjazic | 0:9f57b77b5be5 | 40 | led3 = 1; |
tbjazic | 0:9f57b77b5be5 | 41 | led4 = 1; |
tbjazic | 0:9f57b77b5be5 | 42 | } |
tbjazic | 0:9f57b77b5be5 | 43 | } |
tbjazic | 0:9f57b77b5be5 | 44 | } |