You can write code to sense when an Light Dependent Resistor (what I assume that you mean by photocell) changes from light to dark or light to dark so that you can initiate an action only when the light changes from one state to the other (detect the transition, not the level). For instance here is a demo that will increase the count only when the light changes from dark to light. The state of the LED on pin 13 will toggle when the light changes from light to dark. The same mechanism is used to detect state changes in switches.
A tutorial on state change.
// analog state change by groundFungus aka c.goulding
const byte sensorPin = A0;
const byte ledPin = 13;
const int threshold = 600;
// hysteresis added to prevent chatter around threshold with
// slow moving signals
const int hysteresis = 25;
bool sensorState = true;
bool lastSensorState = true;
int count = 0;
void setup()
{
Serial.begin(115200);
Serial.println("analog state change example");
pinMode(sensorPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
if (sensorValue < threshold - hysteresis)
{
sensorState = true;
}
else if (sensorValue > threshold + hysteresis)
{
sensorState = false;
}
if (sensorState != lastSensorState)
{
if (sensorState == true)
{
count++;
Serial.print("count = ");
Serial.println(count);
}
else
{
digitalWrite(ledPin, !digitalRead(ledPin));
}
lastSensorState = sensorState;
}
}
LDR wired to ground. Load resistor is internal pullup.
Hope you find this useful.