Lights blink when a magnet is detected

Dependencies:   mbed

main.cpp

Committer:
m211002
Date:
2019-04-29
Revision:
0:f556205c07a6

File content as of revision 0:f556205c07a6:

#include "mbed.h"
Serial pc(USBTX,USBRX);

// Hall effect sensor
DigitalIn hall(p21);
DigitalOut hallpwr(p22);

InterruptIn event(p21);
DigitalOut led1(LED1);
DigitalOut led4(LED4);

int hallvalue;
int hallstatus=0;
void trigger()
{
    while (hallstatus!=0) {
        led1=1;
        wait(1);
        led1=0;
        hallstatus=0;
    }
}
int main()
{
    hall.mode(PullUp);
    hallpwr=0;
    wait(0.2);
    hallpwr =1;
    wait(0.2);
    event.rise(&trigger);
    while(1) {
        hallvalue=hall.read();
        if (hallvalue==1) {
            hallstatus=1;
        }
        pc.printf("hallvalues:%d\n\r",hallvalue);
        wait(0.25);
    }
}