Absolute newbie to Arduino's and hardware programming in general, but my Google-fu failed me and I couldn't find anything about this issue I'm running into so here goes:
I'm trying to wire something up that has 4 buttons that increment/decrement an integer value. Tight now I've only wired up one button on my breadboard to test with and I'm starting to see some strange behavior. I tested the button setup on every pin.
- Pins 0 and 1 weren't working but from the board (and the product page for the Unos) these pins seems to be reserved for transmitting and receiving TTL data so that's somewhat expected, even if I don't fully understand (yet) what that means.
- Pins 2, 4, 5, 8, 12 worked as expected
- Pins 3, 6, 7, 9, 10, 11, 13: these are strange ones - for example with pin 3 even when the code is not "watching" this pin when I have the button plugged into it pin 2 seems to be detecting "changes". Is this pin linked to pin 2? I couldn't find any special case documentation and looking at the pin mappings (http://arduino.cc/en/Hacking/PinMapping168) nothing really called out that these are special/reserved pins.
I have the NO button wired on a breadboard with pin 1 of the button connected to the 5V and the other to ground (via a 20k resistor... arbitrary resistor that was just lying around) as well as a jumper that I move between the digital inputs.
The code I'm using to test the button states is as follows:
#include <Event.h>
int buttonPins[] = {2};
const int numButtons = sizeof(buttonPins) / sizeof(int);
int oldButtonStates[numButtons];
void setup()
{
Serial.begin(9600);
for (int i = 0; i < numButtons; i++)
{
pinMode(buttonPins[i], INPUT);
oldButtonStates[i] = digitalRead(buttonPins[i]); // Detect initial state so we don't "miss" the first click
}
}
void loop()
{
for (int i = 0; i < numButtons; i++)
{
processButton(i);
}
}
void processButton(int buttonIndex)
{
int newButtonState = digitalRead(buttonPins[buttonIndex]);
if (newButtonState != oldButtonStates[buttonIndex]) // Edge detection
{
Serial.print("Button ");
Serial.print(buttonIndex);
Serial.print(" on pin: ");
Serial.print(buttonPins[buttonIndex]);
Serial.print(" state: ");
Serial.print(digitalRead(buttonPins[buttonIndex]));
Serial.print(" was: ");
Serial.println(oldButtonStates[buttonIndex]);
oldButtonStates[buttonIndex] = newButtonState;
}
}
The output of the code when the button is connected to pin 3 and pressed (briefly) and nothing is connected to any of the other digital pins:
Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 1 was: 1
Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 0 was: 1
Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 0 was: 1
At first I thought it might be related to the PWM pins but with some of the PWM pins working as expected I am thoroughly baffled.
Am I missing something glaringly obvious?