I'm working on a project for my friends car, he wants a control for the engine fans, he wants to use a single toggle switch to control the 2 fans
Press the button once and the first comes on, press it again the second comes on and the first stays on, press it a third time and the fans both stop
I want to use an interrupt to control it so we can program a few other things in there as well
The problem that I am having is that there is a lot of noise on the line for the button (only hovering around a volt or two) so its constantly tripping the interrupt
The Arduino is connected to two relays which control the fans
I know that there is sample coding for debounce but it uses delays which interrupts can't use, any recommendations as to what I can do for this?
I have tried putting a loading resistor on the line, and a voltage divider circuit to cut the voltage down, and neither worked
GreenGiant:
I'm working on a project for my friends car, he wants a control for the engine fans, he wants to use a single toggle switch to control the 2 fans
I hate having to tell the customer they've got their requirements wrong, but that is a daft arrangement. It requires the driver to keep track of what the fans are doing (which won't necessarily be obvious) and nudge them into the desired state (which may involve dealing with missed or spurious inputs).
The sensible way to do this is with a three position switch so that you can immediately see the current state and move it positively and unambiguously to the desired state.
The customer is always right, but the customer can be right and an idiot at the same time.
I hate having to tell the customer they've got their requirements wrong, but that is a daft arrangement. It requires the driver to keep track of what the fans are doing (which won't necessarily be obvious) and nudge them into the desired state (which may involve dealing with missed or spurious inputs).
The sensible way to do this is with a three position switch so that you can immediately see the current state and move it positively and unambiguously to the desired state.
The customer is always right, but the customer can be right and an idiot at the same time.
I suppose I could have added more information...
He wants to keep the car's interior stock, this switch was used for something else that he doesn't need anymore
There is a thermal override for the fans to cool the engine, this is for if he wants to turn on the fans before pushing the car (its a Trans Am with a chevy small block in it, its been modded for performance and he wants to preemptively cool the engine so it doesn't get too hot)
My original though was to use a 3 position switch, or 2 independent switches, but he wants to use the stock switch
I don't see why not. It isn't the seconds, minutes, hours apart that you want to ignore. It is the microseconds apart bouncing that you are trying to ignore. Or, did I miss something?
I guess what I'm really looking for is a way to distinguish between a true rising edge (all the way to 5 volts) versus a noisy line (0-3 volts)
Oh, well this is not the same as debouncing.
One thing you could do is to move the switch to an analog pin, and read the actual value, anywhere from 0 (0 volts) to 1023 (5 volts).
Another thing to do is, again using the blink with delay example, is to require that the switch be held for some period of time. Noise shouldn't remain high long enough to matter.
I'm a big fan of interrupts but I wouldn't use them in this situation.
Switches can be debounced without delays easily enough (as Paul suggested).
This should hardly interfere with doing "other things".
Do you have a schematic? If things are that noisy, then perhaps some electrical measures to mitigate the noise are in order, e.g. strong pullups and/or filter capacitors.
BTW, toggle switches and pushbutton switches are two different things.
Not trying to peddle my open source library but this library debounces all input devices including buttons. Just in case your friend decides to switch to a rotary encoder or keypad the library handles those plus some more. Take a look if you want. It's not running on interrupt though:
Also you can simply take the debounce code from the library
It doesn't seem like a sensible system to me, but he's the customer.
GreenGiant:
The problem that I am having is that there is a lot of noise on the line for the button (only hovering around a volt or two) so its constantly tripping the interrupt
In understand that the toggle switch is a momentar switch.
Is it normally open or normally closed?
What is connected to your momentary switch? Is there something existing connected to it that you're trying to tap into, or is it dedicated to the Arduino now?