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);
}
}