Figure 6: Contact bounces in a switch To avoid reading the invalid logic, a switch debouncing scheme is needed. One could google for an implementation that suits his/her application. For example, our design above is in Verilog, so I prefer a debounce module written in the same language. Searching with “switch debounce Verilog” keyword gives me several URLs. I just pick the first one Debounce Logic Circuit (with Verilog example) and add the module DeBounce_v.v provided there in my project (the website also provides useful information on switch debounce so please read). The code fits in seemlessly. Click the file below to download the modified Verilog code (you must add the DeBounce_v.v from the link above to the XISE project yourself.)
sw3_asm4db.v This last video demonstrates the 3-way lighting FSM after the switch debouncing modules are added to both switches. Reliability is improved significantly. There are still some minor glitches when I pressed those switches like crazy, but that kind of operation is somewhat too extreme for sane humankind.
Eventually, the FSM now works as desired. The house owner is happy. And now the demanding dude asks if a 4-way lighting is possible. In addition to the A, B push button switches, he wants to be able to turn the light bulb on/off by touching the third button on the screen of his mobile phone. How could you modify the state diagram or ASM to accommodate his new request?