Lights blink when a magnet is detected
Dependencies: mbed
main.cpp@0:f556205c07a6, 2019-04-29 (annotated)
- Committer:
- m211002
- Date:
- Mon Apr 29 17:16:18 2019 +0000
- Revision:
- 0:f556205c07a6
- Child:
- 1:c847f1af2942
Light blinks when a magnet is detected
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m211002 | 0:f556205c07a6 | 1 | #include "mbed.h" |
m211002 | 0:f556205c07a6 | 2 | Serial pc(USBTX,USBRX); |
m211002 | 0:f556205c07a6 | 3 | |
m211002 | 0:f556205c07a6 | 4 | // Hall effect sensor |
m211002 | 0:f556205c07a6 | 5 | DigitalIn hall(p21); |
m211002 | 0:f556205c07a6 | 6 | DigitalOut hallpwr(p22); |
m211002 | 0:f556205c07a6 | 7 | |
m211002 | 0:f556205c07a6 | 8 | InterruptIn event(p21); |
m211002 | 0:f556205c07a6 | 9 | DigitalOut led1(LED1); |
m211002 | 0:f556205c07a6 | 10 | DigitalOut led4(LED4); |
m211002 | 0:f556205c07a6 | 11 | |
m211002 | 0:f556205c07a6 | 12 | int hallvalue; |
m211002 | 0:f556205c07a6 | 13 | int hallstatus=0; |
m211002 | 0:f556205c07a6 | 14 | void trigger() |
m211002 | 0:f556205c07a6 | 15 | { |
m211002 | 0:f556205c07a6 | 16 | while (hallstatus!=0) { |
m211002 | 0:f556205c07a6 | 17 | led1=1; |
m211002 | 0:f556205c07a6 | 18 | wait(1); |
m211002 | 0:f556205c07a6 | 19 | led1=0; |
m211002 | 0:f556205c07a6 | 20 | hallstatus=0; |
m211002 | 0:f556205c07a6 | 21 | } |
m211002 | 0:f556205c07a6 | 22 | } |
m211002 | 0:f556205c07a6 | 23 | int main() |
m211002 | 0:f556205c07a6 | 24 | { |
m211002 | 0:f556205c07a6 | 25 | hall.mode(PullUp); |
m211002 | 0:f556205c07a6 | 26 | hallpwr=0; |
m211002 | 0:f556205c07a6 | 27 | wait(0.2); |
m211002 | 0:f556205c07a6 | 28 | hallpwr =1; |
m211002 | 0:f556205c07a6 | 29 | wait(0.2); |
m211002 | 0:f556205c07a6 | 30 | event.rise(&trigger); |
m211002 | 0:f556205c07a6 | 31 | while(1) { |
m211002 | 0:f556205c07a6 | 32 | hallvalue=hall.read(); |
m211002 | 0:f556205c07a6 | 33 | if (hallvalue==1) { |
m211002 | 0:f556205c07a6 | 34 | hallstatus=1; |
m211002 | 0:f556205c07a6 | 35 | } |
m211002 | 0:f556205c07a6 | 36 | pc.printf("hallvalues:%d\n\r",hallvalue); |
m211002 | 0:f556205c07a6 | 37 | wait(0.25); |
m211002 | 0:f556205c07a6 | 38 | } |
m211002 | 0:f556205c07a6 | 39 | } |