ARDUINO SOURCE CODE –
/*
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;
int ledPin = 11;
int outPut = 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");
//Serial.println(val);
// Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (val + 5)) - 1;
printInteger(distance);
outPut = distance
;printString("\n\r");
lightLED(distance);
//analogWrite(ledPin, outPut);
// wait 10ms for ADC to reset before next reading:
// delay(10);
}
void lightLED(int value){
if( value == 9 ){
analogWrite(ledPin,0);
}else if (value == 8 ){
for( int i=0;i<30;>
outPut=i;
analogWrite(ledPin, i);
delay(50);
}
//Serial.println("distance = 8");
}else if (distance == 7){
for( int i=30;i<90;>
outPut=i;
delay(10);
analogWrite(ledPin,outPut);
}
}else if (distance == 6 ){
for( int i=90;i<120;>
outPut=i;
delay(10);
analogWrite(ledPin,outPut);
}
}else if (distance == 5 ){
for( int i=120;i<150;>
outPut=i;
analogWrite(ledPin,outPut);
}
}else if (distance == 4 ){
for( int i=150;i<180;>
outPut=i;
analogWrite(ledPin,outPut);
}
}else if (distance == 3 ){
for( int i=180;i<255;>
outPut=i;
analogWrite(ledPin,outPut);
}
}
}
// Blink the reset LED:
Here's the Schematic Circuit Diagram:
And here are the images of the work we did today:
No comments:
Post a Comment