login

Author Topic: a little Servo action  (Read 2101 times)

0 Members and 1 Guest are viewing this topic.

ROCO666

a little Servo action
« on: November 25, 2011, 11:01:58 PM »
 hello there, i just got my wiring S and wanted to share a little example of servo controlling using a digital distance sensor. I think this could be helpful to people that are working in robotics and such.

without any more delays here's the code

Code: [Select]
/**
  *Controlling a Servo via distance Sensor
  by ROCO666
 
 */
#include <Servo.h>

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

int sens = 8; // digital pin 8 used to connect the sensor output

void setup()
{
  ser1.attach(7); //attaches the servo on pin 7 to the servo object

  pinMode(sens,INPUT); //sets digital pin 8 as input
   
}

void loop()
{
  if (digitalRead(sens)==LOW)// if the output is low meaning there's an obstacle
 // (in this case the sensor is  active low)
  {
    ser1.write(0); //this sequence sets the servo position
    //between 0 and 90 degrees every 500  miliseconds
    delay(500);
    ser1.write(90);
    delay(500);
  }           
  else            //if the output is high meaning the sensor is clear
  {
    ser1.write(90); //this sequence sets the he servo position
    //between 90 and 180 degrees every 500  miliseconds
    delay(500);
    ser1.write(180);
    delay(500);

  }

}
hope you like it.  :)
by the way the sensor i used is Sharp GP2Y0D810Z0F since i'm new here i'm not sure if it's okay to post links regarding the specs but you can find the datasheet webrowsing.
« Last Edit: December 06, 2011, 01:34:07 PM by bhagman »

barragan

Re: a little Servo action
« Reply #1 on: November 26, 2011, 06:45:52 PM »
nice, which distance sensor did you use?

ROCO666

Re: a little Servo action
« Reply #2 on: November 26, 2011, 09:34:11 PM »
at the end of the code is the reference but I'll post it again the sensor is Sharp GP2Y0D810Z0F, it's very easy to use and it's also cheap, about 6 or 7 bucks.