IR proximity sensor
Here’s the Sharp product i’m using: http://www.sparkfun.com/datasheets/Sensors/Infrared/gp2y0a02yk_e.pdf
bought from here: http://www.sparkfun.com/products/8958
This code is simply smoothing an analog input value (0-5V), and it seems to work ok, but I have to see – I’m working on graphing the values (using Excel and CoolTerm) to make sure that my linearizing function I got from here: http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html = has worked correctly
const int numReadings = 100; int readings[numReadings]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int inputPin = A0; void setup() { // initialize serial communication with computer: Serial.begin(9600); // initialize all the readings to 0: for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; } void loop() { // subtract the last reading: total= total - readings[index]; // read from the sensor: readings[index] = analogRead(inputPin); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings) // ...wrap around to the beginning: index = 0; // calculate the average: average = total / numReadings; // send it to the computer as ASCII digits Serial.println(average); }
This code also is seems good but I have to check it out more closely:
int sensorPin = 0; // input pin for the sensor int barPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int barPinCount = 10; int volt = 0; // variable to store the value coming from the sensor int zeit = 100; // *10 = Gesamtzeit - total time void setup() { Serial.begin(9600); int thisPin; // the array elements are numbered from 0 to (pinCount - 1). // use a for loop to initialize each pin as an output: for (int thisPin = 0; thisPin < barPinCount; thisPin++) { pinMode(barPin[thisPin], OUTPUT); } } void loop() { int volt = 0; for(int i=0; i<10; i++) { volt += analogRead(sensorPin); delay(zeit); } volt /= 10; Serial.println(volt); int litCount = 0; if (volt <= 82) { // >= 80cm litCount = 1; } else if (volt <= 92) { // >= 70cm litCount = 2; } else if (volt <= 102) { // >= 60cm litCount = 3; } else if (volt <= 123) { // >= 50cm litCount = 4; } else if (volt <= 154) { // >= 40cm litCount = 5; } else if (volt <= 184) { // >= 30cm litCount = 6; } else if (volt <= 266) { // >= 20cm litCount = 7; } else if (volt <= 328) { // >= 15cm litCount = 8; } else if (volt <= 461) { // >= 10cm litCount = 9; } else if (volt > 461) { // < 10cm litCount = 10; } for(int b=0; b<10; b++) { if(b<=litCount) digitalWrite(barPin[b], HIGH); // Turn the bar on else digitalWrite(barPin[b], LOW); // Turn the bar off } }