Counting Turns with Hall Effect sensor


i wrote file counting turns using allegro micro analog hall effect senor centers.  the sensor wired pin 1 (+5v) pin 2 (gnd) pin 3 chip atod pin.  read null @ center of 0 1024.  read 1 pole towards 0v , other pole towards 5v.

thanks

/*
turncounter
author: paul noel
date: june 6, 2010
description:  simple turn counter using hall effect sensor
analog version allegro micro.

the turn counter directional.

this uses 2 hall effect sensors
it knows way turning.

the 2 sensors should 4 or 5 mm apart.

the magnet pole direction doesn't matter long
as points towards sensor.

the principal here used rpm counter if
you used 1 sensor , timed instead of keeping
the state of sensors issue.
*/

int redpin = 13;    // led connected digital pin 13
int amberpin = 12;
int greenpin = 11;
int hallpin1 = 5;
int hallpin2 = 4;
int hallpin3 = 3;
int hallval1 = 0;
int hallval2 = 0;
int hallval3 = 0;
int prevstate = 0;
// set magnet strength (offdelta)
int offdelta = 25; // set calculate sensativity
// not set offdelta below 1 low want @ 25
int amberstate = low;
int greenstate = low;
int redstate = low;
int prevambstate = low;
int prevgrnstate = low;
int deltahi = 0;
int deltalo = 0;
int turncount = 0;
int turndelta = 0;
int turndir = 1; // set 1 normal. set else  if want opposite direction
int turnspeed = 10000; // set lower faster sensativity
// not set turnspeed below 1.
// turnspeed microseconds timing...
// setup() method runs once, when sketch starts

void setup()   {                
 // setup serial port
 serial.begin(9600);
 // initialize  pin states i/o:
 pinmode(redpin, output);  
 pinmode(amberpin, output);  
 pinmode(greenpin, output);
 pinmode(hallpin1, input);
 pinmode(hallpin2, input);
 // calculate sensor 0 range
 deltahi = 512 + offdelta + 1;
 deltalo = 512 - offdelta;
}

// loop() method runs on , on again,
// long arduino has power

void loop()                    
{
 hallval1 = analogread(hallpin1);
 hallval2 = analogread(hallpin2);
 greenstate = low;
 amberstate = low;
 
 if (!((hallval1 > deltalo) && (hallval1 < deltahi)))
 {
   greenstate = high;
 }
 if (!((hallval2 > deltalo) && (hallval2 < deltahi)))
 {
   amberstate = high;
 }
 if (!((hallval3 > deltalo) && (hallval3 < deltahi)))
 {
   redstate = high;
 }
 /*
  * process assumes there not case
  * sensor centers high high  from low low
  */
 // set states detection of change 0 zero
 if ((prevgrnstate == low) && (prevambstate == low))
 {
   // move 1 way
   if ((greenstate == high) && (amberstate == low))
   {
     turndelta = 1; // increment
   }
   // move other way
   if ((greenstate == low) && (amberstate == high))
   {
     turndelta = -1; // decrement
   }
  // ignore same state
 }
 if((!(prevgrnstate == prevambstate)) && ((greenstate == high && amberstate == high)))
 {
   turndelta = turndelta * 2;
 }
 // set states previous high/low
 if (((prevgrnstate == high) && (prevambstate == low)) && ((greenstate == low) && (amberstate == low)))
 {
     turndelta = turndelta - 1;
     if (turndelta < 0)
     {
       if (turndir == 1)
       {
       turncount++;
       } else
       {
         turncount--;
       }
       turndelta = 0;
       // report serial port here..
       serial.print("turns = ");
       serial.print(turncount);
       serial.print("\n");
     }
 }
 // low high states
 if (((prevgrnstate == low) && (prevambstate == high)) && (((greenstate == low) && (amberstate == low))))
 {

     turndelta = turndelta + 1;
     if (turndelta > 0)
     {
       if (turndir == 1)
       {
       turncount--;
       } else
       {
         turncount++;
       }
       turndelta = 0;
       // report serial port here..
       serial.print("turns = ");
       serial.print(turncount);
       serial.print("\n");
     }
   
 }
 
 // high high states
if ((prevgrnstate == high) && (prevambstate == high))
{
 // change - side
 if ((greenstate == low) && (amberstate == high))
 {
   turndelta = turndelta -1;
 }
 //  change + side
 if ((greenstate == high) && (amberstate == low))
 {
   turndelta++;
 }
}
 /* set previous state */
 prevgrnstate = greenstate;
 prevambstate = amberstate;
 
 /* write pins */
 digitalwrite(amberpin, amberstate);
 digitalwrite(greenpin, greenstate);
 
 delaymicroseconds(turnspeed);
}

is not possible use interrupts catch hall effect ?


Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > Counting Turns with Hall Effect sensor


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums