const int sensorPin = 13;
const int ledPin = 23;
int lightInit; // initial value
int lightVal; // light reading
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(ledPin, OUTPUT);
lightInit = analogRead(sensorPin);
}
void loop() {
lightVal = analogRead(sensorPin); // read the current light levels
//if lightVal is less than our initial reading withing a threshold then it is dark.
if(lightVal - lightInit < 50)
{
digitalWrite (ledPin, HIGH); // turn on light
}
//otherwise, it is bright
else
{
digitalWrite (ledPin, LOW); // turn off light
}
}