I am trying to reset calibration for 4 photoresistors for varying sunlight throughout the day
I am calibrating 4 photoresistors for a solar tracking application.
I want the photoresistors to continuously be calibrated from sunrise to sunset.
Once the photoresistors are properly calibrated then they can properly track the sun.
I’m using an esp32 which has a 12 bit ADC, although people say it’s noisy.
I am using the Arduino Calibration example:
If I understood it correctly the void setup() function calibrates only once.
The values it will calibrate for in the morning when the sun rises would not be correct for noon, afternoon, etc correct?
The way my code differs is that I put the void setup() portion of the code in the calibration example into my void loop() function
From Arduino calibration example:
// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
Can anyone give feedback if my thought process is correct or if my code is correct?
I have a sample serial output below of my code.
I’m wondering if there will be a conflict between the while millis() < 5000 portion of the code and delay(2000) portion of the code.
My calibration code:
//top left photoresistor sensor pin = PinTopLeft
const uint16_t PinTopLeft = 33;
const uint16_t PinBottomLeft = 35;
const uint16_t PinTopRight = 32;
const uint16_t PinBottomRight = 34;
// The sensor value
uint16_t TopLeft = 0;
uint16_t BottomLeft = 0;
uint16_t TopRight = 0;
uint16_t BottomRight = 0;
// As photoresistor approaches minimum sensor value more light is seen by it
uint16_t MinTopLeft = 4096;
uint16_t MinBottomLeft = 4096;
uint16_t MinTopRight = 4096;
uint16_t MinBottomRight = 4096;
uint16_t MaxTopLeft = 0;
uint16_t MaxBottomLeft = 0;
uint16_t MaxTopRight = 0;
uint16_t MaxBottomRight = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
// Using example from: https://www.arduino.cc/en/tutorial/calibration
// Calibrate during the first five seconds
while (millis() < 5000) {
TopLeft = analogRead(PinTopLeft);
BottomLeft = analogRead(PinBottomLeft);
TopRight = analogRead(PinTopRight);
BottomRight = analogRead(PinBottomRight);
// Record the maximum sensor value
if (TopLeft > MaxTopLeft) {
MaxTopLeft = TopLeft;
}
if (BottomLeft > MaxBottomLeft) {
MaxBottomLeft = BottomLeft;
}
if (TopRight > MaxTopRight) {
MaxTopRight = TopRight;
}
if (BottomRight > MaxBottomRight) {
MaxBottomRight = BottomRight;
}
// Record the minimum sensor value
if (TopLeft < MinTopLeft) {
MinTopLeft = TopLeft;
}
if (BottomLeft < MinBottomLeft) {
MinBottomLeft = BottomLeft;
}
if (TopRight < MinTopRight) {
MinTopRight = TopRight;
}
if (BottomRight < MinBottomRight) {
MinBottomRight = BottomRight;
}
}
// Read the sensor
TopLeft = analogRead(PinTopLeft); // Top left sensor
BottomLeft = analogRead(PinBottomLeft);
TopRight = analogRead(PinTopRight);
BottomRight = analogRead(PinBottomRight);
// Apply the calibration to the sensor reading
TopLeft = map(TopLeft, MinTopLeft, MaxTopLeft, 0, 4000);
BottomLeft = map(BottomLeft, MinBottomLeft, MaxBottomLeft, 0, 4000);
TopRight = map(TopRight, MinTopRight, MaxTopRight, 0, 4000);
BottomRight = map(BottomRight, MinBottomRight, MaxBottomRight, 0, 4000);
// In case the sensor value is outside the range seen during calibration
TopLeft = constrain(TopLeft, 0, 4000);
BottomLeft = constrain(BottomLeft, 0, 4000);
TopRight = constrain(TopRight, 0, 4000);
BottomRight = constrain(BottomRight, 0, 4000);
//Sends analog values in this format: i.e. {380,148,224,260}
uint16_t data[4] = {TopLeft, BottomLeft, TopRight, BottomRight};
uint16_t i;
for (i = 0; i < 4; i++) {
Serial.print(data[i]);
Serial.println(" ");
}
Serial.println(" ");
delay(2000);
}
The following results are from a 2700k, 850 lumen CFL bulb covered with a lamp shade.
The photoresistors are lying flat on a breadboard that is about a meter away from the lamp and it is night time.
Sample serial output:
731
659
830
691
733
667
831
696
739
667
837
698
733
665
835
696