Problem reading multiple analog and led not turned on?

Hello, I am trying to do a project for school called smart parking, in this project I have to have 15 parking slots equipped with ldr and led where if there is a car is present on the slot, ldr will detect the changes and the green led will turn off signifying the space is not available anymore.

So now I have a problem with both ldr and led, I first tried this by connecting the LDR with a 6.8k resistor, at first I tried with only 4 ldrs and it worked fine, problem arise when I tried all 15 ldr with the same coding and resistor value and the reading goes crazy. I have tried the usual putting delay in between analogRead it doesn't react to changes in light at all. (My connection: 1 leg to supply, 1 leg to 6.8k resistor and analog pin)

For the leds, I used a 220ohm resistor, I am sincerely asking for help since this is my first time using arduino and I have no idea how to fix this, I guess the problem is my coding since things worked fine when i tested for only 1 ldr and 1 led? Much thanks everyone.

Sorry my coding my be abit messy

//the number of the LED pin
int ledPin1 = 2;  
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;  
int ledPin7 = 8;
int ledPin8 = 9;
int ledPin9 = 10;
int ledPin10 = 11;
int ledPin11 = 12;  
int ledPin12 = 13;
int ledPin13 = 22;
int ledPin14 = 24;
int ledPin15 = 26;

//the number of the LDR pin
 
 int ldrPin1 = 0;
 int ldrPin2 = 1;
 int ldrPin3 = 2;
 int ldrPin4 = 3;
 int ldrPin5 = 4; 
 int ldrPin6 = 5;
 int ldrPin7 = 6;
 int ldrPin8 = 7;
 int ldrPin9 = 8;
 int ldrPin10 = 9; 
 int ldrPin11 = 10;
 int ldrPin12 = 11;
 int ldrPin13 = 12;
 int ldrPin14 = 13;
 int ldrPin15 = 14; 


void setup() 
{
  Serial.begin(9600);
   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);  
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);  
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);  
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin14, OUTPUT);
  pinMode(ledPin15, OUTPUT);

  pinMode(ldrPin1, INPUT);
  pinMode(ldrPin2, INPUT);
  pinMode(ldrPin3, INPUT);
  pinMode(ldrPin4, INPUT);
  pinMode(ldrPin5, INPUT);
  pinMode(ldrPin6, INPUT);
  pinMode(ldrPin7, INPUT);
  pinMode(ldrPin8, INPUT);
  pinMode(ldrPin9, INPUT);
  pinMode(ldrPin10, INPUT);
  pinMode(ldrPin11, INPUT);
  pinMode(ldrPin12, INPUT);
  pinMode(ldrPin13, INPUT);
  pinMode(ldrPin14, INPUT);
  pinMode(ldrPin15, INPUT);

}

void loop()
{
  int slot1;
  int slot2;
  int slot3;
  int slot4;
  int slot5;
  int slot6;
  int slot7;
  int slot8;
  int slot9;
  int slot10;
  int slot11;
  int slot12;
  int slot13;
  int slot14;
  int slot15;
  
  //read the status of the LDR value
 
  int ldrStatus1 = analogRead(ldrPin1);   
  int ldrStatus2 = analogRead(ldrPin2);
  int ldrStatus3 = analogRead(ldrPin3);
  int ldrStatus4 = analogRead(ldrPin4);
  int ldrStatus5 = analogRead(ldrPin5);   
  int ldrStatus6 = analogRead(ldrPin6);
  int ldrStatus7 = analogRead(ldrPin7);
  int ldrStatus8 = analogRead(ldrPin8);
  int ldrStatus9 = analogRead(ldrPin9);   
  int ldrStatus10 = analogRead(ldrPin10);
  int ldrStatus11 = analogRead(ldrPin11);
  int ldrStatus12 = analogRead(ldrPin12);
  int ldrStatus13 = analogRead(ldrPin13);   
  int ldrStatus14 = analogRead(ldrPin14);
  int ldrStatus15 = analogRead(ldrPin15);

  
  int threshold = 300;

    //Availability of parking slot 1
    if (ldrStatus1 <=threshold) {
    digitalWrite(ledPin1, LOW); //turn LED on
    slot1 = 0; }
    else {
    digitalWrite(ledPin1, HIGH);            
    slot1 = 1;}

    //Availability of parking slot 2
    if (ldrStatus2 <=threshold) {
    digitalWrite(ledPin2, LOW);             
    slot2 = 0; }
    else {
    digitalWrite(ledPin2, HIGH);          
    slot2 = 1 ;}

    //Availability of parking slot 3
     if (ldrStatus3 <=threshold) {
    digitalWrite(ledPin3, LOW);               
    slot3 = 0 ; }
    else {
    digitalWrite(ledPin3, HIGH);          
    slot3 = 1 ; }
    
    //Availability of parking slot 4
    if (ldrStatus4 <=threshold) {
    digitalWrite(ledPin4, LOW);
    slot4 = 0; }
    else {
    digitalWrite(ledPin4, HIGH);          
    slot4 = 1; }

    //Availability of parking slot 5
    if (ldrStatus5 <=threshold) {
    digitalWrite(ledPin5, LOW);
    slot5 = 0; }
    else {
    digitalWrite(ledPin5, HIGH);          
    slot5 = 1; }

    //Availability of parking slot 6
    if (ldrStatus6 <=threshold) {
    digitalWrite(ledPin6, LOW); //turn LED on
    slot6 = 0; }
    else {
    digitalWrite(ledPin6, HIGH);            
    slot6 = 1;}

    //Availability of parking slot 7
    if (ldrStatus7 <=threshold) {
    digitalWrite(ledPin7, LOW);             
    slot7 = 0; }
    else {
    digitalWrite(ledPin7, HIGH);          
    slot7 = 1 ;}

    //Availability of parking slot 8
     if (ldrStatus8 <=threshold) {
    digitalWrite(ledPin8, LOW);               
    slot8 = 0 ; }
    else {
    digitalWrite(ledPin8, HIGH);          
    slot8 = 1 ; }
    
    //Availability of parking slot 9
    if (ldrStatus9 <=threshold) {
    digitalWrite(ledPin9, LOW);
    slot9 = 0; }
    else {
    digitalWrite(ledPin9, HIGH);          
    slot9 = 1; }

    //Availability of parking slot 10
    if (ldrStatus10 <=threshold) {
    digitalWrite(ledPin10, LOW);
    slot10 = 0; }
    else {
    digitalWrite(ledPin4, HIGH);          
    slot10 = 1; }

    //Availability of parking slot 11
    if (ldrStatus11 <=threshold) {
    digitalWrite(ledPin1, LOW); //turn LED on
    slot11 = 0; }
    else {
    digitalWrite(ledPin1, HIGH);            
    slot11 = 1;}

    //Availability of parking slot 12
    if (ldrStatus12 <=threshold) {
    digitalWrite(ledPin12, LOW);             
    slot12 = 0; }
    else {
    digitalWrite(ledPin12, HIGH);          
    slot12 = 1 ;}

    //Availability of parking slot 13
     if (ldrStatus13 <=threshold) {
    digitalWrite(ledPin13, LOW);               
    slot13 = 0 ; }
    else {
    digitalWrite(ledPin13, HIGH);          
    slot13 = 1 ; }
    
    //Availability of parking slot 14
    if (ldrStatus14 <=threshold) {
    digitalWrite(ledPin14, LOW);
    slot14 = 0; }
    else {
    digitalWrite(ledPin14, HIGH);          
    slot14 = 1; }

    //Availability of parking slot 15
    if (ldrStatus15 <=threshold) {
    digitalWrite(ledPin15, LOW);
    slot15 = 0; }
    else {
    digitalWrite(ledPin15, HIGH);          
    slot15 = 1; }

   
  
   int ParkingAvailable = slot1 + slot2 + slot3 + slot4 +slot5 + slot6 + slot7 + slot8 + slot9 +slot10 + slot11 + slot12 + slot13 + slot14 +slot15;

 Serial.println(ParkingAvailable);
 delay(1000); 

}

If you learn about arrays so you don't need all that repetitive code you could probably shorten your program by 75%.

Make a simple drawing showing how everything is connected and post a photo of the drawing.

Successive reading of different analog pins will probably give you errors because reading 2 will have some residue from reading 1. Try it like this

  int ldrStatus1 = analogRead(ldrPin1);   
  ldrStatus1 = analogRead(ldrPin1); 

  int ldrStatus2 = analogRead(ldrPin2);
  ldrStatus2 = analogRead(ldrPin2);

  int ldrStatus3 = analogRead(ldrPin3);
  ldrStatus3 = analogRead(ldrPin3);

so that each pin is read twice and the first reading is ignored

Better still do it using an array like this - 4 lines of code do ALL the pins

for (byte n = 0; n < numLDRs; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  ldrStatus[n] = analogRead(ldrPin[n]);
}

...R

Robin2:
If you learn about arrays so you don't need all that repetitive code you could probably shorten your program by 75%.

Make a simple drawing showing how everything is connected and post a photo of the drawing.

Successive reading of different analog pins will probably give you errors because reading 2 will have some residue from reading 1. Try it like this

  int ldrStatus1 = analogRead(ldrPin1);   

ldrStatus1 = analogRead(ldrPin1);

int ldrStatus2 = analogRead(ldrPin2);
  ldrStatus2 = analogRead(ldrPin2);

int ldrStatus3 = analogRead(ldrPin3);
  ldrStatus3 = analogRead(ldrPin3);



so that each pin is read twice and the first reading is ignored

Better still do it using an array like this - 4 lines of code do ALL the pins


for (byte n = 0; n < numLDRs; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  ldrStatus[n] = analogRead(ldrPin[n]);
}




...R

If I use array , may I know how I can count the number of available parking? I didn't use array because I encountered with an error earlier so I decided to try with this. Much thanks for your suggestion, I am trying to write an array now, and I will draw the circuit diagram later

Robin2:
If you learn about arrays so you don't need all that repetitive code you could probably shorten your program by 75%.

Make a simple drawing showing how everything is connected and post a photo of the drawing.

Successive reading of different analog pins will probably give you errors because reading 2 will have some residue from reading 1. Try it like this

  int ldrStatus1 = analogRead(ldrPin1);   

ldrStatus1 = analogRead(ldrPin1);

int ldrStatus2 = analogRead(ldrPin2);
  ldrStatus2 = analogRead(ldrPin2);

int ldrStatus3 = analogRead(ldrPin3);
  ldrStatus3 = analogRead(ldrPin3);



so that each pin is read twice and the first reading is ignored

Better still do it using an array like this - 4 lines of code do ALL the pins


for (byte n = 0; n < numLDRs; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  ldrStatus[n] = analogRead(ldrPin[n]);
}




...R

Robin2:
If you learn about arrays so you don't need all that repetitive code you could probably shorten your program by 75%.

Make a simple drawing showing how everything is connected and post a photo of the drawing.

Successive reading of different analog pins will probably give you errors because reading 2 will have some residue from reading 1. Try it like this

  int ldrStatus1 = analogRead(ldrPin1);   

ldrStatus1 = analogRead(ldrPin1);

int ldrStatus2 = analogRead(ldrPin2);
  ldrStatus2 = analogRead(ldrPin2);

int ldrStatus3 = analogRead(ldrPin3);
  ldrStatus3 = analogRead(ldrPin3);



so that each pin is read twice and the first reading is ignored

Better still do it using an array like this - 4 lines of code do ALL the pins


for (byte n = 0; n < numLDRs; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  ldrStatus[n] = analogRead(ldrPin[n]);
}




...R

Hello, I revised my coding and this is what I came with, is this right? But it said ldrstatus not declared? I'm sorry i'm really bad at this

int ldrPin[] = {0,1,2,3};

int ledPin[] = {2,3,4,5};

void setup() {

Serial.begin(9600);

for(int index=0; index<16 ; index++)
{
//declare LED as output
pinMode(ledPin[index],OUTPUT);

//declare LDR as input
pinMode(ldrPin[index],INPUT);
}
}

void loop() {

int slot1,slot2,slot3,slot4;

for (byte n = 0; n < 16; n++) {
ldrStatus[n] = analogRead(ldrPin[n]);
ldrStatus[n] = analogRead(ldrPin[n]);

//Availability of parking slot 1
if (ldrStatus[0] >=300) {
digitalWrite(ledPin[0], HIGH); //turn LED on
slot1 = 1; }
else {
digitalWrite(ledPin1, LOW);
slot1 = 0;}

}

Please use the Code button </> rather than the Quote button for code.

You have declared the arrays ldrPin[] and ledPin[] but not the array ldrStatus[]

You seem to be using pins 2 and 3 both for LDRs and for LEDs - I suspect that is not correct.

Don't use pins 0 and 1 as they are used by Serial.

...R

But it said ldrstatus not declared?

Well, it's right. You did NOT declare an array called ldrstatus (or ldrStatus, as the code REALLY says).

You need to engage your gray matter, not just your fingers.

You also have another problem in your code; I just pulled two pins as an example

...
int ledPin1 = 2;
...
...
int ldrPin3 = 2;
...
...

void setup()
{
  ...
  pinMode(ledPin1, OUTPUT);
  ...
  ...
  pinMode(ldrPin3, INPUT);
  ...
  ...
}

void loop()
{
  ...
  ...
}

What will pin 2 be after the above code, an input or an output? What do you want it to be?

sterretje:
You also have another problem in your code; I just pulled two pins as an example

...

int ledPin1 = 2;
...
...
int ldrPin3 = 2;
...
...

void setup()
{
 ...
 pinMode(ledPin1, OUTPUT);
 ...
 ...
 pinMode(ldrPin3, INPUT);
 ...
 ...
}

void loop()
{
 ...
 ...
}



What will pin 2 be after the above code, an input or an output? What do you want it to be?

sterretje:
You also have another problem in your code; I just pulled two pins as an example

...

int ledPin1 = 2;
...
...
int ldrPin3 = 2;
...
...

void setup()
{
 ...
 pinMode(ledPin1, OUTPUT);
 ...
 ...
 pinMode(ldrPin3, INPUT);
 ...
 ...
}

void loop()
{
 ...
 ...
}



What will pin 2 be after the above code, an input or an output? What do you want it to be?

Oh sorry my bad, I thought it would automatically recognize it as analog input if I use analogRead command. I revised my codes can you guys help take a look? God I am sorry even my basic is not good but I am trying anyway lol

int ldrPin[] = {A0,A1,A2,A3};

int ledPin[] = {2,3,4,5};

int ldrStatus[] = {0,1,2,3};

void setup() {

  Serial.begin(9600);

  for(int n=0; n<5 ; n++)
  {
    //declare LED as output
    pinMode(ledPin[n],OUTPUT); 

    //declare LDR as input
    pinMode(ldrPin[n],INPUT); 
  }
}

void loop() {

  int slot1,slot2,slot3,slot4;

  for (int n = 0; n < 5; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  //ldrStatus[n] = analogRead(ldrPin[n]);

    //Availability of parking slot 1
    if (ldrStatus[0] >=300) {
    digitalWrite(ledPin[0], HIGH); //turn LED on
    slot1 = 1; }
    else {
    digitalWrite(ledPin[0], LOW);            
    slot1 = 0;}

    //Availability of parking slot 2
    if (ldrStatus[1] >=300) {
    digitalWrite(ledPin[1], HIGH); //turn LED on
    slot2 = 1; }
    else {
    digitalWrite(ledPin[1], LOW);            
    slot2 = 0;}

    //Availability of parking slot 3
    if (ldrStatus[2] >=300) {
    digitalWrite(ledPin[2], HIGH); //turn LED on
    slot3 = 1; }
    else {
    digitalWrite(ledPin[2], LOW);            
    slot3 = 0;}

    //Availability of parking slot 4
    if (ldrStatus[3] >=300) {
    digitalWrite(ledPin[3], HIGH); //turn LED on
    slot4 = 1; }
    else {
    digitalWrite(ledPin[3], LOW);            
    slot4 = 0;}

 int ParkingAvailable = slot1 + slot2 + slot3 + slot4;

 Serial.println(ParkingAvailable);
 delay(1000);
 
 }}

update: it worked! but I need expert opinions on this one, is it ok? the problem is the LDR on A0 is not giving any reading? If the code is okay then I shall check my connection, thanks to everyone that helped!

    //declare LDR as input
    pinMode(ldrPin[n],INPUT);

Analog pins are input only. You are setting the mode of the digital pin that shares the same physical location as the analog pin. That may not be a good thing.

  int slot1,slot2,slot3,slot4;

I see that you understood the purpose of arrays...

  for (int n = 0; n < 5; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  //ldrStatus[n] = analogRead(ldrPin[n]);

The for loop will iterate for n = 0, 1, 2, 3, and 4. There are NOT 5 elements in your arrays. You just crapped on memory you don't own. All bets are off as to what the code will do when you do that.

If you had grasped the reason for using arrays, and had used them everywhere, the last 4 blocks of code would have been one block in a for loop.

but I need expert opinions on this one, is it ok?

In my opinion, no.

sscloudstrifey:
but I need expert opinions on this one, is it ok?

I certainly don't claim to be an expert but the first thing that strikes me (especially as you have been advised to use arrays to avoid duplicate code) is why you have duplicate code like this for each slot

    //Availability of parking slot 1
    if (ldrStatus[0] >=300) {

...R

Robin2:
I certainly don't claim to be an expert but the first thing that strikes me (especially as you have been advised to use arrays to avoid duplicate code) is why you have duplicate code like this for each slot

    //Availability of parking slot 1

if (ldrStatus[0] >=300) {




...R

PaulS:

    //declare LDR as input

pinMode(ldrPin[n],INPUT);



Analog pins are input only. You are setting the mode of the digital pin that shares the same physical location as the analog pin. That may not be a good thing.

Okay I revised my code, and I think I get the hang of it now, thank you for suggesting array and keeping up with a beginner like me. Much thanks! Even the leds work fine now :slight_smile:

int ldrPin[] = {A0,A1,A2,A3};
int ledPin[] = {2,3,4,5};
int ldrStatus[] = {0,1,2,3};

void setup() {

  Serial.begin(9600);

  for(int n=0; n<4 ; n++)
  {
    //declare LED as output
    pinMode(ledPin[n],OUTPUT); 

    //declare LDR as input
    pinMode(ldrPin[n],INPUT); 
  }
}

void loop() {

  int slot[14];

  for (byte n = 0; n<4 ; n++) {
  ldrStatus[n] = analogRead(ldrPin[n]);
  ldrStatus[n] = analogRead(ldrPin[n]);

    //Availability of parking slots
    if (ldrStatus[n] >=300) {
    digitalWrite(ledPin[n], HIGH); //turn LED on
    slot[n] = 1; }
    else {
    digitalWrite(ledPin[n], LOW);            
    slot[n] = 0;}
  }
  
    int ParkingAvailable = 0;
    for (int i = 0; i < 4; i++)
        ParkingAvailable = ParkingAvailable + slot[i];

 Serial.println(ParkingAvailable);
 delay(1000);
 
 }
int ldrPin[] = {A0, A1, A2, A3};
int ledPin[] = {2, 3, 4, 5};

Why int and not byte and why not const for the pin numbers as they will not change within the program ?

A suggestion. Create a variable to hold the number of elements in the ledPin array using sizeof() and use that variable wherever the array size if referenced in the program. Easier to maintain and less error prone. See later comment about the size of the slot array.

    //declare LDR as input
    pinMode(ldrPin[n],INPUT);

As has been pointed out you don't need, to do this if you are going to use the pins as analogue inputs.

int slot[14];

Why 14 ? Why int not byte ?

UKHeliBob:

int ldrPin[] = {A0, A1, A2, A3};

int ledPin[] = {2, 3, 4, 5};



Why int and not byte and why not const for the pin numbers as they will not change within the program ?

A suggestion. Create a variable to hold the number of elements in the ledPin array using sizeof() and use that variable wherever the array size if referenced in the program. Easier to maintain and less error prone. See later comment about the size of the slot array.

I see, I did not know the function of const earlier so I decided not to use it, will revise my code.

As been pointed out you don't need, to do this if you are going to use the pins as analogue inputs.

Noted, removed as I see now every pins is regarded as inputs unless stated is it?

int slot[14];

Why 14 ? Why int not byte ?

Because I did not know, lol thank you for ur suggestions! I learned a lot :slight_smile: