How to control servo with magnetic sensor

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

 #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);

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

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

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.

= 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.