Lights blink when a magnet is detected
Dependencies: mbed
main.cpp@1:c847f1af2942, 2019-04-29 (annotated)
- Committer:
- m211002
- Date:
- Mon Apr 29 17:20:55 2019 +0000
- Revision:
- 1:c847f1af2942
- Parent:
- 0:f556205c07a6
hall effect code
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 | 1:c847f1af2942 | 16 | while (hallstatus!=0) { // when triggered the lead will send out a light and reset the hallstatus |
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 | 1:c847f1af2942 | 25 | hall.mode(PullUp); // brings up the sensor values |
m211002 | 1:c847f1af2942 | 26 | hallpwr=0; // sets the power readings |
m211002 | 0:f556205c07a6 | 27 | wait(0.2); |
m211002 | 1:c847f1af2942 | 28 | hallpwr =1;// sets the power readings |
m211002 | 0:f556205c07a6 | 29 | wait(0.2); |
m211002 | 1:c847f1af2942 | 30 | event.rise(&trigger); //starts the outside tigger fuctions when something happens on pin 21 |
m211002 | 0:f556205c07a6 | 31 | while(1) { |
m211002 | 1:c847f1af2942 | 32 | hallvalue=hall.read();// reads in hall effect values of (0 &1) |
m211002 | 1:c847f1af2942 | 33 | if (hallvalue==1) { // if the value read in is 1 then there is a change in status |
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 | } |