State change detection

Can you use State Change Detection for use in a counter ?

An example would be the state change of a LED.

If so what would be the best way to code it?

Thanks

Can you use State Change Detection for use in a counter ?

You can count the number of times that the state changes.

An example would be the state change of a LED.

An LED does not change state arbitrarily, like a switch can.

If so what would be the best way to code it?

Start with a reasonable set of requirements.

If you have a loop that is blinking a LED, can you use State change detection for your counter to count the events? And use a value of change of state for the counter?

State change DETECTION implies that you are detecting the change of state of change of something like a button or sensor.

Since an LED is controlled by the Arduino there is no need to detect it. You simply need to increment a variable every time you change the state.

Take a look at the StateChangeDetection example in the IDE. digitalRead() the state of the LED or (better) use the value of a variable that you have set equal to the state of the LED and detect when it changes. Add to the counter when you detect a relevant change of state.

krobert35: If you have a loop that is blinking a LED, can you use State change detection for your counter to count the events? And use a value of change of state for the counter?

I think we need to know what YOU have in mind when you use the phrase "state change detection"

If you have code that is causing an LED to blink why can't it also increment your counter variable?

...R

Thanks everyone especially UKHeliBob! Digitalread looks like just the function to use. I'll continue on with my sketch, now thanks for the help. Sometimes you have to ask the awkward questions to see another point of view.

Digitalread looks like just the function to use.

Although I suggested it as a possible solution to your original question I don't understand why you would need to use it. What else is setting the state of the LED other than your program ? If your program is setting the state of the LED you will know what the state is, when/if it changes and the state it changes from/to.

digitalRead() can be useful to flip the state of an output but that is not what you asked. digitalWrite(ledPin, !digitalRead(ledPin));would change the state of the LED whatever state it is in but of course it does not inherently permit you to count the number state changes without more code.