how to make a photocell read each second

Hi. Basically I have a circuit where I have a photocell and a led. If the photocell reads more than certain amount of light it turns on, otherwise it doesn´t. This works just fine, the problem is thought that the photocell only reads the amount of light one time, but I need it to read it constantly each second:

int sensorPin = 0;
int sensorValue = 0;
int LEDpin = 13;

void setup() {

Serial.begin(9600);
pinMode(LEDpin, OUTPUT);

}

void loop() {

sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(2000);
if (sensorValue > 1000)
{
digitalWrite(LEDpin, HIGH);
}
else
{
digitalWrite(LEDpin, LOW);
}

}

but I need it to read it constantly each second:

Then, you should not be delaying for TWO seconds on each pass through loop.

The sensorValue variable will contain a value between 0 and 1023. Limiting the range that triggers the LED to 1001 to 1023 just might be too restrictive.

If you remove the delays in your loop it will actually read the analog value thousands of times a second.

hi thanks it solved it :slight_smile:

If you're tracking/logging data then it might make more sense to record the times when the state changes. You will get more event-accuracy and maybe need to record less actual data.