hall.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include <pigpio.h>
00004 
00005 /*
00006 OH3144E or equivalent Hall effect sensor
00007 
00008 Pin 1 - 5V
00009 Pin 2 - Ground
00010 Pin 3 - gpio (here P1-8, gpio 14, TXD is used)
00011 
00012 The internal gpio pull-up is enabled so that the sensor
00013 normally reads high.  It reads low when a magnet is close.
00014 
00015 gcc -o hall hall.c -lpigpio -lrt -lpthread
00016 sudo ./hall
00017 */
00018 
00019 #define HALL 14
00020 
00021 void alert(int gpio, int level, uint32_t tick)
00022 {
00023    static uint32_t lastTick=0;
00024 
00025    if (lastTick) printf("%d %.2f\n", level, (float)(tick-lastTick)/1000000.0);
00026    else          printf("%d 0.00\n", level);
00027 
00028    lastTick = tick;
00029 }
00030 
00031 int main(int argc, char *argv[])
00032 {
00033    int secs=60;
00034 
00035    if (argc>1) secs = atoi(argv[1]); /* program run seconds */
00036 
00037    if ((secs<1) || (secs>3600)) secs = 3600;
00038 
00039    if (gpioInitialise()<0) return 1;
00040 
00041    gpioSetMode(HALL, PI_INPUT);
00042 
00043    gpioSetPullUpDown(HALL, PI_PUD_UP);
00044 
00045    gpioSetAlertFunc(HALL, alert);
00046 
00047    sleep(secs);
00048 
00049    gpioTerminate();
00050 }
00051 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57