Zainul Charbiwala
/
TouchSense
Using a capacitor to implement a touch sensitive \"button\"
main.cpp@0:1d63f10fba0a, 2010-10-19 (annotated)
- Committer:
- zainulcharbiwala
- Date:
- Tue Oct 19 00:08:04 2010 +0000
- Revision:
- 0:1d63f10fba0a
Initial upload of TouchSense
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zainulcharbiwala | 0:1d63f10fba0a | 1 | #include "mbed.h" |
zainulcharbiwala | 0:1d63f10fba0a | 2 | |
zainulcharbiwala | 0:1d63f10fba0a | 3 | #define numsamples 1 |
zainulcharbiwala | 0:1d63f10fba0a | 4 | int touchSense(void); |
zainulcharbiwala | 0:1d63f10fba0a | 5 | |
zainulcharbiwala | 0:1d63f10fba0a | 6 | DigitalOut myled(LED1); |
zainulcharbiwala | 0:1d63f10fba0a | 7 | AnalogIn input(p20); |
zainulcharbiwala | 0:1d63f10fba0a | 8 | DigitalIn charger(p19); |
zainulcharbiwala | 0:1d63f10fba0a | 9 | DigitalOut ground(p18); |
zainulcharbiwala | 0:1d63f10fba0a | 10 | Serial pc(USBTX, USBRX); // tx, rx |
zainulcharbiwala | 0:1d63f10fba0a | 11 | |
zainulcharbiwala | 0:1d63f10fba0a | 12 | int main() { |
zainulcharbiwala | 0:1d63f10fba0a | 13 | while(1) { |
zainulcharbiwala | 0:1d63f10fba0a | 14 | if (touchSense()) { |
zainulcharbiwala | 0:1d63f10fba0a | 15 | myled = 1; |
zainulcharbiwala | 0:1d63f10fba0a | 16 | } else { |
zainulcharbiwala | 0:1d63f10fba0a | 17 | myled = 0; |
zainulcharbiwala | 0:1d63f10fba0a | 18 | } |
zainulcharbiwala | 0:1d63f10fba0a | 19 | wait(0.005); |
zainulcharbiwala | 0:1d63f10fba0a | 20 | } |
zainulcharbiwala | 0:1d63f10fba0a | 21 | } |
zainulcharbiwala | 0:1d63f10fba0a | 22 | |
zainulcharbiwala | 0:1d63f10fba0a | 23 | int touchSense(void) |
zainulcharbiwala | 0:1d63f10fba0a | 24 | { |
zainulcharbiwala | 0:1d63f10fba0a | 25 | float sample; |
zainulcharbiwala | 0:1d63f10fba0a | 26 | ground = 0; |
zainulcharbiwala | 0:1d63f10fba0a | 27 | charger.mode(PullUp); |
zainulcharbiwala | 0:1d63f10fba0a | 28 | charger.mode(PullNone); |
zainulcharbiwala | 0:1d63f10fba0a | 29 | sample=input.read(); |
zainulcharbiwala | 0:1d63f10fba0a | 30 | if (sample < 0.3) { |
zainulcharbiwala | 0:1d63f10fba0a | 31 | return 1; |
zainulcharbiwala | 0:1d63f10fba0a | 32 | } else { |
zainulcharbiwala | 0:1d63f10fba0a | 33 | return 0; |
zainulcharbiwala | 0:1d63f10fba0a | 34 | } |
zainulcharbiwala | 0:1d63f10fba0a | 35 | } |