There are two ways you can do what you want. The first is the easy way:
- Create either a global variable or a static local variable where you will store your count
- Somewhere in the 26.2 ms loop area, have an if statement which increments the variable if the digital io is pressed. IE:
Code:
if(digital_io_01 == 0) // Note that the inputs are pulled high, so when the sensor is indicating it is pressed, you are actually getting 0
countVariable++;
The second way involves using interrupts and is slightly more difficult, but necessary if you are going to be getting digital IO state changes at a high rate. (An encoder on a drive wheel would be an example of something which typically needs interrutps.)