Controlling LED with LDR sensor

Hi, :slight_smile:
I'm trying to control 4x4 LED with 4 LDR in a box. the average resistance will use for analog input to arduino (the resistance converted to voltage first). i hope PWM as the result will drive the LED (converted to analog voltage first). here's the program. but it still didn't work. can you tell me what is wrong with this? pleease =(

float V1,V2,V3,V4;
float luxAverage;
float luxLDR1, luxLDR2, luxLDR3, luxLDR4;
float Isensor;
float Upid;
float Isp;
float m;
float e1, e2, e3;

/********************************************************
 * PID Adaptive Tuning Example
 * One of the benefits of the PID library is that you can
 * change the tuning parameters at any time.  this can be
 * helpful if we want the controller to be agressive at some
 * times, and conservative at others.   in the example below
 * we set the controller to use Conservative Tuning Parameters
 * when we're near setpoint and more agressive Tuning
 * Parameters when we're farther away.
 ********************************************************/

//Define Variables we'll be connecting to
double Setpoint, Input;

//Define the aggressive and conservative Tuning Parameters
double Kp=0.1, Ki=0.3;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
    //initialize the variables we're linked to
  Input = luxAverage;
  Setpoint = Isp;
}
void loop() {
  int sensorValue1 = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage1 = sensorValue1 * (5.0 / 1023.0);
  
V1 = voltage1;
luxLDR1 = V1;
luxLDR1 = map(luxLDR1, 0,5.0, 0,170);

  int sensorValue2 = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage2 = sensorValue2 * (5.0 / 1023.0);

V2 = voltage2;
luxLDR2 = V2;
luxLDR2 = map(luxLDR2, 0,5.0, 0,170);

  int sensorValue3 = analogRead(A2);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage3 = sensorValue3 * (5.0 / 1023.0);

V3 = voltage3;
luxLDR3 = V3;
luxLDR3 = map(luxLDR3, 0,5.0, 0,170);

  int sensorValue4 = analogRead(A3);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage4 = sensorValue4 * (5.0 / 1023.0);

V4 = voltage4;
luxLDR4 = V4;
luxLDR4 = map(luxLDR4, 0,5.0,  0,170);

Isp = 170;
luxAverage = (luxLDR1+luxLDR2+luxLDR3+luxLDR4)/4;

  e1= abs (Isp-luxAverage);
  e2=e1;
  e3=e2;
Upid = Kp*(e1)+Ki*(e1+e2);

float PWMout;
int outputValue;

  // read the analog in value:
  PWMout = Upid;            
  // map it to the range of the analog out:
  outputValue = map(PWMout, 0, 170, 0, 255);  
  // change the analog out value:

analogWrite(9, outputValue);
Serial.println (outputValue);
delay(1000);

}