Tip: Layer 3 backgrounds and tides disappear when you hit a message box, so plan your level around that. For example, you can add a sublevel without layer 3 at the start/midpoint, and include your message there.
This code will add a short cooldown to the ON/OFF switches to prevent Mario or sprites to trigger two of them at the same time. Unlike the patch version, this will also work with custom ON/OFF switches.
You can increase the cooldown in the asm file (you may want it higher if used in a water level or if you want switches triggered by cape twirls).