Count amount of times a boolean changed value

I have a photoresistor thats detecting an LED and I want to count the amount of times the LED triggers the photoresistor. The problem is, when I just use a normal (int) counter, the value shoots up as soon as the resistor detects light.

Now I thought I could just use a boolean that switches from false to true when it detects light over a certain threshold, which works, but now I need to count the amount of times it switches. How can I do that?

Here's some piece of code for better understanding

if (lightVal >= 800)
  {
    ticker == true;
  }
  else
  {
    ticker = false;
  }

Now I need to count how many times it went from false to true.

Greetings :slight_smile:

Look for transitions instead of levels. The state change detection tutorial explains how to do that. Count the times that ticker changes from false to true (positive edge). See also.

Tell us what this does ?

oh yeah sure. Thats the intensity of the light which hits the photoresistor. If that value is above 800 (when a laser hits it) it should trigger the counter.

The loop( ) function is run 1000s of times per second, over and over and over again.

When lightVal >= 800 is true, since loop( ) keeps looping, you will have 1000s of Counts occurring.


BTW
ticker == true; should be ticker = true;

= verses ==

What is lightVal when laser is ON? When OFF?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.