Go Down

Topic: How to control servo with magnetic sensor (Read 959 times) previous topic - next topic

hi all,  i am newbie.

I'm learning how to make a robot, and this time I have a problem to make my robot move .

How to control servo with magnetic sensor??

Code: [Select]
#include <Servo.h>

Servo servo;

const int pinmagnet =13; // magnet pin
const int pinservo = 9 ; // servo pin

int right = 180; //  servo move 180 degrees
int left = 0; // servo move 0 degrees
int magnetvalue = 0 ;

void setup (){
 
  pinMode ( pinservo, OUTPUT);
  pinMode ( pinmagnet, INPUT);
  Serial.begin(9600);
}

void loop ()
{
 
  magnetvalue= analogRead(pinmagnet);
 
if ( magnetvalue >= 180 )
     {
       servo.write (right);
     }
     
     else {
       servo.write(left);
     }
}


when magnetic sensor work, servo will move 180 degrees, and when magnetic sensor not work , servo will move to 0 degrees..
please help me..

JimboZA

You could get a reed switch, or better yet a Hall effect sensor, either of which will serve as a switch to make your pin high or low depending on where the magnet is.

Do you have either of those? If you don't, you could just simulate the presence or absence of the magnet with a push-button switch and push it by hand. Have you tried that?

I think you'll need a delay after each of those servo.writes, to give the servo time to get to where you want it.

"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Dave137

>= 180??? Are you trying to read the position of a servo with the feedback, more than just the 3 wires? I have not bought one of those servos but am very curious. Seems like it should give an accurate measurement of distance if the tires do not slip.

Go Up