Author Topic: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino  (Read 3051 times)

0 Members and 1 Guest are viewing this topic.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Trying to find a code for the IR rangefinder that I can tinker with, all I'm looking for is a code that will make a LED light up when something is 20cm away from the sensor.  Been looking around the internet all morning, and everything is too confusing...  So far this is what I found
Code: [Select]
int tot = 0;                  // the running total
void setup()
 {
 Serial.begin(9600);
 }
 
float read_ir(byte pin) {
int temptot;
temptot = analogRead(pin);
if (temptot < 3)
return -1; // invalid value

return (6787.0 /((float)temptot - 3.0)) - 4.0;
}
void loop()
 {
tot = read_ir(2);  // Assign the value of function "read_ir" (passing the
                   // variable 2 is used to tell the function what pin the
                   // IR signal is connected to) to variable "tot".
Serial.println(tot); // print the tot variable to the serial connection
 delay(500);       //delay 1/4 seconds.
 }
 
// this is based on code: http://www.arduino.cc/playground/Main/ReadGp2d12Range
but I dont really understand any of it.  Is there a simpler way?

Offline amando96

  • Robot Overlord
  • ****
  • Posts: 187
  • Helpful? 1
Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
« Reply #1 on: June 29, 2010, 05:22:31 PM »
You just have to read an analogue port  ???
Rorcle, 60% complete
AATV, 5% complete

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
« Reply #2 on: June 29, 2010, 05:56:54 PM »
Yes,  you could simply compare the value returned by analogread to some value that you find by trial and error or by looking at the specs.

Please forgive that I have not done very much yet with the Arduino so I may not completely flesh this out...

That is you could start off by doing something as simple as:

Have your main loop simply repeat looping through doing something like:
Code: [Select]
int val;
int prev;

void setup() {
  prev = 0;
  Serial.begin(9600);
}

void loop() {
  val = analogRead(0);    // assume pin 0...
  if (val != prev) {
    Serial.println(val, DEC);
    prev = val;
  }
  delay(50);
}

This will simply do the analog conversions and print out the values when it changes.  Now you can simply move a target around and see how the value changes.  You will probably find that as you move the target closer to you the value will go up.  You can then decide at what the value is at the distance you wish to trigger the LED.  You then can simply set the LEDS output port to the appropriate value.  For example if you find that the trigger point is around 330, you could change the code to something like:
Code: [Select]
void loop() {
  val = analogRead(0);    // assume pin 0...
  if (val >330)
    digitalWrite(1, HIGH);
  else
    digitalWrite(1,LOW)
  delay(50);
}
Again there may be issues here, also I show pins 1 and 0... But hopefully it gives you a few hints.  Also how you turn on or off the LED will depend on how you have it wired.

Good Luck
Kurt

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
« Reply #3 on: June 30, 2010, 06:03:22 AM »
Excellet, that was exactly the kind of advice i wanted to hear.  thank you!

 


Get Your Ad Here