My switches are acting weird

I use a Radioshack electronics learning lab I got a while back to give me some stuff to work with. Whenever I touch the springs 46, 48, 50, 52 on the push button switches I complete the circuit and my arduino thinks I pushed the button. I'm confused because no where am I touching a power rail, how does this happen?

I have tried a simple button blink script, and it worked normal. I did not complete the circuit when I touched one spring. However, I noticed that when I pressed the button, the light stayed lit for a second as opposed to when I jumped the 2 springs with my finger it immediately turned off.

Here is my test script...

void setup() {
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  if (digitalRead(4) == HIGH) {
    digitalWrite(13, HIGH);
  }
  if (digitalRead(5) == HIGH) {
    digitalWrite(13, HIGH);
  }
  digitalWrite(13, LOW);
}

My problem only seems to happen when I use the Mouse.move(); command. It also seems to execute commands on its own without me doing anything. Do I have a defective unit or something?

Here is my mouse macro script...

void setup() {
  pinMode(4, INPUT);
  pinMode(5, INPUT);
}

void loop() {
  if (digitalRead(4) == HIGH) {
    Mouse.begin();
    Mouse.move(30, -7, 0);
    Mouse.click();
    Mouse.end();
    delay(1000);
  }
  if (digitalRead(5) == HIGH) {
    Mouse.begin();
    Mouse.move(100, 0, 0);
    Mouse.end();
    delay(1000);
  }
}

If anyone is wondering, I have a Leonardo board.

Without seeing how you have it wired, my guess is the digital inputs are floating and are subject to noise that can cause sporadic behavior.

Should I use analog inputs? I just got this thing 2 days ago, I'm still learning.

You use something like a 10k resister between the pin and gnd or the pin and 5v to hold the pin one way or the other. You then wire the switch so it connects the pin the opposite way, if you have the resistor holding it to gnd, the switch connects it to 5V. When you get used to it you can use code to activate the arduino's own internal resisters but thats for another day. Google pull up (or pull down) resisters...........

So that's my problem? Thank you! Ill try that out.

bkvaluemeal:
So that's my problem?

Considering we're not the ones sitting at your desk, we can't be sure but that's the likely culprit.

Change to the following to use the internal pullups:

void setup() {
  pinMode(4, INPUT);
digitalWrite (4,  HIGH); // <<<
  pinMode(5, INPUT);
digitalWrite (5,  HIGH); // <<<
}

void loop() {
  if (digitalRead(4) == LOW) { // <<< connect to Gnd vs connecting to +5
    Mouse.begin();
    Mouse.move(30, -7, 0);
    Mouse.click();
    Mouse.end();
    delay(1000);
  }
  if (digitalRead(5) == LOW) {// <<< connect to Gnd vs connecting to +5

    Mouse.begin();
    Mouse.move(100, 0, 0);
    Mouse.end();
    delay(1000);
  }
}

I tried both a pull-down and a pull-up and they both fixed my problem. I decided to use the software pull-up as suggested by CrossRoads, and this is what I've got. (I also removed the second if)

void setup() {
  pinMode(2, INPUT);
  digitalWrite (2,  HIGH);
}

void loop() {
  if (digitalRead(2) == LOW) {
    Mouse.begin();
    Mouse.move(30, -7, 0);     //Moves mouse to the icon
    delay(100);
    Mouse.click();
    delay(100);
    Mouse.move(-30, 7, 0);     //Moves mouse to the bottom left corner
    Mouse.end();
  }
}

When I push the button it clicks the icon for firefox in my task bar, but when I click it again it clicks the icon next to it and does that every time after that. Why is that?

You may notice in the picture that I have it connected to 3.3v. That's because if I use 5v it resets itself.

Picture is not coming thru

"http://s18.postimage.org/yfar6nb2f/IMG_0959.jpg"

I don't understand what your saying. The picture seems to work for me, and that link you posted works just fine.

Huh. OP's pic is not visible, but if I click my re-posting of the link it does work. Odd.

Anyway...
The pic seems to suggest that 3.3V is being shorted to ground when the switch is pressed.

I would remove all connections to the 3.3V or 5v, the internal pullup resistor makes that for you.
All that is needed is the wire from D2 to spring 46, and spring 47 to Gnd.
This might also explain the behavior, if the sketch was being restarted every time after the power supply was shorted out.

Okay, that seemed to work. Thank you, but that still doesn't explain why it clicks the icon next to firefox. It works the first time, but not the times after that. Further investigation yields that after the second Mouse.move(); it is not returning to the origin. Is the second movement not the exact opposite of the first?

Can't help you with that part, sorry.

Well thank you for the help you gave me, that helped a lot.