Pages: [1] 2   Go Down
Author Topic: Need help getting IR sensor to trigger Servo motor  (Read 3524 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

WEll assuming the sensor is analog type you need to do something like this
Code:
#define sharp 0
main()
{
sharp(//set as input)
}
loop
{
analog read ( sharp )
if (//value <> something )
{
action
}
this is the pseudo code
- hope it helps  smiley
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...but without the 'main'
Logged

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...but without the 'main'
?  :-?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(the one in your example 'pseudo code')
Logged

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is Arduinoland - what is 'main'?
Logged

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
tried the pseudo-code out but i couldnt get it to work
well the pseudo code isnt ment to work smiley-wink post your code and someone will help you out  smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   smiley-razz

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

Pages: [1] 2   Go Up
Jump to: