I'm kinda new with electronics (but not programming), and I don't really get the phenomenon which happend to me.
I'm currently working on a binary clock project with an arduino nano and interrupts. I connected my buttons as it was described in this example. In the interrupt function I also included some primitive debouncing logic like this:
void setHour() {
static unsigned long latHourInterruptTime = 0;
unsigned long hourInterruptTime = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (hourInterruptTime - latHourInterruptTime > debounceDelay) {
hour = (hour + 1) % 24;
int cPin5To7 = PORTC & B11100000;
PORTC = cPin5To7 + hour;
digitalWrite(hourLEDPin, !digitalRead(hourLEDPin));
}
latHourInterruptTime = hourInterruptTime;
}
The interrupt was attached first in mode HIGH than after I saw some random flipping without pushing the button I used RISING, however this didn't change anything, and I still experienced some random flickering.
I then changed the input pin to mode INPUT_PULLUP removed the 10k resistor and connected the other pin of the button to GND, and changed the interrupt mode to FALLING. In this configuration in the last 30 minutes or so I didn't see any random signals.
Could maybe somebody explain me what was the problem with the pull-down configuration? I mean The pin shouldn't be floating, so afaik it should have worked.
UPDATE: as I clicked post it just triggered the interrupt two times, without pushing the button. However I slightly pushed my desk. Maybe my breadboard is broken? I'm genuinely confused...
I tend to troubleshoot these things by separating the hardware from the software.
I would:
Check to see if the internal pullup on the #2 pin is enabled. BTW I don't use switches often but the folks here prefer to use buttons from the pin to ground with the internal pullup enabled.
Change wiring to eliminate the possibly of a spurious hardware interrupt:
Move the Black & Red "power" wires to the opposite side of the push board. Reason, keep the ground closer to the switch.
Move the Green wire on the push board to the ground hole right next to the black ground wire.
Now run your program, you should be able to run for days without an interrupt. The button will do nothing.
BTW the push boards can be problematic. Be sure the wiring is the correct size and is pushed in fully.
You should be able to wiggle each wire with no change in program function.
Thanks JohnRob for the hints. For the last couple of hours I disconnected the two interrupt pins with their internal pull-ups on and let it run. Until now it works like a clockwork (hehe pun intended :D).
So my guess is A) the push buttons are somehow glitchy, and they let through now and than some voltage, which sounds highly unusual, but hey they are no high quality fancy buttons.
Or B) my breadboard is glitchy and somehow shorts some of the holes, which again, would be somewhat surprising, as it would mean that it is able to short lines across 3 lines (pin A-empty line-pin B)....
So I might try to solder it, and see if it is working, or just include a switch and two transistors to put the clock into "setting" mode which connects the interrupt pins with the button.
No this sort of thing can happen on bread board or in an electrically noisy environment. Simple fact is that that tutorial is not the best engineering practice. A 10K pull down is not too clever.
The best way is to use an external pull up resistor of about 1K as well as the internal ones, with the switch connecting the input to ground. In that way a disconnection of the button wiring does not cause a rouge input.
In your post #3 you mention "pull-ups" In the circuit referenced by your initial post, the button will pull the interrupt pin high. when the button is not pressed the 10k resistor pulls the pin down (to gnd).
If you have the internal pull-up enabled, it will interact with the 10k resistor keeping the interrupt pin somewhere in the middle (not a good place to be). Will cause all sorts of havoc.
In your post #3 you mention "pull-ups" In the circuit referenced by your initial post, the button will pull the interrupt pin high. when the button is not pressed the 10k resistor pulls the pin down (to gnd).
If you have the internal pull-up enabled, it will interact with the 10k resistor keeping the interrupt pin somewhere in the middle (not a good place to be). Will cause all sorts of havoc.
Try your circuit with the pullup disabled.
Yes I know, however as I mentioned in the initial post I rebuilt the the circuit and removed the 10k resistor and change that the other side of the button connects to gnd.
In the meantime I got some hints from Paul__B, that the use of the interrupt is not really optimal for my use case, and that I should reconsider using them. I'm going to try that and also some of the things you guys suggested. Sorry that I didn't react before, but I was busy doing other things. ^^
Thank you all for the help, I think now I somewhat understand what the issue is, and I think I can do something against it. Thank you very very much for the fast responses and the hints what I was doing wrong