Code works well on MEGA but not so well on NANO

Hi Guys.

I have been working on a project involving a button triggered interrupt which increments a variable +1 each time the button is pressed. Originally I was working my code using a MEGA and found that at times, when I pressed the button once, the the variable would increment a couple of times (as if I had pressed the button a couple of times) I figured that might be due to noise. So I added a small capacitor and it really helped! Now I transferred the project to a nano to keep everything snug on one breadboard and for some reason the interrupt is not working reliably (both, with and w/o a capacitor) (based on info displayed on the serial monitor). Sometimes I press a button and it doesn’t increment at all! Sometimes it seems that there is lag. Why would this happen and what can I do?

  • ngelb016

As you have not posted your programs or your wiring diagrams it is hard to help. See How to use the Forum


There should be no difference.

Either you've forgotten to modify part of the code (maybe pinmode where you set it to input pullup?), leading to floating pin issues - or there's a loose wire.

Breadboard is notorious for making unreliable connections that lead to difficult to debug intermittent problems. I consider breadboard unfit for purpose for this reason.

I use protoboard, aka solderable breadboard, and solder everything that doesn't need to be removable, and use male dupont pins on the PCB and female dupont connectors (the male dupont into female pin header is fiddly, while female dupont into male pin header is rock solid) where it does need to be removable. I happen to sell prototyping board like this, of excellent quality if I do say so myself.

I would do a very simple sketch: (assuming the button is wired in the recommended way, with pullup enabled and the switch connecting the pin to ground)

void setup() {
pinMode(your button pin,INPUT_PULLUP);
void loop() {
digitalWrite(13,!digitalRead(your button pin));

This will turn on the light while the button is pressed; hold down the button and poke at the wires to see if any of them are loose.

Also - you can debounce the switch in software instead of with the capacitor - this has been written about by many people at great length, search and ye shall find.