count led flashes

Hi
I’m totaly new to Arduino and and I’m quite new to programming,I’ve a ldr on my board connected to Arduino analog in port 0,it must count the number of flashes from a led but there is something wrong with my code,it must count 1+ every time led flashing (analogRead(ldr)< 150),the led flashing between 0 and 8 times per second,but every time there’s a blink it’s count the 10 to 15 times

here is my code

int ldr = 0;
int (a) = 0;
void setup()
{
pinMode(ldr,INPUT);
Serial.begin(9600);
}

void loop()
{
for (int a; a=a+1;)
if (analogRead(ldr)< 150)Serial.println(a);

}

A few things:

Digital pin zero and analogue pin zero are completely different pins, so the pinMode sets up digital pin zero.
It isn’t necessary to set the pinMode of an analogue pin.

You have two varaiables called “a”, but you’re only using the one declared in “loop”.
The one declared outside (“int (a) = 0;”) has superfluous brackets.

“a = a + 1” will always be true, until “a” wraps around to zero.
“a” is not initialised.

I’m not sure an LDR is responsive enough to count at 8Hz.

Could you maybe change the spelling in the title?
People may get the wrong impression! :wink:

I'm not sure an LDR is responsive enough to count at 8Hz.

Normally correct, yes, for 8hz you would need a photodiode or similar. On an LDR you would need a delay between each flash for it to be able to count them properly (delay needed changes depending on the LDR.

Mowcius

An LDR is a photodetector. Perhaps you mean a photodiode or phototransistor.

Sorry for my english,i used google translate ::)

An LDR is a photodetector.
Perhaps you mean a photodiode or phototransistor.

Oops, who said photodetector? :stuck_out_tongue:

I can't see how that code counts anything. To count you need to see when something makes a transition from one state to another. In this case from a reading above 150 to a reading below it. To do this you have to store the last value you read and only update the count when the last value has crossed the threshold in one direction.

It might be me but I don't see that code doing this.