Automatic Lighting System

I'm relatively new to programming and I'm trying to get light sensors to activate an array of lights. The idea is to have 4 LDR (Light Dependent Resistors) capture light, and if any 3 out of the 4 LDRs are activated, then it will set off the array.

I keep running into an issue with the third sensor (buttonState3) routinely fails in the circuit.

In the MULTI SENSOR SWITCHING section.

When the third sensor (buttonState3) is used to activate the array the voltage is low. If the array is activated using the other 3 sensors then the array voltage is nominal. The problem is all in the program, because I switched the analog inputs for the third(A3) and forth(A4) sensors to the same result. The LDR in place for the third senor (buttonState3) would still cause a low voltage. Furthermore, using senor 3's LDR(A3) would still allow the forth sensor (buttonState4) to operate normally.

The program is attached, along with photographs of the test board and wiring diagram. The diagram does not include the additional LEDs for the individual INPUTS. They are only there for test-bench purposes. Each LED is sent to ground via a 220ohm resistor. I have omitted parts of the program that are irrelevant to shorten the post.

Due to file size restrictions the photos are on a google drive folder here:
https://drive.google.com/drive/folders/1uHbRg69BOpC7-nKW2-xNQy3I7CWcaBNX?usp=sharing

// MULTI SENSOR SWITCHING

  //OUTPUTS

const int LDR1 = 7;

const int LDR2 = 9;

const int LDR3 = 11;

const int LDR4 = 13;
  //INPUTS
const int LDR1IN = 6;
const int LDR2IN = 8;
const int LDR3IN = 10;
const int LDR4IN = 12;
  // INPUT READS
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;




//EXTERNAL POWER AND DISPLAY


const int HD_SW = 5;
const int PWR_ON = 4;
const int HD_ON = 3; //TEST LED


//LIGHT DEPENDENT RESISTEOR LEVELS


const int SENSOR1 = 0;
const int SENSOR2 = 0;
const int SENSOR3 = 0;
const int SENSOR4 = 0;
int val = 0;
int old_val = 0;
int LDRA1 = 0;
int LDRA2 = 0;
int LDRA3 = 0;
int LDRA4 = 0;




void setup() {
  Serial.begin(9600);
  
  pinMode(LDR1, OUTPUT);
  pinMode(LDR2, OUTPUT);
  pinMode(LDR3, OUTPUT);
  pinMode(LDR4, OUTPUT);
  pinMode(LDR1IN, INPUT);
  pinMode(LDR2IN, INPUT);
  pinMode(LDR3IN, INPUT);
  pinMode(LDR4IN, INPUT);
  
  pinMode(HD_ON, OUTPUT);
  pinMode(PWR_ON, OUTPUT);
  pinMode(HD_SW, OUTPUT); 
}


void loop() {
// MULTI SENSOR SWITCHING  


   buttonState1 = digitalRead(LDR1IN);
   buttonState2 = digitalRead(LDR2IN);
   buttonState3 = digitalRead(LDR3IN);
   buttonState4 = digitalRead(LDR4IN);
   
  if ((buttonState1 == HIGH) && (buttonState2 == HIGH) && (buttonState3 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  if ((buttonState2 == HIGH) && (buttonState3 == HIGH) && (buttonState4 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  if ((buttonState3 == HIGH) && (buttonState4 == HIGH) && (buttonState1 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  if ((buttonState4 == HIGH) && (buttonState1 == HIGH) && (buttonState2 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  } else {
    digitalWrite(HD_ON, LOW); //if none of the above statements are met the LED will default to LOW
  }
  
}

When you say the voltage at the output is low, you probably mean that the light intensity is low. This will be due to the logic of switching the lights on, then off again in rapid succession.

Your if/then/else statement group has to be reworked to deliver a single result (on or off), not on then, a statement later, off.

Edit

I guess I’d do it with a counter when setting the button states. If the counter is 3 (or more) then ON, else off.

When any of the inputs becomes HIGH add 1 to a counter
When any of the inputs becomes LOW subtract 1 from a counter

If the counter is 3 or above then turn on the light

  if ((buttonState4 == HIGH) && (buttonState1 == HIGH) && (buttonState2 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  } else {
    digitalWrite(HD_ON, LOW); //if none of the above statements are met the LED will default to LOW
  }

you've code has 3 distinct if statements and suggests that the code expects only one to be true.

but the last has an else. so the else condition in the last if can be true as well any of the other 2 if conditions. so while one of the if conditions may set HD_ON HIGH, the else may set it LOW

i think the code would do as you intend if you used if/else if/else

of course all the if conditions can be true if all the buttons are HIGH.

Thank you for the help. I knew that I was missing something simple. Here is the corrected code and evrything works perfectly.

// MULTI SENSOR SWITCHING
  //OUTPUTS
const int LDR1 = 7;
const int LDR2 = 9;
const int LDR3 = 11;
const int LDR4 = 13;
  //INPUTS
const int LDR1IN = 6;
const int LDR2IN = 8;
const int LDR3IN = 10;
const int LDR4IN = 12;
  // INPUT READS
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;


//EXTERNAL POWER AND DISPLAY

const int HD_SW = 5;
const int PWR_ON = 4;
const int HD_ON = 3; //TEST LED

//LIGHT DEPENDENT RESISTEOR LEVELS

const int SENSOR1 = 0;
const int SENSOR2 = 0;
const int SENSOR3 = 0;
const int SENSOR4 = 0;
int val = 0;
int old_val = 0;
int LDRA1 = 0;
int LDRA2 = 0;
int LDRA3 = 0;
int LDRA4 = 0;


void setup() {
  Serial.begin(9600);
  
  pinMode(LDR1, OUTPUT);
  pinMode(LDR2, OUTPUT);
  pinMode(LDR3, OUTPUT);
  pinMode(LDR4, OUTPUT);
  pinMode(LDR1IN, INPUT);
  pinMode(LDR2IN, INPUT);
  pinMode(LDR3IN, INPUT);
  pinMode(LDR4IN, INPUT);
  
  pinMode(HD_ON, OUTPUT);
  pinMode(PWR_ON, OUTPUT);
  pinMode(HD_SW, OUTPUT); 
}

void loop() {

//LIGHT DEPENDENT RESISTER LEVELS  
  
  LDRA1 = analogRead(A1);

  val = analogRead(SENSOR1); //read the value from the sensor

  Serial.println(val); //print the value to serial port

  if(LDRA1 < 250){
  
    digitalWrite(LDR1, HIGH);
  }
  if(LDRA1 > 250){
    digitalWrite(LDR1, LOW);
  } 
   LDRA2 = analogRead(A2);

  val = analogRead(SENSOR2); //read the value from the sensor


  if(LDRA2 < 250){
  
    digitalWrite(LDR2, HIGH);
  }
  if(LDRA2 > 250){
    digitalWrite(LDR2, LOW);
  }
  
    LDRA3 = analogRead(A3);

  val = analogRead(SENSOR3); //read the value from the sensor

  if(LDRA3 < 250){
  
    digitalWrite(LDR3, HIGH);
  }
  if(LDRA3 > 250){
    digitalWrite(LDR3, LOW);
  }
  
   LDRA4 = analogRead(A4);

  val = analogRead(SENSOR4); //read the value from the sensor
  
  if(LDRA4 < 250){
  
    digitalWrite(LDR4, HIGH);
  }
  if(LDRA4 > 250){
    digitalWrite(LDR4, LOW);
  }
 
// MULTI SENSOR SWITCHING  

   buttonState1 = digitalRead(LDR1IN);
   buttonState2 = digitalRead(LDR2IN);
   buttonState3 = digitalRead(LDR3IN);
   buttonState4 = digitalRead(LDR4IN);
   
  if ((buttonState1 == HIGH) && (buttonState2 == HIGH) && (buttonState3 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  else if ((buttonState2 == HIGH) && (buttonState3 == HIGH) && (buttonState4 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  else if ((buttonState3 == HIGH) && (buttonState4 == HIGH) && (buttonState1 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  }
  else if ((buttonState4 == HIGH) && (buttonState1 == HIGH) && (buttonState2 == HIGH)){
    digitalWrite(HD_ON, HIGH);
  } else {
    digitalWrite(HD_ON, LOW);
  }


}

Good that it works. For a more compact solution, in the future, look at using arrays to minimise repetition of a lot of similar code.