Hi, thanks for reading this,
I'm trying to make my IR detect an obstacle when its values increase to 2.5 feet away. Then using a motor that increases in strength as the object approaches. My IR can go to 5 feet away, but it seems that it can only go to about 1 feet. I'm pretty certain my hardware is correct, I checked it many times. I'm using Arduino Uno if you want to know
I don't know if it is my sensor or my program that's having a problem, look below.
I had trouble figuring out what's wrong with my code,
As you can see the commented out "finalir" section. if I tried to use the average or "finalir" Both results seemed to change only by the numbers. What I mean is if I put my had in front of the IR, it stays the same, the values continue to show what they are showing. For ex: 60, 60 or 280, 280. It seems to move on its own, sometimes jumping to negative if I use the map() function.
please help! My deadline to do this is by November and I seem a long way from making it,
thanks for reading and helping,
Squidsirymchenry
const int irreadings = 40;
int readings[irreadings];
int i = 0;
int total = 0;
int average = 0;
int finalir = 0;
void setup() {
pinMode(A0,INPUT);//Input for the Ir sensor
pinMode(4,OUTPUT);// LED and motor
Serial.begin(9600);
for (int irnow = 0; irnow < irreadings; irnow++) {
readings[irnow] = 0;
}
}
void loop() {
total = total - readings[i];
readings[i] = analogRead(A1);
total = total + readings[i];
i = i + 1;
if (i >= irreadings)
{i = 0;}
average = total / irreadings;
//finalir = map(average, 100, 600, 0, 200);
if (average > 90) // if (finalir> 90)
digitalWrite (4, HIGH);
else
digitalWrite (4, LOW);
Serial.println(average);//Serial.println(finalir);
delay(1);
}
Which pin is your sensor connected to? You set the pinMode() of A0 (unnecessary if you are using the pin for analogRead()) but getting your input from A1. That won't work if your sensor is on A0.
"Infrared proximity sensor made by Sharp. Part # GP2Y0A02YK0F has an analog output that varies from 2.8V at 15cm to 0.4V at 150cm with a supply voltage between 4.5 and 5.5VDC."
So it should measure from about 6" to 60" (5 feet).
Your reading of 90 would be 0.44V so somewhat further than 6". At what distance does the LED switch on and off?
const int irreadings = 40;
int readings[irreadings];
int i = 0;
int total = 0;
int average = 0;
int finalir = 0;
void setup() {
pinMode(A0,INPUT);//Input for the Ir sensor
pinMode(4,OUTPUT);// LED and motor
Serial.begin(9600);
for (int irnow = 0; irnow < irreadings; irnow++) {
readings[irnow] = 0;
}
}
//void loop(){
//analogRead(A0);
//}
void loop() {
total = total - readings[i];
readings[i] = analogRead(A0);
total = total + readings[i];
i = i + 1;
if (i >= irreadings)
{i = 0;}
average = total / irreadings;
//finalir = map(average, 100, 600, 0, 200);
if (average > 90)
digitalWrite (4, HIGH);
else
digitalWrite (4, LOW);
Serial.println(average);
delay(1);
}
For the truth having a 90 is fairly low for me as my serial port shows way higher numbers without mapping. It may be glitching for some of them just browse through them and look.
If this sketch doesn't show different analog values at different distances your sensor is either faulty or wired incorrectly.
// Try this just to see what analog values you get for different distances.
// Write down a list of distances and values.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
Yeah, I tried that, It worked!! and you people should stop screaming at a 13-year-old boy who is self-taught at programming and logged online to aurdino last night!!
I think I see the wiring problem. It looks like you have a capacitor in the +5V wire going to the sensor. That is not good. Why did you do that? If someone told you to put a capacitor ACROSS the power input to reduce noise the + side of the capacitor goes to the +5V wire and the - side of the capacitor goes to Ground.
Hello, I've been working on a new code. @john you are correct, I switched the capacitor and it worked!!@jremington, I'm afraid you misunderstood, I meant that the three trailing lines connects to the Ir sensor, which was cut off because of the size of the file. I apologize for not making that clear. And thank you for stopping your screams. Anyways, I worked to improve my code, listed below
// const int irreadings = 40;
int total;
int readings;
int irnow;
int average;
int iRead;
//float Cm;
int newaverage;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(A1, INPUT);
int iRead = (analogRead(A1));
Serial.println(iRead);
delay(100);
}
void loop()
{
irnow= 0;
while ( irnow < 9)
{
readings = analogRead(A1);
//Serial.println("readings");
// Serial.println(readings);
total = total + readings;
// Serial.println("total");
// Serial.println(total);
irnow = irnow +1;
// Serial.println("irnow");
// Serial.println(irnow);
}
average = total / 11 ;
// Serial.println("average");
// Serial.println(average);
delay(200);
/*Cm = 27.742 * pow (average, -1.239);
Serial.println("Cm");
Serial.println(Cm);
*/
newaverage = map(average, 0, 1023, 150,-10);
Serial.println("newaverage");
Serial.println(newaverage);
if (newaverage < 120)
{
digitalWrite(2, HIGH);
delay(100);
}
else
{
digitalWrite(2, LOW);
delay(100);
}
average = 0;
total = 0;
}
1 problem left is that the vibration sensor keeps running even when it shows this