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.
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.
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
}
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 ?