problem with counter++ (adds 2 instead of 1) (done, connection problems)

hi I'm making a smart parking and whenever the closest parking is taken the code tells it to go to the next closest one, but when the second closest parking is taken it says from instead go to park 2 ( it starts from 0) I says go to 3. here is my code:

**EDIT NO PROBLEM WITH THE CODE**

would like to know if I'm using counter++ wrong and how to fix it or am I doing something wrong.

I don’t think that it is adding 2 instead of 1. I think, instead, you just have an error in your logic.

The logic:
It only adds to the counter if val is less than 550.
It only displays on the LCD screen if val is 550 or greater.

The problem:
Multiple times in succession, val may be less than 550. The counter keeps adding silently. Finally, once val reaches 550 or greater, the total is displayed; possibly skipping some results in between.

Please explain:

int i = 0
for (i = 0; i < 6; i++)
val = analogRead(i);

How is the variable i both an integer used in a for loop counter and also a pin number you are reading the value of? How does reading the value from pin i of the Arduino affect the logic of your machine? Why does counter only increment if val is less than 550? And, how does the servo interact with functionality of your machine? Do you have a picture of your machine or an explanation of how it is supposed to function?

ty_ger07:
And, how does the servo interact with functionality of your machine?

its used to block the entrance to the parking once all the parks is full.
also how would you fix this problem if you know? I'm sitting on this the whole day cant figure it out.

The root of your problem is your logic.

Why is val an analogRead of i? What is i? When is val less than 550? Why only counter++ when val is less than 550? When is val 550 or greater? Why only display on LCD if val is 550 or greater?

I can’t tell you how to fix it because I don’t understand how it is supposed to work. It really makes no sense to me. What is i and why are you doing an analogRead of i?

ty_ger07:
The root of your problem is your logic.

Why is val an analogRead of i? What is i? When is val less than 550? Why only counter++ when val is less than 550? When is val 550 or greater? Why only display on LCD if val is 550 or greater?

I can't tell you how to fix it because I don't understand how it is supposed to work. It really makes no sense to me. What is i and why are you doing an analogRead of i?

here is what I'm trying to make:

I'm trying to make a smart parking by using photodiodes and IRleds when the val of the analog pin 0 (which is the first park) if the val of the park is less then 550 it means the IR light don't reach the photodiode ( park is taken)

val I supposed to tell me if the park is taken or not by the value that given by the photodiode
i is an integer that goes from 0 to 5 so it will analogread 0 1 2 3 4 5 and tell me which park is the closest by that and it will print on the lcd what park is the closest.

So, in succession, you are effectively doing:
val = analogRead(0);
val = analogRead(1);
val = analogRead(2);
val = analogRead(3);
val = analogRead(4);
val = analogRead(5);

You have photodiodes connected to pin 0, 1, 2, 3, 4, and 5? Is the LCD screen also connected to pin 2, 3, 4, 5, 6, and 7?
LiquidCrystal lcd( 2, 3, 4, 5, 6, 7);

Seems like an issue if you are sharing pins between reading the values from the photodiodes and using the same pin to output data to the LCD display.

ty_ger07:
You have photodiodes connected to pin 0, 1, 2, 3, 4, and 5?

They are connected to A0 to A5, I assume.

For analogRead 0 or A0 means the same thing.

Whandall:
They are connected to A0 to A5, I assume.

For analogRead 0 or A0 means the same thing.

yes they are.

After switching channels there could be some overtalking from the previous channel,
you could try to read each value twice and throw away the first value.

Whandall:
After switching channels there could be some overtalking from the previous channel,
you could try to read each value twice and throw away the first value.

sounds complicated, its my first work with Arduino, could you show me how to do it?

It only sounds complicated :wink:

  for (i = 0; i < 6; i++)
  {
    val = analogRead(i);
    val = analogRead(i);
    if (val < 550)

Whandall:
It only sounds complicated :wink:

  for (i = 0; i < 6; i++)

{
    val = analogRead(i);
    val = analogRead(i);
    if (val < 550)

welp doesn’t fix it, i also tried to add delay between both of them still don’t help

Then you probably need more debug output, like which values are read from which port.

It could be a bad connection reading taken when it is supposed to read free.

hmmm
seems like a have a bugged out Arduino i didn't cahgned anything and somewhy now it worked i was shocked then tried again didn't work but now it works again
i don't have any idea what is going on but thanks for helping??
lol

Whandall:
Then you probably need more debug output, like which values are read from which port.

It could be a bad connection reading taken when it is supposed to read free.

you mean pinMode ( a0, input) and so on?

edit while i cant post yet

seems like i have connection problems with the IRLED in the 4th parking

thank you again and have a good day. :slight_smile: