I have trouble with programming Inferred sensor, model #:gp2y0a02yk0f

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.:cry: 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);

  }

I don't see anything obviously wrong with the code, so either the sensor is not working or you have wired it incorrectly.

Post a wiring diagram (hand drawn, not Fritzing).

Hint: rather than all the unnecessary averaging, just use analogRead() in a loop to make sure the sensor actually works.

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?

@jremington
Funny, I tried the analogRead() and it didn't show anything. here's a section of my old code's numbers

5
10
16
21
27
32
38
44
49
55
61
66
72
77
82
88
93
99
104
109
115
121
127
132
138
144
149
155
161
166
172
178
183
189
195
201
206
212
218
224
224
225
225
226
226
227
227
227
228
228
228
229
229
230
231
231
232
233
234
234
235
235
235
236
237
237
238
239
239
239
240
241
241
242
242
242
243
244
244
245
245
246
246
247
247
247
248
248
249
250
250
250
251
251
251
252
252
252
253
254
254
254
255
255
255
256
256
256
257
257
257
258
258
258
259
259
259
260
260
260
261
261
262
261
262
262
262
263
264
263
264
265
264
265
265
265
265
266
266
266
267
267
267
268
268
268
269
269
269
269
270
270
270
271
271
271
271
272
271
272
272
272
273
273
273
273
274
274
274
274
274
274
275
275
275
275
276
276

my new code didn't show anything. I'm sorry, but I only know how to do fritzing, sorry

here's the hyperlink
https://media.aisler.net/aisler-go-prod/project_images/contents/000/000/944/original/data?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=WQFKBTHVE2LQZ77G0W3R%2F20171008%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171008T013519Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=aecdf4ae0d13268a71d9e3dad6d6537cfd99860ad9ffaae1d5ba10e16ce31b40

and

https://media.aisler.net/aisler-go-prod/project_images/contents/000/000/945/original/data?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=WQFKBTHVE2LQZ77G0W3R%2F20171008%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171008T013752Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=b67714dd878369a670cef43bde4696179db4dd1e5590e719a60e17491c67e00a

@johnwasser
Hi, I changed my code to this

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.

12126
119
119
126
118
116
124
121
115
123
123
116
122
125
118
119125
122
116
123
123
116
123
125
118
120
126
119
119
126
118
116
124
121
115
123
123
116
122
125
118
119125
122
116
123
123
116
123
125
118
120
126
119
119
126
118
116
124
121
115
123
123
116
122
125
118
1193
6
12
21
29
37
43
44
44
44
44
46
47
47
47
49
57
66
74
81
83
83
83
84
85
88
96
96
97
100
107
108
108
116
116
116
125
125
125
133
130
128
129
123
115
110
110
111
111
119
119
117
125
125
126
133
125
117
116
110
110
112
118
118
117
123
116
116
122
120
114
122
122
115
121
124
117
119
125
118
118
125
118
116
124
121
115
122
122
116
122
125
117
119
125
117
117
124
118
116
123
121
115
123
123
115
121
124
117
119
125
117
117
125
118
116
123
121
114
123
123
116
121
125
118
118
125
117
117
124
117
114
122
121
114
122
122
115
121
125
117
118
124
117
117
124
117
115
121
120
114
121
121
114
119
123
117
117
124
117
117
123
118
114
121
120
113
121
121
114
119
123
116
116
123
116
116
123
117
113
121
120
114
121
121
114
119
123
116
117
124
116
116
123
118
114
121
120
113
120
120
114
118
123
116
117
124
116
116
124
119
114
122
121
115
122
122
115
119
124
117
117
123
116
116
123
118
114
122
121
115
122
122
115
119
124
116
116
123
116
116
123
118
113
121
121
114
121
122
114
118
123
116
116
122
115
115
122
118
113
121
121
115
122
122
115
118
124
116
116
122
114
114
121
117
112
120
120
114
121
119
113
120
121
113
117
123
116
116
124
116
115
122
110
117
109
108
116
115
107
116
116
109
116
120
111
111
120
111
111
119
110
109
117
117
109
118
118
110
117
121
114
114
122
114
114
122
113
110
118
118
110
119
119
111
117
121
113
113
121
113
113
121
113
109
118
118
110
119
119
112
116
122
114
114
122
113
113
120
113
109
116
116
108
118
118
111
114
121
113
113
122
112
112
120
115
110
119
119
110
119
119
111
115
121
112
112
120
111
111
119
115
109
118
118
110
119
119
111
114
120
113
113
121
111
111
119
115
110
117
117
109
118
118
111
113
120
112
112
120
111
111
118
115
109
118
118
110
119
119
111
113
120
112
112
119
111
111
119
117
111
118
118
110
119
119
111
112
118
111
111
119
110
110
117
115
109
117
117
110
118
121
114
114
122
113
113
120
111
111
119
118
110
118
118
112
120
123
114
114
122
114
114
121
113
112
120
120
113
121
121
114
122
125
118
118
126
117
117
124
116
114
120
120
113
122
122
115
123
126
119
119
127
119
119
125
117
115
123
123
115
124
124
117
123
127
119
119
127
119
119
126
117
115
122
122
115
123
123
116
121
126
119
119
126
117
117
124
116
113
119
119
112
121
121
114
119
125
118
118
125
116
116
123
117
113
121
121
114
123
123
116
119
126
117
117
124
116
116
123
118
113
121
121
115
124
124
117
120
127
119
119
126
117
117
123
119
113
121
121
114
123
124
119
121
128
120
120
127
119
119
126
123
119
128
128
122
131
131
125
126
135
126
126
133
124
124
131
128
121
130
130
123
132
133
126
128
136
128
128
136
127
127
134
132
126
135
135
128
137
139
132
131
137
128
128
136
127
127
134
133
127
136
136
129
138
141
134
134
142
134
134
142
133
132
139
138
132
141
141
135
142
146
139
140
147
139
139
147
137
136
145
145
140
150
152
146
155
159
153
154
161
154
154
163
154
153
162
162
157
167
168
162
168
175
167
167
173
163
163
170
162
158
164
164
156
164
164
157
162
167
163
166
175
169
172
182
179
179
186
188
185
193
194
186
192
198
191
193
197
188
189
194
189
182
190
190
183
192
192
184
187
193
185
185
192
182
182
189
184
177
180
176
164
167
162
150
147
148
138
136
138
128
126
134
131
128
136
137
134
146
148
147
155
167
165
170
182
177
182
192
193
194
208
213
210
222
225
219
222
231
224
226
237
230
233
243
240
240
250

the LED flickers like crazy and so does the motor

johnwasser:
Your reading of 90 would be 0.44V so somewhat further than 6". At what distance does the LED switch on and off?

the part where you said 90, I think you might have missunderstood. The setial port shows numbers way higher than those.
thaks,
squidsirymchenry

Why have you not posted your wiring diagram?

Is drawing lines on a piece of paper, using a pencil, too difficult for you?

Do not use PMs for asking technical questions on the forum.

Here are the diagrams, sadly it had to be in fritz, I know of no other designing software.

diagram of project.pdf (69.1 KB)

diagram of project 2.pdf (1.01 MB)

jremington:
Do not use PMs for asking technical questions on the forum.

Seconded

jremington:
Is drawing lines on a piece of paper, using a pencil, too difficult for you?

Evidently:

Squidsirymchenry:
Here are the diagrams, sadly it had to be in fritz, I know of no other designing software.

DON'T CROSS POST!!!!!!!!!!!!!!!!!!!!
http://forum.arduino.cc/index.php?topic=504397.msg3439897#msg3439897
I HAVE REPORTED THIS THREAD TO THE MODERATORS

You're already getting plenty of help in the other thread. Why would you do this?

Threads merged.

pert:
You're already getting plenty of help in the other thread. Why would you do this?

Because I don't have anyone helping.....
fun fact, I thought PM was for requesting for help.

sadly it had to be in fritz, I know of no other designing software.

So, you won't read and follow the forum rules, you can't draw a sensible wiring diagram on paper, and you don't know how to use Fritzing either.

The drawings posted in reply #7 are not only utterly useless, they are clearly wrong.

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!!

109
61
61
61
48
46
26
64
26
27
64
25
57
48
61
41
555
461
93
52
4
310
272
193
37
368

so what do you recommend next, I'm drawing it right now, on the computer, You won't understand my handwritting

Here is the drawing if you still want to help... the other part missing is the inferred sensor

new picture.pdf (113 KB)

the other part missing is the inferred sensor

Surely you are joking.

The wiring to the infrared sensor is the most important part of the project.

Carefully study reply #2 until you understand what johnwasser is saying.

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

61
newaverage
142
newaverage
142
newaverage
141
newaverage
141
newaverage
142
newaverage
141
newaverage
142
newaverage
141
newaverage
142
newaverage
142

I wonder why, and I also have another question what's the voltage that can power my Ir, 5 or 9 or.... Take a look at attached

weird symbols.pdf (9.35 KB)

here's more of those weird symbols

weird symbols (2).pdf (10.8 KB)