Author Topic: help with Arduino IR sensor to servo coding. Will pay $  (Read 2050 times)

0 Members and 1 Guest are viewing this topic.

Offline totmasterTopic starter

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
help with Arduino IR sensor to servo coding. Will pay $
« on: October 22, 2013, 04:07:10 PM »
I found generic code based on looping code to turn a servo 180 degrees. I need to have the code rewritten so that the servo moves left (counter clockwise) 180 degrees and then back to 0 degrees and stops. The code is listed below

#include <Servo.h>

Servo myservo;  //creates a servo object
                         //a maximum of eight servo objects can be created

int pos = 0;        //variable to store servo position

//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)

int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;       

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 12;            //digital pin connected to the PIR's output
int pirPos = 13;           //connects to the PIR's 5V pin

void setup(){
  myservo.attach(4);    //attaches servo to pin 4
  Serial.begin(9600);    //begins serial communication
  pinMode(pirPin, INPUT);
  pinMode(pirPos, OUTPUT);
  digitalWrite(pirPos, HIGH);

  //give the sensor time to calibrate
  Serial.println("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(calibrationTime - i);
  //while making this Instructable, I had some issues with the PIR's output
  //going HIGH immediately after calibrating
  //this waits until the PIR's output is low before ending setup
  while (digitalRead(pirPin) == HIGH) {
  Serial.print("SENSOR ACTIVE");

void loop(){

  if(digitalRead(pirPin) == HIGH){  //if the PIR output is HIGH, turn servo

    /*turns servo from 0 to 180 degrees and back
    it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180
    and setting the servo's position in degrees to "pos" every 5 milliseconds
    it then does it in reverse to have it go back
    to learn more about this, google "for loops"
    to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn
    for(pos = 0; pos < 180; pos += 1)  //goes from 0 to 180 degrees
    {                                                 //in steps of one degree
      myservo.write(pos);                   //tells servo to go to position in variable "pos"
      delay(5);                                   //waits for the servo to reach the position
    for(pos = 180; pos>=1; pos-=1)    //goes from 180 to 0 degrees
      myservo.write(pos);                  //to make the servo go faster, decrease the time in delays for
      delay(5);                                  //to make it go slower, increase the number.
      //makes sure we wait for a transition to LOW before further output is made
      lockLow = false;           
      Serial.print("motion detected at ");
      Serial.println(" sec");
    takeLowTime = true;

  if(digitalRead(pirPin) == LOW){     

      lowIn = millis();             //save the time of the transition from HIGH to LOW
      takeLowTime = false;    //make sure this is only done at the start of a LOW phase
    //if the sensor is low for more than the given pause,
    //we can assume the motion has stopped
    if(!lockLow && millis() - lowIn > pause){
      //makes sure this block of code is only executed again after
      //a new motion sequence has been detected
      lockLow = true;                       
      Serial.print("motion ended at "); //output
      Serial.print((millis() - pause)/1000);
      Serial.println(" sec");
« Last Edit: October 26, 2013, 12:32:52 PM by totmaster »

Offline WhomBom

  • Full Member
  • ***
  • Posts: 64
  • Helpful? 0
Re: help with Arduino IR sensor to servo coding. Will pay $
« Reply #1 on: December 07, 2013, 05:06:37 PM »
hi totmaster I see this is your first post here, and you have not had any replies so far so I'll give it a try...

In general the people on this forum are very helpful if you ask well defined questions and are willing to do the work.

I know that if you have little experience things can be a bit overwhelming, especially in robotics where so many fields (mechanics, electronics, programming) come together, but don't let this put you off!

What you need to do is first make an overview of what it is you want your system to do, I'm guessing it is something like:

if IR sensor detects object: move servo left
otherwise: move servo right

Once you know exactly what it is you want your system to do, read into servo's. What makes the move left, what makes them move right?
Then get to know your arduino. I remember when I first used it they had some tutorials involving a blinking LED, that should give you a good base to drive your servo. And no, I'm not telling you why. You have to be willing to do the work remember? Just tinker around, I'm sure you'll find it to be worth the effort!


Get Your Ad Here