Object tracker

Few details about building

Servo

Two sharps

Arduino uno and roboshield

Acrylic strips

Sensors mounted

Completed work

Tracks the object

  • Actuators / output devices: Tower pro MG995 servo
  • Control method: Autonomous.
  • CPU: arduino uno
  • Operating system: windows XP
  • Power source: 5V USB
  • Programming language: Arduino
  • Sensors / input devices: Sharp GP2D12 Infrared Sensor x 2
  • Target environment: indoor

This is a companion discussion topic for the original entry at https://community.robotshop.com/robots/show/object-tracker

cant your sharp track your

cant your sharp track your hand at any farther distance? just curious. Also, please post the code, it looks different from your forum post. Very well done, no jerky movements, it is slick

Yes

Yes the sharp can track my hand at the farther distance than shown in the video.

Code:

#include <Servo.h>
int sharp1 = 0;             // right sensor analogue pin
int sharp2 = 1;            // left sensor analogue pin
int leftdist = 0;           // variable to store left sensor reading
int rightdist = 0;        // variable to store right sensor reading
int headpin = 8;       // pin to which servo is connected
int pos = 90;          // center position for servo

Servo head;      // create a servo object

void setup() {
  head.attach(headpin);
  head.write(pos);         //center the servo
}

void loop() {
  delay(10);
  rightdist = analogRead(sharp1);
  leftdist = analogRead(sharp2);
  if (rightdist < 600 && leftdist < 600) faceforward();
  if (rightdist < 600 && leftdist < 400) faceright();
  if (leftdist < 600 && rightdist < 400) faceleft();
  head.write(pos);
}

void faceright() {
  pos = pos + 1;      //  adding 5 to the servo command(i even tried with 1 instead of 5)
  }
void faceleft() {
  pos = pos - 1;     //  subtracting 5 from the servo command(i even tried with 1 instead of 5)
  }
  void faceforward () {
  head.write(pos);
  }

:slight_smile:

:slight_smile:

ya thanks to you for the

ya thanks to you for the code revision

Good to see that your

Good to see that your tracker has improved a lot =)
If you make the following adjustment, your tracking code should be even more responsive:

void loop() {
  delay(10);
  rightdist = analogRead(sharp1);
  leftdist = analogRead(sharp2);
  if (rightdist < 600 && leftdist < 600) {
    if (rightdist > leftdist) faceright();
    else if (leftdist > rightdist) faceleft();
  }
  head.write(pos);
}

There is actually no need for the faceforward() function, as you’re already performing head.write(pos); at the end of every loop.
Also, since you have only ‘if’ comparisons and no ‘else if’ or ‘else’ comparisons, when leftdist and rightdist are both < 400, you actually end up calling faceforward() then faceright() then faceleft() and then writing with head.write(pos) after that, all in the same iteration of loop().

ThanksI’ll try this code

Thanks

I’ll try this code .

and yeah i had figured it out earlier that there was no need to put faceforward() function but i was too lazy to change the code and test it again because of my arduino IDE.(see the forum post here maybe you can give a solution)

 

Nice suggestion! I would

Nice suggestion!  I would love to see video on how this changes the response.  I still haven’t recieved my sharps yet… :(  I’ll be playing with this soon enough.

**Info, **

Where do you finf your roboshield ?

Thanks

Michel

Basile gave the answer to

Basile gave the answer to your question.

Thanks

Thanks for info!!

Great ! thanks 

 

Michel, Canada