Digital Input Counter

I am using a switch that is connected to a digital input.
I want to use the switch as a counter, every time I press the switch, I want the count to increase by one.

I am using a if statement to detect if the switch is high and once it is high I am simply increasing the count:
if (Switch == True)
{
count = count + 1;
}

The problem I am stuck with is that the code loops multiple times while the switch is pressed, this results in a count of more than one for example 11 even if the switch is only pressed once.

any help will be greatly appreciated

This is the classic issue where you are testing the state of the input rather than testing for the change in state of the input.

Review the following tutorial: StateChangeDetection

1 Like

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