Very Novice Advice Needed

Hi guys. I’m having a stupid problem with most likely a very simple solution. I have a number of buttons and a switch, and I’m trying to make a control panel for my Arduino.
/
All of the buttons are 2 pin buttons. (So I guess you would call them interrupt buttons?)
Using copper wire, I threaded one pin of each button to Arduino GND. (It’s an Arduino Due) So 1 pin of each button is connected to a common wire, and that common wire is connected the Ground. The other pin of each button is connected to an individual Arduino Digital Pin.

All I want to do is make it so when I press one button, the Arduino prints a corresponding letter to the Serial Montitor.

#define STATE HIGH

#define BLACK_SWITCH 7
#define MEDIUM_GREEN 6
#define MEDIUM_BLACK_1 5
#define MEDIUM_BLACK_2 4
#define RED_4 3
#define RED_3 2
#define RED_2 8
#define RED_1 9

void setup() {
  Serial.begin(9600);
for (int pinNumber; pinNumber < 10; pinNumber++) {
  pinMode(pinNumber, INPUT);
}

}

void loop() {
if (digitalRead(BLACK_SWITCH) == STATE) {
  Serial.println("A");
}
if (digitalRead(MEDIUM_GREEN) == STATE) {
  Serial.println("B");
}
if (digitalRead(MEDIUM_BLACK_1) == STATE) {
  Serial.println("C");
}
if (digitalRead(MEDIUM_BLACK_2) == STATE) {
  Serial.println("D");
}
if (digitalRead(RED_4) == STATE) {
  Serial.println("E");
}
if (digitalRead(RED_3) == STATE) {
  Serial.println("F");
}
if (digitalRead(RED_2) == STATE) {
  Serial.println("G");
}
if (digitalRead(RED_1) == STATE) {
  Serial.println("H");
}
}

When STATE is HIGH, no letters are printed, no matter what button is pressed.
When STATE is LOW, all the letters are printed, no matter what button is pressed.

I know this is a silly question, but could someone help me out? :o

Do you think there is a difference between "a button" and "a switch"? You want to define all your pins connected to switches as "INPUT_PULLUP", so you use the internal resistors. Then change your logic so LOW is when a switch is pressed and is HIGH when not pressed.

Look at "https://www.arduino.cc/en/Tutorial/DigitalPins" for clarification.

Paul

Right, without pull-up resistors your inputs are floating and undefined when the switch is open.

During setup() enable the internal pull-ups for all of the input-pins you're using.

pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

When STATE is HIGH, no letters are printed, no matter what button is pressed.
When STATE is LOW, all the letters are printed, no matter what button is pressed.

I understand what you're trying to say and this doesn't affect what your program is doing, but 'STATE' is always HIGH because you've defined it that way. :wink:

And 'STATE' is not a good name for something that never changes state, and you could just use HIGH instead of defining a constant. :wink:

DVDdoug:
Right, without pull-up resistors your inputs are floating and undefined when the switch is open.

During setup() enable the internal pull-ups for all of the input-pins you're using.

pinMode(pin, INPUT);           // set pin to input

digitalWrite(pin, HIGH);      // turn on pullup resistors




I understand what you're trying to say and this doesn't affect what your program is doing, but 'STATE' is always HIGH because you've defined it that way. ;) 

And 'STATE' is not a good name for something that never changes state, and you could just use HIGH instead of defining a constant. ;)

Can't I just use "INPUT_PULLUP"? Rather than "digitalWrite(pin, HIGH)"?

With this code (I got rid of “STATE” and I used the aforementioned digitalWrite method) nothing happens when I press a button.

#define BLACK_SWITCH 7
#define MEDIUM_GREEN 6
#define MEDIUM_BLACK_1 5
#define MEDIUM_BLACK_2 4
#define RED_4 3
#define RED_3 2
#define RED_2 8
#define RED_1 9

void setup() {
  Serial.begin(9600);
for (int pinNumber; pinNumber < 10; pinNumber++) {
  pinMode(pinNumber, INPUT);
}
for (int pinNumber; pinNumber < 10; pinNumber++) {
  digitalWrite(pinNumber, HIGH);
}

}

void loop() {
if (digitalRead(BLACK_SWITCH) == LOW) {
  Serial.println("A");
}
if (digitalRead(MEDIUM_GREEN) == LOW) {
  Serial.println("B");
}
if (digitalRead(MEDIUM_BLACK_1) == LOW) {
  Serial.println("C");
}
if (digitalRead(MEDIUM_BLACK_2) == LOW) {
  Serial.println("D");
}
if (digitalRead(RED_4) == LOW) {
  Serial.println("E");
}
if (digitalRead(RED_3) == LOW) {
  Serial.println("F");
}
if (digitalRead(RED_2) == LOW) {
  Serial.println("G");
}
if (digitalRead(RED_1) == LOW) {
  Serial.println("H");
}
}

Please show us how your switches are connected.

Sorry for the poor image quality.

As you can see, I copper wire runs through 1 pin of each switch, with one individual wire coming out of the other pin of the switch. This individual wire plugs into a digital pin on the Arduino

for (int pinNumber; pinNumber < 10; pinNumber++) {

You didn’t give any starting vlue for pinNumber. So you really don’t know if it made any of your pins INPUT. It will have whatever value was left over in the memory address it gets. You don’t know that it didn’t start out at 31 and never ran the for loop at all.

"Can't I just use "INPUT_PULLUP"? Rather than "digitalWrite(pin, HIGH)"?".

The first defines the electrical configuration of the pin. The second actually sets the voltage level on that pin. Related, but not even close to being the same. Read the documentation.

Paul

Paul_KD7HB:
“Can’t I just use “INPUT_PULLUP”? Rather than “digitalWrite(pin, HIGH)”?”.

The first defines the electrical configuration of the pin. The second actually sets the voltage level on that pin. Related, but not even close to being the same. Read the documentation.

Paul

Thanks for the info!

Delta_G:

for (int pinNumber; pinNumber < 10; pinNumber++) {

You didn’t give any starting vlue for pinNumber. So you really don’t know if it made any of your pins INPUT. It will have whatever value was left over in the memory address it gets. You don’t know that it didn’t start out at 31 and never ran the for loop at all.

I will change my code to “int pinNumber = 0;” rather than “int pinNumber;”. Is this what you mean?

Attached is a quick little diagram. It shows pretty-much the same circuit, but with 4 buttons, just as an example.

stupid-questions:
Thanks for the info!

I will change my code to "int pinNumber = 0;"

Do you want it to start with pin 0? That wouldn't match with the pin numbers you defined at the top of the sketch.

stupid-questions:
Sorry for the poor image quality.

As you can see, I copper wire runs through 1 pin of each switch, with one individual wire coming out of the other pin of the switch. This individual wire plugs into a digital pin on the Arduino

...and what is the copper wire connected to? Ground? Are you sure it's really grounded?

Delta_G:
Do you want it to start with pin 0? That wouldn't match with the pin numbers you defined at the top of the sketch.

Oh, right. "pinNumber = 2" then.

Paul_KD7HB:
"Can't I just use "INPUT_PULLUP"? Rather than "digitalWrite(pin, HIGH)"?".

The first defines the electrical configuration of the pin. The second actually sets the voltage level on that pin. Related, but not even close to being the same. Read the documentation.

Paul

I think you should either have another look at the doc or maybe show us where you are finding the difference. Using INPUT_PULLUP is exactly equivalent to setting a pin to INPUT and then writing it HIGH.

aarg:
...and what is the copper wire connected to?

(assistant - forceps please :wink: )

Pin one of each button, and then GND.

EH!!!! It's working! Now all I have to do is clean up the wires (insulate the exposed copper so it doesn't interfere with anything) and then I will be good-to-go!

Thank you everyone!

Delta_G:
Using INPUT_PULLUP is exactly equivalent to setting a pin to INPUT and then writing it HIGH.

As evidenced here:

From wiring_digital.c (version 1.6.5r5 but I doubt it has changed)

void pinMode(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *reg, *out;

	if (port == NOT_A_PIN) return;

	// JWS: can I let the optimizer do this?
	reg = portModeRegister(port);
	out = portOutputRegister(port);

	if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out &= ~bit;
		SREG = oldSREG;
	} else if (mode == INPUT_PULLUP) {
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out |= bit;
		SREG = oldSREG;
	} else {
		uint8_t oldSREG = SREG;
                cli();
		*reg |= bit;
		SREG = oldSREG;
	}
}