user68204: The LDR sensor gives a range of values to show whether a LED is OFF/ON, let's say above LDR range 400, the led is ON. When a LED is ON, after reading the sensor values it shows above LDR range 800, because the LDR sensor measuring in ms, a single LED blink shows six LDR 800+ values. Is there any way to count all the six  LDR 800+ values to increase counter by 1?
Here's my output in serial monitor. 314 to 322 is led OFF state and 816 to 851 is led ON state.
LED : 314
LED : 314
LED : 314
LED : 321
LED : 322
LED : 322
LED : 816
LED : 817
LED : 818
LED : 817
LED : 817
LED : 815
LED : 351
LED : 329
LED : 323
LED : 327
LED : 326
LED : 339


user31105: I'm not sure what the value 400 is used for exactly, but the code below is for increasing the counter when at least 6 consecutive values above 800 are occurred.
The key is to use a state machine.
I don't have a compiler at hand so forgive possible syntax errors.
First we define a state. In your case, there are only two possibilities, so a boolean (bool) is used, normally an enum is used.
bool _counting = FALSE; 

We also need a constant (or define) and a counter to see how many ON values are needed before incrementing the counter, which counts downwards to 0 (and can go negative when more ON values occur).
const int MIN_FLASH_LENGTH = 6;
int _onBeforeIncrementing = MIN_FLASH_LENGTH;

However, we can use the last variable to see if counting is occurring, so we do not have to use the boolean above.
Then we need a counter for the amount of flashes itself:
int _amountOfFlashes = 0;

Both of these are global variables.
Furthermore, in the loop, we need to check for the LED value.
Thus the entire code will be:
const int MIN_FLASH_LENGTH = 6;

int _onBeforeIncrementing = MIN_FLASH_LENGTH;
int _amountOfFlashes = 0;

void loop()
{
    int ledValue = getLedValue(); // Use your code here
    if (ledValue >= 800)
    {
        _onBeforeIncrementing --;
        if (_onBeforeIncrementing == 0)  // Flash of enough length occurred
        {
            _amountOfFlashes++;
        }
    }
    else // Restart counting
    {
        _onBeforeIncrementing = MIN_FLASH_LENGTH;
    }
}

The code fragment
if (_onBeforeIncrementing == 0)
{
    _amountOfFlashes++;
}

can be reduced to:
 _amountOfFlashes += (_onBeforeIncrementing == 0) : 1 : 0;

Also note if you have more code to add, to use the name LED or LDR into the global variables for clearity.

user7508: There are two distinct problems here.
The first problem is to convert the analog reading into a binary value.
You measure a value between 314 and 856, and you want to know whether
the LED is ON or OFF. The simplest solution is to compare the reading to
a threshold that is roughly in the middle of the range you measure. You
could say, for example, that the LED of OFF if the reading is below 580,
and is ON otherwise:
bool led_is_on = analogRead(ldr_pin) >= 580;

If the readings are noisy while they cross the threshold (does not seem
to be the case in the short span of data you show), you may want to use
some noise cancelling techniques. You could, as suggested by Michel
Keijzers, require multiple consecutive “ON” reading before deciding the
LED is actually ON. Or you may research the technique of using
hysteresis, which would be my favorite solution, as it is extremely
simple. However, if your readings are always as clean as what you are
showing us (never fluctuating mid-range), you can stick with the simple
threshold solution shown above.
The second problem is called edge detection. You want to count the
blinks. For this, you need to count how many times the LED turns ON,
which is not the same as how many times the LED is ON. You know that
the LED turned ON if you just detected it is ON and, on the previous
iteration it was OFF. So you have to remember whether the LED was ON
last time you looked at it. This means you have to store this
information (whether the LED was ON) in a static (or, if you prefer,
global) variable. Which leads to the following program:
int blink_count;

void loop() {
    static bool led_was_on = false;
    bool led_is_on = analogRead(ldr_pin) >= 580;
    if (!led_was_on && led_is_on) {
        blink_count++;
    }
    led_was_on = led_is_on;
}

Note the condition: !led_was_on && led_is_on means “The LED was not
ON and now it is ON”.

68204: <p>The LDR sensor gives a range of values to show whether a LED is OFF/ON, let's say above LDR range 400, the led is ON. When a LED is ON, after reading the sensor values it shows above LDR range 800, because the LDR sensor measuring in ms, a single LED blink shows six LDR 800+ values. Is there any way to count all the six  LDR 800+ values to increase counter by 1?</p>
<p>Here's my output in serial monitor. 314 to 322 is led OFF state and 816 to 851 is led ON state.</p>
<pre><code>LED : 314
LED : 314
LED : 314
LED : 321
LED : 322
LED : 322
LED : 816
LED : 817
LED : 818
LED : 817
LED : 817
LED : 815
LED : 351
LED : 329
LED : 323
LED : 327
LED : 326
LED : 339
</code></pre>
 31105: <p>I'm not sure what the value 400 is used for exactly, but the code below is for increasing the counter when at least 6 consecutive values above 800 are occurred.</p>
<p>The key is to use a state machine.
I don't have a compiler at hand so forgive possible syntax errors.
First we define a state. In your case, there are only two possibilities, so a boolean (<code>bool</code>) is used, normally an <code>enum</code> is used.</p>
<pre><code>bool _counting = FALSE; 
</code></pre>
<p>We also need a constant (or define) and a counter to see how many ON values are needed before incrementing the counter, which counts downwards to 0 (and can go negative when more ON values occur).</p>
<pre><code>const int MIN_FLASH_LENGTH = 6;
int _onBeforeIncrementing = MIN_FLASH_LENGTH;
</code></pre>
<p>However, we can use the last variable to see if counting is occurring, so we do not have to use the boolean above.</p>
<p>Then we need a counter for the amount of flashes itself:</p>
<pre><code>int _amountOfFlashes = 0;
</code></pre>
<p>Both of these are global variables.</p>
<p>Furthermore, in the loop, we need to check for the LED value.</p>
<p>Thus the entire code will be:</p>
<pre><code>const int MIN_FLASH_LENGTH = 6;

int _onBeforeIncrementing = MIN_FLASH_LENGTH;
int _amountOfFlashes = 0;

void loop()
{
    int ledValue = getLedValue(); // Use your code here
    if (ledValue &gt;= 800)
    {
        _onBeforeIncrementing --;
        if (_onBeforeIncrementing == 0)  // Flash of enough length occurred
        {
            _amountOfFlashes++;
        }
    }
    else // Restart counting
    {
        _onBeforeIncrementing = MIN_FLASH_LENGTH;
    }
}
</code></pre>
<p>The code fragment</p>
<pre><code>if (_onBeforeIncrementing == 0)
{
    _amountOfFlashes++;
}
</code></pre>
<p>can be reduced to:</p>
<pre><code> _amountOfFlashes += (_onBeforeIncrementing == 0) : 1 : 0;
</code></pre>
<p>Also note if you have more code to add, to use the name LED or LDR into the global variables for clearity.</p>
 7508: <p>There are two distinct problems here.</p>
<p>The first problem is to convert the analog reading into a binary value.
You measure a value between 314 and 856, and you want to know whether
the LED is ON or OFF. The simplest solution is to compare the reading to
a threshold that is roughly in the middle of the range you measure. You
could say, for example, that the LED of OFF if the reading is below 580,
and is ON otherwise:</p>
<pre class="lang-cpp prettyprint-override"><code>bool led_is_on = analogRead(ldr_pin) &gt;= 580;
</code></pre>
<p>If the readings are noisy while they cross the threshold (does not seem
to be the case in the short span of data you show), you may want to use
some noise cancelling techniques. You could, as suggested by Michel
Keijzers, require multiple consecutive “ON” reading before deciding the
LED is actually ON. Or you may research the technique of using
<em>hysteresis</em>, which would be my favorite solution, as it is extremely
simple. However, if your readings are always as clean as what you are
showing us (never fluctuating mid-range), you can stick with the simple
threshold solution shown above.</p>
<p>The second problem is called <em>edge detection</em>. You want to count the
blinks. For this, you need to count how many times the LED <em>turns</em> ON,
which is not the same as how many times the LED <em>is</em> ON. You know that
the LED turned ON if you just detected it is ON and, <em>on the previous
iteration</em> it was OFF. So you have to remember whether the LED was ON
last time you looked at it. This means you have to store this
information (whether the LED was ON) in a <code>static</code> (or, if you prefer,
global) variable. Which leads to the following program:</p>
<pre class="lang-cpp prettyprint-override"><code>int blink_count;

void loop() {
    static bool led_was_on = false;
    bool led_is_on = analogRead(ldr_pin) &gt;= 580;
    if (!led_was_on &amp;&amp; led_is_on) {
        blink_count++;
    }
    led_was_on = led_is_on;
}
</code></pre>
<p>Note the condition: <code>!led_was_on &amp;&amp; led_is_on</code> means “The LED was <em>not</em>
ON and now it is ON”.</p>
