Here is the debouncing setup I'm using in code: http://forum.arduino.cc/index.php?topic=266132.0
Yes, it does read, delay, read. But if I can condition the input side, then I only have to account for normal Arduino bouncing (crazy boards), and make sure I decouple my ICs as any circuit grows.
Relays are one method, but expensive and bulky for my needs.
I want to stay with optoisolation because it keeps the entire 24vac site 100% separate from the logic side. Cost and complexity is much less with the IC handling some functions for me.
Rob- It gets better, LOL. Not too bad once you get the basics down, but some real gotchas for newbies to circuit designs.
The chip I'm using uses (in this case) 24vac of the thermostat line to light a very tiny LED inside the chip. LEDs only work in one direction, so on its own it would only light on either the top or bottom of the AC sine wave. So the chip includes a second one on the other half of the AC circuit (in reverse) so that one of them will be lit up any time there is AC power on the line.
The other side of the chip is a tiny detector that sees the LEDs light up and closes an internal switch between two legs on the logic side, completing a DC circuit. Because there is no physical connection between the separate electrical paths, there is no chance of short circuits or AC getting on a DC line (poof!) or DC causing interference on the AC line (malfunctions).
Since the method involves Optical emission and detection, the circuit is said to be Opto-Isolated (and derivatives of that term are acceptable)
The Arduino has digital input pins than can detect this circuit because it has a tiny amount of power that goes through the chip's two legs when closed and passes through a resistor to ground. The resistor prevents a dead short when the circuit is closed (like putting a screwdriver across battery terminals would be
The problem is where the top half of the AC sine wave drops from positive to negative. It must cross the zero voltage line, aka "zero crossing." It is at this point that NEITHER LED is lit up. So it has a little internal flicker.
This causes the output on the logic side to have matching gaps, aka pulses or ripple.
To complicate things, the circuit is pulsing twice in a cycle, which is 60 times per second, making it 120 times per second that it will be at zero (plus a little padding). The Arduino reads the digital input at some frequency that depends on the chip frequency, the circuit, the program running, etc etc etc.
MOST of the time the Arduino will miss the zero crossing, but once in a while it will hit it.
A debounce software method most often will read, make a small delay and read again, returning a "my switch changed state" message only if they match. This method helps a great deal because the odds of hitting that zero crossing corridor twice in a row get much smaller. But the chances are not zero.
Rectifying an AC circuit is just a fancy way of saying you are chopping off one half of the sine wave, usually to convert it to DC. For this example, let's say we are keeping the top half and cutting off the bottom half.
We use a diode (not Light Emitting Diode, but it could be), which act as one-way check valves. They block flow in the backwards direction and permit flow in the forward direction- when the "pressure" on the other side of the check valve drops. Just like hydronic systems.
So once we've eliminated the "backwards" part of an AC circuit, we have little one way pulses. But for DC, we usually want one way SOLID current without pulses. Enter the capacitor.
Capacitors are like little stateful batteries. They charge when there is power and once the circuit drops, it discharges its stored power into the circuit. The use we are discussing here is their ability to "bridge" or "power" a circuit during very short outages...like pulses.
The capacity, type, and power rating of a capacitor has to be appropriate for the circuit so that it will bridge the gap between pulses without dropping too low (which would be seen as an outage pulse by the circuit), but not so large that it powers the circuit too long and we miss a proper drop when the circuit is turned off on purpose.
Making a mistake either doesn't work properly, or it releases the magic smoke inside all electronics, LOL.
I don't want to have any chances of shorting either side of the circuit. I don't want any chance of interfering with the 24vac side. It takes just an extra pair of wires attached to anywhere in the thermostat circuit to detect when a thermostat is closed, without any impact on the system (50ma won't be noticed)
I am not expert at selecting capacitors. I've read it is common to specify at least 2x the expected voltage. For an electronics guru, this is a very normal and simple day to day thing. I'm hoping one will lend his expertise here so I don't make a mistake.
Edit to add: Once rectified, then the voltage would have to be adjusted from 24vdc down to 5vdc. The 24vac input is often 29-31vac, so it takes some caution, usually a voltage divider- or better, a regulator. When I re-read my post, I forgot this part.