This program takes input from a Sharp GP2D120 infrared ranging sensor and outputs the result in ASCII. The ranging formula comes from an excellent article on Acroname’s site, tweaked a bit to work with my circuit.
Written in Wiring syntax, tested on an Arduino board:
/*
GP2D120 sensor example
by Tom Igoe
Reads a changing voltage from a GP2D120 IR ranging sensor
on analog input 0 and sends the result out in ASCII-encoded
decimal numbers.
Arduino/ATMega8 hardware connections:
A0/PC5: potentiometer on analog in 1
D0/PD0/RX0: Serial input from the PC via MAX232 or hex inverter
D1/PD1/TX0: Serial output to PC via MAX232 or hex inverter
Distance ranging formula comes from Acroname, http://www.acroname.com:
http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
Created 6 Oct. 2005
Updated 25 Oct. 2005
*/
int val; // outgoing ADC value
int distance = 0;
//function prototype:
void blink(int howManyTimes);
void setup()
{
// start serial port at 9600 bps:
beginSerial(9600);
blink(3);
}
void loop()
{
// read analog input:
val = analogRead(0);
// send analog value out:
printString("Analog Value =\t");
// Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (val + 5)) - 1;
printInteger(distance);
printString("\n\r");
// wait 10ms for ADC to reset before next reading:
delay(10);
}
// Blink the reset LED:
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}
Written for the BX-24:
'GP2D120 sensor example
'by Tom Igoe
' Reads a changing voltage from a GP2D120 IR ranging sensor
' on analog input 0 and sends the result out in ASCII-encoded
' decimal numbers.
' Analog in is on pin 15 of the BX-24
' Distance ranging formula comes from Acroname, http://www.acroname.com:
' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
' created 6 Oct 2005
' Updated 25 Oct. 2005
dim val as integer ' outgoing ADC value
dim distance as integer
sub main ()
call blink(3)
do
' read analog input:
val = getADC(15)
' Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (val + 5)) - 1
' send analog value out:
debug.print "Distance = "; cStr(distance)
' wait 10ms for ADC to reset before next reading:
call delay(0.01)
loop
end sub
' Blink the reset LED:
sub blink(byVal howManyTimes as integer)
dim i as integer
for i=0 to howManyTimes
call putPin(26, 1)
call delay(0.2)
call putPin(26,0)
call delay(0.2)
next
end sub
Written in PicBasic Pro, tested on a PIC18F252:
' GP2D120 sensor example ' by Tom Igoe ' Reads a changing voltage from a GP2D120 IR ranging sensor ' on analog input 0 and sends the result out in ASCII-encoded ' decimal numbers. ' Distance ranging formula comes from Acroname, http://www.acroname.com: ' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html ' I had to change the final constant to match my sensor's readings. ' Created 6 Oct. 2005 ' Updated 25 Oct. 2005 ' Define ADCIN parameters DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS ' pins: tx var portc.6 ' serial TX rx var portc.7 ' serial RX resetLED var portb.7 ' blinking LED ' analog in is on porta.0 ' constant to set the baud rate: inv9600 con 16468 ' define variables: adcVar var word distance var word ' subroutine variables: i var byte TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result setup: gosub blink main: ' read analog input: adcin 0, adcVar ' Calculate linear slope of reading (thanks, Acroname!): distance = (2914 / (adcVar + 5)) - 1 ' send the distance out: serout2 tx, inv9600, ["Distance = ", 9, distance, 10, 13] ' wait 10ms for ADC to reset before next reading: pause 10 goto main ' Blink the reset LED: blink: for i=0 to 3 high resetLED pause 200 low resetLED pause 200 next return