So I've got several buttons connected up to pins 8-13 in my circuit (like this: http://www.arduino.cc/en/uploads/Tutorial/button.png), and am reading them through PINB. However, I get random results that aren't affected by whether there is power going to the buttons or not.
Here is my code:
Even if I have no input to any of those pins, the integer printed out isn't zero and changes from time to time. Is this due to noise (I thought that only happened when attached the board)?
It should not change during reset. You could measure if the 10k resistor is always between the input pin and GND. Perhaps you have the button 90 degrees rotated.
Avoid pin 13.
Depending on the type of Arduino, an onboard LED/resistor could be permanently attached to that pin.
When the pin is used as input, e.g with a switch to ground, it could permanently be ~1.8volt. Never getting "high", even with a 10k pull-up resistor.
Leo..
Yes, the older Duemilanove, and the Promini and Nano have an LED/Resistor to Gnd.
I think most others use an op amp as a buffer to drive the onboard LED.
Even if the LED is driven directly, it only presents a small load, a mA or so, that an external device can usually drive okay.
A button to Gnd would certainly not be a problem.
DDRB only sets the input/output direction.
I would enable the internal pullup resistor, then use a switch to Gnd to take the desired pin low.
In setup() :
for (byte x = 8; x<14; x=x+1){
pinMode (x, INPUT_PULLUP); // sets DDRB and PUD as needed
}
Then loop the same as you had before:
void loop()
{
delay(1000);
Serial.print(PINB); // x-x-D13-D12-D11-D10-D9-D8, upper 2 bits not connected to IO pins
Serial.print("_");
}