Need advice on logic...TIA

Hey everyone,

This is what my goal is:
The photocell I'm using will be reading the values of the light around it at all times.
A bright light will be introduced at a random time whose peak value from the photocell is a trigger for a dc motor to turn ON.
The bright light is removed but the dc motor remains ON.
Once the bright light is re-introduced, the dc motor will turn OFF.

I can get the motor to turn ON if the threshold is reached. I can get the motor to stay ON once the light is removed. I can't quite get the motor to turn back off since its the initial condition that started the operation. I've written a few programs with arduino and haven't gone beyond the if and while statements. If anyone could point me in the right direction, I would appreciate it!

It would help to post your code. You could have a variable called toggle.
if (light is bright){
if (first==1){ //I did this so that it won't turn on and off constantly while the light is on, without this it would toggle constantly
first=0; // it will only do this when it first gets bright, not constantly while it's bright
if (toggle==1){ //toggles between on and off
turn it on
toggle=0;
} else if (toggle==0){
turn it off
toggle=1;
}
}
}
if(light is dark){
first=1;
}

Sorry, here is what I have so far. I don't know much about using counts in arduino, so I'm not even sure I have it assigned properly..

const int transistorPin = 8; // transistor output connected to pin 8
int photocellPin = 0; // photocell connected to analog pin 0
int photocellVal = 0;
int thresholdL = 850; // value assumed to be bright light
unsigned count = 0; // count

void setup()
{
pinMode(transistorPin, OUTPUT);
pinMode(photocellPin, INPUT);
Serial.begin(9600);
}

void loop()
{
photocellVal = analogRead(photocellPin); //read the photocell
delay(100); // wait before further action
Serial.println(photocellVal); // print to screen
delay(500);

if (photocellVal > thresholdL)
{
digitalWrite(transistorPin, HIGH);
count = count+1;
}
if (count == 2)
{
digitalWrite(transistorPin, LOW);
}
}

At some point, with that code, count will increment to 3, 4, 5, ...

Then, what happens? The pin never gets turned off.

You need to either reset count to 0 when it gets to be 2, or use the modulo function:

if(count % 2 == 0)

In addition to what PaulS said, the way you have it the pin will constantly be switched on and off if it is bright. You only want it to do something each time it gets bright, so you need another variable like 'first' or in some tutorial examples you would use 'buttonstate' because it's commonly used for buttons.

if (photocellVal > thresholdL)
{
if (first==1){
first=0; //set first to 0 so that it won't constantly keep switching things on and off
digitalWrite(transistorPin, HIGH);
count = count+1;
}
if (count == 2)
{
digitalWrite(transistorPin, LOW);
}
}
}
if(photocellVal < thresholdL){ //when its dark again
first=1; //reset first so that it will perform an action when it gets bright again
}