number 4 comes out of nowhere

hi, for whatever reason I get the value 4 in the integer “i” from nothing, cant figure out what is the problem, I thought its physical problem but I’m pretty sure its in the code (to me it looks fine and I don’t see a reason for it to happen)

int i = 0, counter = 1, val;
byte photodiodes[6] = {A0, A1, A2, A3, 12, 11};
#define GreenLed 9
#define RedLed 10

#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd( 2, 3, 4, 5, 6, 7);
Servo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(8);
  lcd.begin(16, 2);
}

void loop() {
  lcd.setCursor(0, 1);
  counter = 1;
  Serial.println(i);
  for (i = 0; i < 6; i++)
  {
    val = digitalRead(photodiodes[i]);
    if (val == LOW) {
      counter++;
    }
    else
    {
      digitalWrite(RedLed, 0);
      digitalWrite(GreenLed, 1);

      myServo.write(90);
      lcd.clear();
      lcd.print("Parking bay ");
      lcd.print(counter);
      lcd.setCursor(0, 2);
      lcd.print("is available");
      delay(200);
      break;
    }

    if (counter == 6)
    {
      lcd.clear();
      lcd.print("The parking lot");
      lcd.setCursor(0, 2);
      lcd.print("is full");
      delay(200);
      digitalWrite(RedLed, 1);
      digitalWrite(GreenLed, 0);

      myServo.write(0);
      break;
    }
  }
}

also, the 6th pin from the array doesn’t do anything (even when I change the order of the numbers in the array)

Edit removed post

BulldogLowell:
Edit removed post

what removed post?

Jumping around and starting a new post is pointless.

There are two break statements in your for loop. If either of the conditions that contain those break statements is true on the fifth iteration of the for loop then i will be 4 when printed. Most likely this is occurring because val is LOW through the loops so counter increments each time and so when counter == 6 i will be 4:
i counter
0 2
1 3
2 4
3 5
4 6

keep in mind a counter value of 6 will be reached in a matter of microseconds.

pert:
There are two break statements in your for loop. If either of the conditions that contain those break statements is true on the fifth iteration of the for loop then i will be 4 when printed. Most likely this is occurring because val is LOW through the loops so counter increments each time and so when counter == 6 i will be 4:
i counter
0 2
1 3
2 4
3 5
4 6

keep in mind a counter value of 6 will be reached in a matter of microseconds.

hmmm, from what I understood is that I need to get rid of the break statements, now, I tried that and the 6th value in the array works but now the screen refreshes all the time and when there is an empty parking the RGB flashing between red and green and the screen flashing between the parking is full and the empty space.
this is why I placed the break statement in the first place. any idea how to fix this without causing problems like the break;? I tried longer delay but it doesn’t help.

alonkvetny1:
I tried longer delay but it doesn’t help.

I’d try:

  1. a functional approach
  2. only update the display when the first available spot is changed or the lot is full

like this:

void loop() 
{
  static int savedFirstSpot = -99;
  const int firstAvailableSpot = getFirstAvailableSpot();

  if(firstAvailableSpot != savedFirstSpot)
  {
    if(firstAvailableSpot == -1)
    {
      displayFullLot();
    }
    else
    {
      diaplayFirstAvailableSpot(firstAvailableSpot);
    }
    savedFirstSpot = firstAvailableSpot;
  }
}

void displayFullLot(void)
{
  lcd.clear();
  lcd.print("The parking lot");
  lcd.setCursor(0, 2);
  lcd.print("is full");
  //delay(200); // edit commented this out, it is not necessary
  digitalWrite(RedLed, 1);
  digitalWrite(GreenLed, 0);
  myServo.write(0);
}

void diaplayFirstAvailableSpot(int spot)
{
  digitalWrite(RedLed, 0);
  digitalWrite(GreenLed, 1);
  myServo.write(90);
  lcd.clear();
  lcd.print("Parking bay ");
  lcd.print(spot);
  lcd.setCursor(0, 2);
  lcd.print("is available");
}

int getFirstAvailableSpot()
{
  for(int i = 0; i < sizeof(photodiodes)/sizeof(photodiodes[0]); i++)
  {
    if(!digitalRead(photodiodes[i]))
    {
      return i;
    }
    return -1;
  }
}

BulldogLowell:
I’d try:

  1. a functional approach
  2. only update the display when the first available spot is changed or the lot is full

like this:

void loop() 

{
  static int savedFirstSpot = -99;
  const int firstAvailableSpot = getFirstAvailableSpot();

if(firstAvailableSpot != savedFirstSpot)
  {
    if(firstAvailableSpot == -1)
    {
      displayFullLot();
    }
    else
    {
      diaplayFirstAvailableSpot(firstAvailableSpot);
    }
    savedFirstSpot = firstAvailableSpot;
  }
}

void displayFullLot(void)
{
  lcd.clear();
  lcd.print(“The parking lot”);
  lcd.setCursor(0, 2);
  lcd.print(“is full”);
  //delay(200); // edit commented this out, it is not necessary
  digitalWrite(RedLed, 1);
  digitalWrite(GreenLed, 0);
  myServo.write(0);
}

void diaplayFirstAvailableSpot(int spot)
{
  digitalWrite(RedLed, 0);
  digitalWrite(GreenLed, 1);
  myServo.write(90);
  lcd.clear();
  lcd.print("Parking bay ");
  lcd.print(spot);
  lcd.setCursor(0, 2);
  lcd.print(“is available”);
}

int getFirstAvailableSpot()
{
  for(int i = 0; i < sizeof(photodiodes)/sizeof(photodiodes[0]); i++)
  {
    if(!digitalRead(photodiodes[i]))
    {
      return i;
    }
    return -1;
  }
}

wow, this looks too much complicated and I don’t even know what most of those lines means. and I’m not sure I’m can use this because of this advanced (for me) code,

where can I use break;? (without causing problems)
also, why is it saying that the park is full if the counter needs to get to 6 but if there is a empty park it will not reach 6

alonkvetny1:
wow, this looks too much complicated and I don't even know what most of those lines means. and I'm not sure I'm can use this because of this advanced (for me) code

hmmmm.... most of it (read "all of it" ) was just refactored from your code.

Maybe spend some time in the tutorials to get yourself familiar with the basics

Or, try to just read the code I posted, most of it is VERY straightforward.

BulldogLowell:
hmmmm… most of it (read “all of it” ) was just refactored from your code.

Maybe spend some time in the tutorials to get yourself familiar with the basics

Or, try to just read the code I posted, most of it is VERY straightforward.

I didn’t meant it but anyway,
I did not understood this lines: do I need to change those to something or? sorry for the dumb questions but I have never code before but I would like to learn!

displayFullLot();

getFirstAvailableSpot();

diaplayFirstAvailableSpot(firstAvailableSpot);

void displayFullLot(void)

void diaplayFirstAvailableSpot(int spot)

int getFirstAvailableSpot()
{
  for(int i = 0; i < sizeof(photodiodes)/sizeof(photodiodes[0]); i++)
  {
    if(!digitalRead(photodiodes[i]))
    {
      return i;
    }
    return -1;
  }
}

a function call:

getFirstAvailableSpot();

the definition of that function:

int getFirstAvailableSpot()
{
  for(int i = 0; i < sizeof(photodiodes)/sizeof(photodiodes[0]); i++)
  {
    if(!digitalRead(photodiodes[i]))
    {
      return i;
    }
    return -1;
  }
}