const int ldrTopPin = A0;     // Analog pin for LDR positioned at the top
const int ldrRightPin = A1;   // Analog pin for LDR positioned on the right
const int ldrBottomPin = A2;  // Analog pin for LDR positioned at the bottom
const int ldrLeftPin = A3;    // Analog pin for LDR positioned on the left
const int voltagePin = A6;    // Analog pin for voltage measurement
const int distancePin = A7;   // Analog pin for distance measurement

unsigned long previousMillis = 0;  // Store the previous time
const long interval = 500;        // Define the interval in milliseconds

void setup() {
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();  // Get the current time

  // Check if it's time to perform the next reading
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a reading was taken
    previousMillis = currentMillis;
    // Read values from the four LDRs, voltage, and distance pins
    int luxTop = analogRead(ldrTopPin);
    int luxRight = analogRead(ldrRightPin);
    int luxBottom = analogRead(ldrBottomPin);
    int luxLeft = analogRead(ldrLeftPin);
    int voltageValue = analogRead(voltagePin);
    int distanceValue = analogRead(distancePin);

    // Calculate lux, voltage, and distance based on the analogRead values
    float luxTopNormalized = map(luxTop, 0, 1023, 0, 10000); // Adjust the mapping based on LDR characteristics
    float luxRightNormalized = map(luxRight, 0, 1023, 0, 10000); // Adjust the mapping based on LDR characteristics
    float luxBottomNormalized = map(luxBottom, 0, 1023, 0, 10000); // Adjust the mapping based on LDR characteristics
    float luxLeftNormalized = map(luxLeft, 0, 1023, 0, 10000); // Adjust the mapping based on LDR characteristics
    float voltage = map(voltageValue, 0, 1023, 0, 5); // Adjust the mapping based on voltage measurement
    float distance = map(distanceValue, 0, 1023, 0, 100); // Adjust the mapping based on distance measurement

    // Calculate the candela value using the formula for each direction
    float area          = 4 * PI * distance * distance; // Assuming a spherical area
    float candelaTop    = (luxTopNormalized * area) / (voltage * distance * distance);
    float candelaRight  = (luxRightNormalized * area) / (voltage * distance * distance);
    float candelaBottom = (luxBottomNormalized * area) / (voltage * distance * distance);
    float candelaLeft   = (luxLeftNormalized * area) / (voltage * distance * distance);

    float averagecandela = (candelaTop + candelaRight + candelaBottom + candelaLeft) / 4;

    // Determine the dominant direction
    String dominantDirection;
    if (candelaTop > candelaRight && candelaTop > candelaBottom && candelaTop > candelaLeft) {
      dominantDirection = "Top";
    } else if (candelaRight > candelaBottom && candelaRight > candelaLeft && candelaRight > candelaTop) {
      dominantDirection = "Right";
    } else if (candelaBottom > candelaLeft && candelaBottom > candelaRight && candelaBottom > candelaTop) {
      dominantDirection = "Bottom";
    } else if (candelaLeft > candelaTop && candelaLeft > candelaRight && candelaLeft > candelaBottom){
      dominantDirection = "Left";
    } else {
      dominantDirection = "nan";
    }

    // Print the calculated candela values and dominant direction
    Serial.print("Candela (Top): ");
    Serial.println(candelaTop);
    Serial.print("Candela (Right): ");
    Serial.println(candelaRight);
    Serial.print("Candela (Bottom): ");
    Serial.println(candelaBottom);
    Serial.print("Candela (Left): ");
    Serial.println(candelaLeft);
    Serial.print("Dominant Direction: ");
    Serial.println(dominantDirection);
    Serial.print("average :" );
    Serial.println(averagecandela);
  }
}
$abcdeabcde151015202530fghijfghij
$abcdeabcde151015202530fghijfghij
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
pot2:GND
pot2:SIG
pot2:VCC
pot1:GND
pot1:SIG
pot1:VCC
ldr1:VCC
ldr1:GND
ldr1:DO
ldr1:AO
ldr2:VCC
ldr2:GND
ldr2:DO
ldr2:AO
ldr3:VCC
ldr3:GND
ldr3:DO
ldr3:AO
ldr4:VCC
ldr4:GND
ldr4:DO
ldr4:AO