Need help getting IR sensor to trigger Servo motor

Hi, I’m relatively new to arduino and coding, I picked it up as an interest and so far I’ve been enjoying it even though it’s been pretty basic stuff. Now I’ve wanted to try something more advanced/tricky. I want to use a Sharp GP2Y0A21YK IR Proximity sensor to trigger two continuous servo motors (Parallax). I’ve managed to get the two servo’s moving in one direction, but i have no clue where to start or if im doing something wrong.

This is what I have so far:
#include <Servo.h>

Servo myservo;
Servo myservo2;

int pos = 0;

void setup(){
myservo.attach(12);
myservo2.attach(4);
}

void loop(){
for(pos = 0; pos < 180; pos+=1){
myservo.write(pos);
delay(15);}

for(pos = 0; pos < 180; pos-=1){
myservo2.write(pos);
delay(15);}
}

If someone would be kind enough to point this clueless newbie (me) in the right direction it would be much appreciated.

You haven’t declared your Sharp sensor or its interface - how do you expect it to trigger anything?

WEll assuming the sensor is analog type you need to do something like this

#define sharp 0 
main()
{
sharp(//set as input)
}
loop
{
analog read ( sharp ) 
if (//value <> something )
{
action
}

this is the pseudo code

  • hope it helps :slight_smile:

...but without the 'main'

...but without the 'main'

? :-?

(the one in your example 'pseudo code')

Sorry but i cant get you what do you exactly mean ? the pseudo code shouldnt include then main ?

This is Arduinoland - what is 'main'?

Oops i forgot [edit] main = void setup ()[/edit] :)

I figured that’s what main was. thanks though. I tried the pseudo-code out but i couldnt get it to work, like servos move foward and everything but it wont respond to the sensor. might be a hardware issue i guess? either that or im messing up somewhere.

tried the pseudo-code out but i couldnt get it to work

well the pseudo code isnt ment to work ;) post your code and someone will help you out :)

Oh i know that. I used it as a guideline. :slight_smile:
here’s the code. I did some changes to it because it was giving me errors.
so If something seems off, its because of me.

#include <Servo.h> 
#define sharp 0
Servo myservo;
Servo myservo2;
int pos = 0;
void setup(){
  Serial.begin(9600);
  myservo.attach(12);
  myservo2.attach(4);
}
void  loop() {
int sensorValue = analogRead (sharp);
  Serial.println(sensorValue);

  if(sensorValue > 250);
  {
 for(pos = 0; pos < 180; pos+=1){
    myservo.write(pos);
    delay(15);}
  for(pos = 0; pos < 180; pos-=1){
    myservo2.write(pos);
    delay(15);}
}

delay(15);
}

What's the problem? What does it do that is shouldn't and what doesn't it do that it should? What do you observe?

Ok what are the values you see on the serial monitor ?

so many questions @_@

ok. let me respond to Groove first. My intention for this project is to get the sensor to trigger the servos to move. the problem I’m having is that the servos rotate non stop without the trigger. so either the sensor is not working the way it should or I’m not sure what else. I know the sensor isnt dead or defective since i tried it out just to see if it was before.

now to newbie. when i open up the serial monitor i get one number, literally just one since no other value prints there after. and each time i reopen the serial monitor i get a new value. basically i get a single random value each time ranging from the 100’s to 3000’s.

not sure if I’ve answered the questions correctly but, oh well. I’m literally a complete newbie. :stuck_out_tongue:

oh and thanks for taking the time to help me. ;D

Servos don't normally rotate non-stop; sooner or later they hit the end-stops. Are these R/C servos modified for continuous rotation?

nope. I bought them as continuous rotation servos from adafruit. here's the link if you need it: http://www.adafruit.com/index.php?main_page=product_info&cPath=34&products_id=154

Also, difficult to see how an analogueRead returns 3000.

Cut out all the servo driving stuff and concentrate on getting the sensor readings looking sensible, just using prints. Tackle one thing at a time. [edit] Let's be clear on one thing; whatever they were described or sold as, those are not servos. How are you powering them? Not from the Arduino, I hope [/edit]

oh no, of course not. I figured that the arduino wouldn’t be able to power them. they’re being powered externally by a 9v battery pack, which seems to be working fine. although they drain the battery pretty fast, but thats expected.

Why are you powering 6V motors from 9V? Didn't you read the spec?