Programming with a Hall effect sensor and servo motor

Actually this was listed by another member at a long long time ago...
http://arduino.cc/forum/index.php/topic,60455.0.html

I've discovered this thread while seraching through Google, and I've tried to upload that program on #16 to the board.

However, the servo motor had some strange performances. The motor turned to the desired position, but it will go back to 0 degrees immediately rather than stop at the desired position.

I tried with using analog and digital inputs, but it still does not solve my problem.

Would anyone give me some advice? Thanks!

We cannot see the code that you are using because you have not posted it here. Reply #16 in the thread that you linked to has only got a code snippet in it and we have no idea how you have incorporated it into your code.

Actually I've tried two versions of that code...but same problem exists in both of these codes...

The first one:

#include <Servo.h> //include the servo libary
Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int inPin = 0;    // select the input pin for the Hall Effect Sensor

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
 val = analogRead(inPin);  // read the Hall Effect Sensor
 if (val > (mean + sensitivity) || val < (mean - sensitivity))
 {
   // Move the servo one way
  for(pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
 else
 {
   // Move the servo the other way
   for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees
   {
     myservo.write(pos);              // tell servo to go to position in variable ‘pos’
     delay(15);                       // waits 15ms for the servo to reach the position
   }
 }
}

The second one is here:

int inPin = 0;    // select the input pin for the Hall Effect Sensor
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor
int mean = 508;
int sensitivity = 20;

void setup() 
{
pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}


void loop()
{
static bool DetectingHallEffect=false; // We will likely detect the hall effect sensor several times, make sure we only react when we first detect it.

val = analogRead(inPin);  // Read the Hall Effect Sensor
if((val < (mean + sensitivity)) && (val > (mean - sensitivity))) // Do we see the sensor?
  {
  if(!DetectingHallEffect)     // Make sure we only do this once for each detection of the hall effect sensor
    {
    DetectingHallEffect=true;  // This will be true until we don't see the sensor any more
    if(servo.read()==0)        // Check where the servo is and move it to the other position
      servo.write(170);
    else
      servo.write(0);
    }
  }
else
  DetectingHallEffect=false;   // Don't see the sensor any more, permit action the next time we see it
}

The first one won't work because each time through the loop() function the input is checked and, depending on it value, the servo is swept one way or the other, then the sensor is read again and the servo sweep repeats. Actually, it won't compile because of undeclared variables.

The second one tries to get round the problem by remembering the state of the sensor but that one won't compile either because the servo library has not been included, which makes me suspicious of the whole program.

Then I'll try to modify the second program by including the servo library and double-check it :wink:

BTW the first program did compiled without errors XD

What, even with val, mean and sensitivity undefined ?

Servo button toggle test code.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

BTW the first program did compiled without errors

Not as you posted it, it didn't... I copied the whole thing into the IDe and the compile fell over because "mean" and "sensititvity" are not defined.

(Unless there's something like VB's Option Explicit that I don't know about.....)

Unfortunately the Hall effect sensor was broken and I can't continue until I can get one in these 2-3 days...

However I've modified the code, and compiled with success. Would somebody can double-check it? Thanks!

#include<Servo.h>
Servo myServo;    // create the servo object to control the servo

int inPin = 3;    // select the input pin for the Hall Effect Sensor
int ledPin = 13;  // select the pin for the LED
int val = 0;      // variable to store the value coming from the sensor

void setup() 
{
  pinMode(inPin, INPUT);     // declare the inPin as an INPUT
  pinMode(ledPin, OUTPUT);   // declare the ledPin as an OUTPUT
  myServo.attach (9);        // attaches the servo on pin 9 to the servo object
}


void loop()
{
  static bool DetectingHallEffect=false;                    // We will likely detect the hall effect sensor several times, make sure we only react when we first detect it.

  val = digitalRead(inPin);                                 // Read the Hall Effect Sensor
  if((val = 0 ) && (val > 0))                               // Do we see the sensor?
  {
    if(!DetectingHallEffect)                                // Make sure we only do this once for each detection of the hall effect sensor
    {
      DetectingHallEffect=true;                             // This will be true until we don't see the sensor any more
      if(myServo.read()==0)                                 // Check where the servo is and move it to the other position
        myServo.write(10);
      else
        myServo.write(0);
    }
  }
else
  DetectingHallEffect=false;                                // Don't see the sensor any more, permit action the next time we see it
}

I only looked this far. There may be other problems

 if((val = 0 ) && (val > 0))
  1. = instead of ==
  2. Even with == how could val ever be equal to zero and greater than zero at the same time ?

Finally got the new sensor, but I found another problem...

In this testing program, I want the LED to turn off when the magnet is present and turn on again when the magnet is brought away.
But what happened is the LED just kept off and did not turn on again. What modifications should be made?

const int hallPin = 7;     // hall effect sensor pin inserted to pin 7
const int ledPin =  13;     // LED pin inserted to pin 13
// variables will change:
int hallState = 0;          // variable for reading the hall sensor status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the hall effect sensor pin as an input:
  pinMode(hallPin, INPUT);     
}

void loop(){
  // read the state of the hall effect sensor:
  hallState = digitalRead(hallPin);

  if (hallState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

How is the sensor wired ? Do you have a pullup resistor wired to it ?

If not, try initialising it like this  pinMode(hallPin, INPUT_PULLUP);This will ensure that the pin is held HIGH when the sensor is not active and avoid picking up stray signals on the input pin.

As a basic test, have you tried printing the output from the sensor to make sure that it is doing what you think ?