milis analog read error

Hello, i’m having a problem with this code. Actually i want to read an analogRead in 3 seconds with a photoresistor, but it didn’t work. I connected the photoresistor to A0 pin, also a led connected a led to simulate this code that blink every 3 sec. Please correct my code below if i missed something or wrong.

void setup
{

pinMode( LED, OUTPUT );
pinMode(A0, INPUT);
pinMode(12, OUTPUT);

}
boolean analogAboveThreshold(int pin, unsigned long millisec, int threshold)
{
unsigned long start = millis();
while (analogRead(pin) >= threshold); // blocking !!

unsigned long duration = millis() - start;

return ( abs(duration - millisec) <= 100 );
}

void loop()
{

if (analogAboveThreshold(A0, 3000, 500) )
{
digitalWrite(13,HIGH);
delay(3000);
digitalWrite(13,LOW);// yes we have ~3 seconds high
}
digitalWrite(12,HIGH);
delay(3000);
digitalWrite(12,LOW);
}

anyway thank you

lets start simple

what do you want it to do?

you want it to read a photocell for 3 seconds. Or you don't want it to read the photocell for 3 seconds?

write a simple line by line list of what you want

example:

first 3 seconds do nothing read photocell to see if it light if it is light turn on led if it is dark turn off led repeat

I agree with @gpop1's suggestion to write out what you want clearly, line by line.

You may find something useful in planning and implementing a program which starts with a list of steps. Also look carefully at how it uses millis() to manage timing and does not use delay().

...R

@Ha-fiz: Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum