I’m not finding any information on the net to explain how to program the gp2d120 with an arduino. I have seen stuff in other codes but don’t understand any of it. So what I’m wondering is how do I actually tell it what to do at a certain distance. I understand that it use a certain amount of volt for different distance but wasn’t sure how you actually put that in a code. I have everything I need to get started on my robot and hoping I can just figure this sensor out so I can post it up on here. If someone know a tutorial for me that would be awesome.
ADC
Do you know what an Analogue to Digital Convertor is and does? Do you know how to use one in an Arduino program?
If so, apply all that to the G2D120.
If not, wait for another hero to come along. Sorry.
Loops / Conditionals
I may be missing your question but it sounds like you want something that looks like this:
void loop()
{
int distance = readSensor();
if ( distance > 10 )
{
doSomethingDifferent();
}
else
{
doNormalStuff();
}
}
I really should write
a basic progrtamming guide.
May do that tonight.
I was thinking about
I was thinking about something like that but wasn’t sure if it would understand if I would put 10 and it would take it as 10cm. Sorry I’m very new with sensors. So would that be just about how to do it then. Also do you really have to have a bypass capacitor or would it be ok without it.
that be a good idea,
that be a good idea, not really any good ones out there for actually writing codes for robotics.
Listen to rik
Forget about the fact that you are using a sensor. This is just an analogue device. Learn about ADC inputs, not sensors.
Oh I got it now!
The real problem here is geography! I just did a quick member check… OF COURSE you are having these problems…
Sedgwick County?!?!?!? –Well, there’s your problem…
Move up to Manhattan where it’s normal! --But then again, you do have The Yard there… I guess that’s a good thing!
lol, yep you better believe
lol, yep you better believe it.
Try this
If you hook your sensor up to the arduino then change the pin in this arduino code to the right pin, upload to your board then click the serial monitor button in your arduino ide then you will be able to see what is being given back from the sensor.
http://arduino.cc/en/Tutorial/Graph
Once you know all of that is working and you want to see it as a graph then download processing from:
here and run it with the code supplied in that example.
Uploaded code for my robot
I’ve uploaded the code for my robot https://www.robotshop.com/letsmakerobots/node/2372 which shows how I handled it.
**int LED =13;void setup(){ **
int LED =13;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
int distance = analogRead(0);
if ( distance > 2 )
{
digitalWrite(LED,LOW);
}
else
{
digitalWrite(LED,HIGH);
}
}
Am I’m doing something wrong cause it isn’t reading right. How do I get it to read 2 inches. It seem to always be on and then when I get in the 17 inch and under it goes off. Sorry I’m really new and the arduino board aren’t helping at all. I get much quicker reply here then I have ever anywhere else. Thank guys
The return from analogRead isn’t in inches
The value will be some number that you have to apply some math to in order to get the inches.
The following links will help:
http://www.arduino.cc/playground/Main/ReadGp2d12Range
http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
Measure approx 2 inches away
Measure approx 2 inches away from the bot and see what the distance reading is. Make that number = to the danger distance.
thanks alot, not actually
thanks alot, not actually what I was looking for but it still work. Luckly I try messing around with the serial port to see what it does. Now my question is, how do you get an exact reading though. Like is there an easier way of doing this or is this the best way to do it. I figure I could some how convert some numbers down to get it to read inches. Still I really have to thank you a bunch for this. I was wondering if I could put those numbers in but never even tried.
thanks, the arduino site
thanks, the arduino site didn’t make any sense to me since I haven’t done anything with bytes and the other site I’m going to have to look over more, but think I might be able to figure it out. I can’t believe I didn’t see that cause thats where I bought my sensor at.
**If you cant find a formula**<br><p>If you can
t find a formula that someone has already made it might be easier to just measure some arbitrary distances and make them constants in your code. Try the code below. See what numbers it spits out of the serial port. If you put something 2 inches in front and the value is say 500 then you can #define 2INCH 500 in your code and use/adjust it easily.
int LED =13;
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int distance = analogRead(0);
Serial.println(distance);
delay(100);
}
Yea thanks, that what I been
Yea thanks, that what I been doing for the past like 10 minutes or more lol. It spike alot so I just watch for the most common number. Also I went back and measure to just see if I had it right and it was off a little. I think I need something better to detect off of or something lol. So you think this would be the best way to do it though to shorten my code down alot. To me it seem like it, but just want to make sure.
Edit: Didn’t really understand this part #define 2INCH 500. I just put int distance == 500 and it mean turn it on at 2inch. Is the define part better or different, I don’t know much about #define.
#define
#define 2INCH 500 will allow you to do something like:
if ( analogRead(2) < 2INCH)
{
// your code here
}
that way when you come back a couple of months later, you can look at your code and remember that you wanted to do something at 2 inches, and not what the magic number 500 mean. This is especially helpful if that magic number is used in more than one place. Otherwise, at least put a comment in your code to help you remember later on what 500 meant.
Sensor Spikes
Two things:
- Do you have a 10uf capacitor across the voltage & ground connections of your sensor?
- You may need to do some smoothing of your data. Basically, you take multiple samples to help filter out spikes. See: http://www.arduino.cc/en/Tutorial/Smoothing