Go Down

Topic: Counting Turns with Hall Effect sensor (Read 1 time) previous topic - next topic

PaulNoel

Jun 06, 2010, 03:36 pm Last Edit: Jun 06, 2010, 03:37 pm by TopTurtle Reason: 1
I wrote a file for counting turns using Allegro Micro Analog Hall Effect Senor that centers.  The sensor is wired Pin 1 (+5V) Pin 2 (GND) Pin 3 to chip AtoD pin.  Read Null at Center of 0 to 1024.  Read one pole towards 0V and the other pole towards 5V.

Thanks

/*
TurnCounter
Author: Paul Noel
Date: June 6, 2010
Description:  Simple turn Counter using Hall Effect Sensor
Analog version from Allegro Micro.

The turn counter is directional.

This uses two Hall Effect Sensors so that
it knows which way you are turning.

The two sensors should be about 4 or 5 mm apart.

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

The principal here could be used for an RPM counter if
You used only one sensor and timed it instead of keeping
the state of the sensors as the issue.
*/

int redPin = 13;    // LED connected to 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;
// this is set to magnet strength (offDelta)
int offDelta = 25; // Set this to calculate the sensativity
// Do not set offDelta below 1 Probably this is about as low as you want at 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 for normal. Set this to anything else  if you want the opposite direction
int turnSpeed = 10000; // Set this lower for faster sensativity
// do not set turnSpeed below 1.
// turnSpeed is microseconds timing...
// The setup() method runs once, when the sketch starts

void setup()   {                
 // Setup the Serial Port
 Serial.begin(9600);
 // initialize the  pin states I/O:
 pinMode(redPin, OUTPUT);  
 pinMode(amberPin, OUTPUT);  
 pinMode(greenPin, OUTPUT);
 pinMode(hallPin1, INPUT);
 pinMode(hallPin2, INPUT);
 // calculate the sensor zero range
 deltaHi = 512 + offDelta + 1;
 deltaLo = 512 - offDelta;
}

// the loop() method runs over and over again,
// as long as the 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;
 }
 /*
  * This process assumes that there will not be a case where
  * The sensor centers HIGH HIGH  from LOW LOW
  */
 // Set the states for Detection of Change from Zero Zero
 if ((prevGrnState == LOW) && (prevAmbState == LOW))
 {
   // Move One Way
   if ((greenState == HIGH) && (amberState == LOW))
   {
     turnDelta = 1; // Increment
   }
   // Move the other way
   if ((greenState == LOW) && (amberState == HIGH))
   {
     turnDelta = -1; // Decrement
   }
  // We ignore same state
 }
 if((!(prevGrnState == prevAmbState)) && ((greenState == HIGH && amberState == HIGH)))
 {
   turnDelta = turnDelta * 2;
 }
 // Set the states now for 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 this to the Serial Port here..
       Serial.print("Turns = ");
       Serial.print(turnCount);
       Serial.print("\n");
     }
 }
 // This is for 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 this to the Serial Port here..
       Serial.print("Turns = ");
       Serial.print(turnCount);
       Serial.print("\n");
     }
   
 }
 
 // This is for HIGH HIGH states
if ((prevGrnState == HIGH) && (prevAmbState == HIGH))
{
 // change to the - side
 if ((greenState == LOW) && (amberState == HIGH))
 {
   turnDelta = turnDelta -1;
 }
 //  change to the + side
 if ((greenState == HIGH) && (amberState == LOW))
 {
   turnDelta++;
 }
}
 /* set the previous state */
 prevGrnState = greenState;
 prevAmbState = amberState;
 
 /* write the pins */
 digitalWrite(amberPin, amberState);
 digitalWrite(greenPin, greenState);
 
 delayMicroseconds(turnSpeed);
}

moustic

Is it not possible to use interrupts to catch hall effect ?

Go Up