Go Down

Topic: Need help getting IR sensor to trigger Servo motor (Read 3895 times) previous topic - next topic

Zephirym

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.

Groove

You haven't declared your Sharp sensor or its interface - how do you expect it to trigger anything?
Per Arduino ad Astra

jada

WEll assuming the sensor is analog type you need to do something like this
Code: [Select]

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

this is the pseudo code
- hope it helps  :)

Groove

Per Arduino ad Astra

jada


Groove

Per Arduino ad Astra

jada

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

Groove

Per Arduino ad Astra

jada

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

Zephirym

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.

jada

Quote
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  :)

Zephirym

Oh i know that. I used it as a guideline.  :)
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.

Code: [Select]
#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);
}

Groove

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?
Per Arduino ad Astra

jada

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

Zephirym

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.   :P

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

Go Up