Hi,
The 8s comes because when SW is not pressed then latdelay is not reset to 0, it runs 4, 5, 6....253, 254, 255, 0, 1, 2, 3. Then it
This is 256 x 32ms = 8,2s.
Therefore I recommend to use ">=" instead of "=".
I'm no C specialist. But here my ideas:
(Please see my code as pseudo code)
--> FOR TOGGELING THE PORT IN MAIN
* I think latdelay should be declared "volatile"
* use a new volatile variable, "SWdebounced"
* use a new volatile variable, "toggleFlag"
Within the ISR:
* SWdebounced = SW (this is updated every 32ms only, bouncing usually is less than 10ms --> simple debounce method. No need for hardware debounce)
* latdelay ++
* if latdelay >=3 then {
* latdelay = 0
* toggleFlag = 1 }
In main:
* if (SWdebounced AND toggleFlag) then {
* toggle port
* toggleFlag = 0}
But this needs to ensure that the main loop at least runs once per 64ms.
This is usually no problem, but in some cases in main loop you wait for sth, or you do heavy data communication...
********
Therefore consider to do the complete toggeling in the ISR.
(This still is very fast ISR)
In main:
do nothing
In ISR:
* latdelay ++
* if (latdelay >= 3) and SW then {
* toggle port
* latdelay =0}
With this solution toggeling works even if the main loop is busy.
When SW = 1, then the counter runs 0, 1, 2, 3, 0, 1...
When SW = 0, then the counter runs 0, 1, 2, 3, 4, 5, ...
This means min toggleperiod is 100ms, but you will see immediate response when pressing SW after a longer time.
Klaus