Pages: [1]   Go Down
Author Topic: How to control servo with magnetic sensor  (Read 807 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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..
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4430
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: