Backwards counter

Good evening everyone.

I configured an esp so I can read the number of pulses (contact to ground) and convert them into meters (1 pulse = 0.3m).
My problem is when a specific gpio is high, in fact from that moment the counter must count backwards.

any ideas on how to implement this part?

thanks for any suggestions


Without any code, I’ll just talk through my thought.

Assuming the contact to ground just adds .3 to something, put a check in before the add that is basically if specific gpio is high, make value -0.3, else make it 0.3.

Then, add value to something.

You can also set value to 0.3 above and if specific gpio is high, make value = value * -1. The advantage to this is if you decide to change 0.3 to something else, you only have to change it in one place and not forget the second one.