switch and interference problems

hi guys,

this one completely confuses the life out of me. i was working with my project and got it all working fine. i took the arduino and my relay card out of the traffic light to repaint it. i put everything back together. (the arduino never got unplugged i just disconnected the lights from the relays) i plug it in and its freaking out. if i have any sort of wire in pin 2 or if my hand is to near the board it thinks the button was pushed.

i have tried using a 22k ohm resistor as a pull up or down and it still does it.
i also tried using the arduino’s internal pullup by assigning the pin to high.

the wire is almost acting like an antenna.

if i leave pin 2 unplugged it works fine.

i didn’t write the code i only modified it slightly to add the euro overlap

// Sample serial commands:
//   ta!     toggle all
//   fffr!   flash red three times
//   FFFr!   flash red once for 3x the regular period
//   +r!     light red in addition to previous
//   -r!     dim red from previous
//   +ffr!   flash red twice in addition to previous
//   ffa!    flash all twice
//   y!      light only yellow
//   !       light none

////////////////// CONSTANTS //////////////////

// Output light constants (combinable flags)
const int NONE = B000;
const int RED = B001;
const int YELLOW = B010;
const int GREEN = B100;
const int ALL = RED | YELLOW | GREEN;

// Output pins; pins are doubled up so there is
// more power to drive the electromechanical relays
const int RED_PIN_1 = 10;
//const int RED_PIN_2 = 10;
const int YELLOW_PIN_1 = 11;
//const int YELLOW_PIN_2 = 6;
const int GREEN_PIN_1   = 12;
//const int GREEN_PIN_2 = 8;
const int POTpin = 3; 

// Input pins and interrupts
const int BUTTON_PIN = 2;
const int BUTTON_INTERRUPT = 0; // Pin 2
const int INTERRUPT_THRESHOLD = 250; // ms
const int CHECK_INTERRUPT_PERIOD = 200; // ms

// Other parameters
const int SERIAL_BAUD = 9600;
const int FLASH_TIME = 500; // ms
const int FAST_FLASH_TIME = 250; // ms

////////////////// GLOBAL VARIABLES //////////////////

// The current light state
int lit = B000;
// set pot val to 0
int POTval = 0;
// zero out the light timers
int redInterval = 0;
int greenInterval = 0;

// Pattern functions
typedef void (* PatternFunctionPointer) (); 
const int PATTERN_COUNT = 5;
PatternFunctionPointer patterns[PATTERN_COUNT] = { 

// Pattern management
int currentPattern = 1;
boolean patternChanged = true;
boolean patternInitialized = false;

////////////////// SETUP //////////////////

void setup() {
  pinMode(RED_PIN_1, OUTPUT);
  //pinMode(RED_PIN_2, OUTPUT);
  pinMode(YELLOW_PIN_1, OUTPUT);
  //pinMode(YELLOW_PIN_2, OUTPUT);
  pinMode(GREEN_PIN_1, OUTPUT);
  //pinMode(GREEN_PIN_2, OUTPUT);

  digitalWrite(BUTTON_PIN, HIGH); // Enable internal pullup resistors
  attachInterrupt(BUTTON_INTERRUPT, changePattern, LOW);

////////////////// LOOP //////////////////

void loop() {
  // Reset patternChanged for the next interrupt
  if (patternChanged) {
    patternChanged = false;
    patternInitialized = false;
    flash(RED, 1, FAST_FLASH_TIME);

  POTval = analogRead(POTpin);
  greenInterval=POTval *15 +4000;
  redInterval=POTval *15 +2000;


////////////////// INTERRUPT //////////////////

void changePattern() {
  // Make sure the interrupt does not happen withing
  // the given period. This is to prevent "bouncy" buttons

  static unsigned long lastInterrupt = 0;

  if (millis() - lastInterrupt > INTERRUPT_THRESHOLD) {
    lastInterrupt = millis();
    currentPattern = (currentPattern + 1) % PATTERN_COUNT;
    patternChanged = true;

////////////////// PATTERNS //////////////////

void normalPattern() {
  if (!lightDelay(GREEN, greenInterval))

  if (!lightDelay(YELLOW, 2000))

  lightDelay(RED, redInterval);

void euroPattern() {
  if (!lightDelay(GREEN, greenInterval))

  if (!lightDelay(YELLOW, 2000))

if (!lightDelay(RED, redInterval))

  lightDelay(RED + YELLOW, 2000);

void blinkRed() {
  flash(RED, 1, 750);

void blinkAmber() {
  flash(YELLOW, 1, 750);

void serialControl() {
  static boolean serialBegun = false;

  if (!serialBegun) {
    serialBegun = true;

  if (!patternInitialized) {
    flash(ALL, 1, FAST_FLASH_TIME);
    patternInitialized = true;

  static int flashes = 0;
  static int lights = NONE;
  static int flash_delay = 0;
  static boolean doToggle = false;
  static boolean add, subtract = false;
  boolean commandDone = false;
  while (Serial.available() > 0) {

    char value = Serial.read();
    // Stupid Arduino IDE doesn't send \n
    if (value == '!' || value == '\n')
      commandDone = true;

    if (value == 't')
      doToggle = true;
    if (value == '+')
      add = true;
    if (value == '-')
      subtract = true;

    if (value == 'f') {
      flash_delay = FLASH_TIME;
    if (value == 'F') {
      flashes = 1;
      flash_delay += FLASH_TIME;

    if (value == 'r')
      lights |= RED;

    if (value == 'y')
      lights |= YELLOW;

    if (value == 'g')
      lights |= GREEN;
    if (value == 'a')
      lights |= ALL;

  if (commandDone) {
    if (add)
      lights = (lights | lit);
    else if (subtract)
      lights = (~lights & lit);
    if (flashes)
      flash(lights, flashes, flash_delay, lit);
    else if (doToggle)

    lights = NONE;
    flashes = 0;
    doToggle = false;
    flash_delay = 0;
    subtract = false;
    add = false;


////////////////// TRAFFIC LIGHT CONTROL //////////////////

void light(int lights) {
  digitalWrite(RED_PIN_1, lights & RED);
  //digitalWrite(RED_PIN_2, lights & RED);
  digitalWrite(YELLOW_PIN_1, lights & YELLOW);
  //digitalWrite(YELLOW_PIN_2, lights & YELLOW);
  digitalWrite(GREEN_PIN_1, lights & GREEN);
  //digitalWrite(GREEN_PIN_2, lights & GREEN);
  lit = lights;  

void toggle(int lights) {
  light(lit ^ lights);

void dim(int lights) {
  light(lit & !lights);

boolean flash(int lights) {
  return flash(lights, 1);

boolean flash(int lights, int count) {
  return flash(lights, count, FLASH_TIME); 

boolean flash(int lights, int count, int delay) {
  return flash(lights, count, delay, NONE);

boolean flash(int lights, int count, int delay, int previous) {
  int litPreviously = lit;

  for (int i = 0; i < count; i++) {
    if (!lightDelay(lights, delay))
      return false;
    if (!lightDelay(previous, delay))
      return false;

  return true;

boolean lightDelay(int lights, int time) {
  // We want to periodically check for an interrupt
  // in the middle of each delay; return false if an
  // interrupt has occured and true if the delay
  // completes successfully

  int loops = time / CHECK_INTERRUPT_PERIOD;
  int remainder = time % CHECK_INTERRUPT_PERIOD;

  for (int i = 0; i < loops; i++) {
    if (patternChanged)
      return false;

  return true;

Sounds like you have a floating input, which could be the result of poor contact between pin2 and whatever it is supposed to be connected to. Or you could have got a dry joint, or splashed something on the board, or some other non-obvious physical damage.

I suggest you carefully disconnect your Arduino and run the Button example. This will turn on the integral LED according to the state of pin2 so provides a very convenient way to test your hardware.

i thought the same thing, button test works fine.

i have another version of the code using pin 2 and that one works fine.
but this code has the serial commands and would like to use it.

So it's either your sketch, or the hardware that is connected to your pin2 in your fully assembled solution. You haven't changed the sketch since it was (presumably) working correctly, and you have disturbed the hardware slightly, so I'd look to the hardware first and do a continuity check between pin2 on the Arduino PCB and the corresponding track on the device that is supposed to be connected to it.

if i run another sketch that has the switch on pin 2 it runs fun. i have also replaced the switch. im hoping its not my arduino. could it possibly fail like this? im going to try and run it on pin 3. could this be related to a power problem? im wondering if i have "dirty voltage"?

I know you ran the Button example, but was that with the Arduino on its own? Can you run the Button sketch with the Arduino+shields fully assembled and using the real button for pin2? This would prove once and for all whether it was a hardware problem.

leaving every thing plugged in except the potentiometer because i needed the 5v, ran fine. i wonder if the board is resetting? im going to try it with the flashing turned off.

so i stripped the board, i only have a led running from pin 12 to ground. with my sketch running and it's still getting interference from the switch lead. its almost like one of those touch lamps.

edit* even if i switch the button pin to pin 3 its still doing it. its almost like its ignoring the pins high or low state.

ok well iv narrowed down the problem.

the circuit works fine if its powered from an external 12v source.

once i have the 120v lines going into the relays, it starts spazzing out.

i think the 120v lines are giving interference. im not sure how to fix this.

i rebuilt the entire circuit using all new parts. im going to program a new arduino but if anyone else knows how to solve the interference it would be great.

I think it's time you published your circuit.

im trying to upload my fritzing schematic. but i think because im using led traffic lights i have an inductive circuit.

should i have a snubber on the a/c side of the relay? how does one wire a snubber?

You've got inductive LEDs? That's a new one.

they are 120v traffic light modules. they have a transformer in them. from my understanding a transformer is an inductive load.
if i disconnect the lights from the relays the arduino starts working correctly.