First Last
/
BMT-K9_wait_potmeter_store_value
blink LED; blink ratio on / off depends on potmeter value
main.cpp@0:34a08ea1e3fc, 2013-09-24 (annotated)
- Committer:
- vsluiter
- Date:
- Tue Sep 24 15:05:54 2013 +0000
- Revision:
- 0:34a08ea1e3fc
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:34a08ea1e3fc | 1 | #include "mbed.h" |
vsluiter | 0:34a08ea1e3fc | 2 | |
vsluiter | 0:34a08ea1e3fc | 3 | // myled is an object of class DigitalOut. It uses the LED_RED pin |
vsluiter | 0:34a08ea1e3fc | 4 | // in human speech: myled is an output that can be set to zero or one. LED_RED is the pin which is connected to the output |
vsluiter | 0:34a08ea1e3fc | 5 | DigitalOut myled(LED_RED); |
vsluiter | 0:34a08ea1e3fc | 6 | |
vsluiter | 0:34a08ea1e3fc | 7 | // pot is an object of class AnalogIn. It uses the PTB0 pin |
vsluiter | 0:34a08ea1e3fc | 8 | // in human speech: pot is an analog input. You can read the voltage on pin PTB0 |
vsluiter | 0:34a08ea1e3fc | 9 | AnalogIn pot(PTB0); |
vsluiter | 0:34a08ea1e3fc | 10 | |
vsluiter | 0:34a08ea1e3fc | 11 | int main() { |
vsluiter | 0:34a08ea1e3fc | 12 | //declare a variable to store the potmeter value in. myled.read() returns a float. |
vsluiter | 0:34a08ea1e3fc | 13 | float tijd; |
vsluiter | 0:34a08ea1e3fc | 14 | //while 1 is unequal to zero. For humans: loop forever |
vsluiter | 0:34a08ea1e3fc | 15 | while(1) { |
vsluiter | 0:34a08ea1e3fc | 16 | //read value of potmeter, and store in variable |
vsluiter | 0:34a08ea1e3fc | 17 | tijd = pot.read(); |
vsluiter | 0:34a08ea1e3fc | 18 | //make pin myled 'high', which means 3.3V. This turns off the LED |
vsluiter | 0:34a08ea1e3fc | 19 | myled = 1; |
vsluiter | 0:34a08ea1e3fc | 20 | //Wait takes seconds as argument ( wait(2.5) waits for 2 seconds) |
vsluiter | 0:34a08ea1e3fc | 21 | //in this case, we're waiting 0.5 seconds if the potmeter is in one end position (analog in has value 1.00) |
vsluiter | 0:34a08ea1e3fc | 22 | // and we're waiting zero seconds if the potmeter is in the other end position (analog in has value 0.00) |
vsluiter | 0:34a08ea1e3fc | 23 | wait(0.5*tijd); |
vsluiter | 0:34a08ea1e3fc | 24 | //make pin myled 'low', which means 0V. This turns on the LED |
vsluiter | 0:34a08ea1e3fc | 25 | myled = 0; |
vsluiter | 0:34a08ea1e3fc | 26 | //wait depending on 'tijd', inverse operation of other wait statement, so total time will always be 0.5 |
vsluiter | 0:34a08ea1e3fc | 27 | wait(0.5*(1-tijd)); |
vsluiter | 0:34a08ea1e3fc | 28 | } |
vsluiter | 0:34a08ea1e3fc | 29 | } |