Skip navigation

Arduino

1 Post authored by: lifeisgood082

I am trying to use a hall effect sensor to activate a servo. I want the servo to move to a specific position when the hall effect sensor is activated and then move back when the sensor is deactivated. When I go to the analog read and look at the values I get from the hall effect sensor, the values are 0 when deactivated, but they range from 195-215 when acivated. I think this is why the servo twitches, but im not certain. I think I should have the input read 0 or 1. When he input is 1 the servo should move to this position and move back for values of 0.  Can you help me out? Heres the code I have so far

 

 

 

 

 

 

#include <Servo.h>

 

Servo myservo;  // create servo object to control a servo

 

int inPin = 0;    // select the input pin for the Hall Effect Sensorr

int val;    // variable to read the value from the analog pin

 

void setup()

{

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

 

void loop()

{

  val = analogRead(inPin);            // reads the value of the Hall Effect Sensorr (value between 0 and 300)

  val = map(val, 0, 300, 0, 20);     // scale it to use it with the servo (value between 0 and 20)

  myservo.write(val);                  // sets the servo position according to the scaled value

  delay(15);                           // waits for the servo to get there

Filter Blog

By date: By tag: