Society of Robots - Robot Forum

Software => Software => Topic started by: Xyver on June 29, 2010, 03:27:50 PM

Title: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
Post by: Xyver on June 29, 2010, 03:27:50 PM
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?
Title: Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
Post by: amando96 on June 29, 2010, 05:22:31 PM
You just have to read an analogue port  ???
Title: Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
Post by: KurtEck 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
Title: Re: Trouble with coding the Sharp GP2D120 IR Range Sensor with Arduino
Post by: Xyver on June 30, 2010, 06:03:22 AM
Excellet, that was exactly the kind of advice i wanted to hear.  thank you!