Problem with reading large no. of digital pins

im making a arduino controller by sending a string of 11 values and using them via python keyboard module for keypresses, the problem is all of the digital read pins are always in the high state when viewed with serial moniter or python.
the code is this
//A1 --> yaxis
//A2 --> xaxis
//3 --> x
//4 --> c
//5 --> a
//6 --> s
//7 --> d
//8 --> esc
//9 --> tab
//10 --> space
//11 --> z

int inputPins[11] = {A0, A1, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int output[11];

void setup() {
Serial.begin(9600);
for(int i = 0; i < 12; i++){
pinMode(i, INPUT);
}
}

void loop() {
output[0] = analogRead(A0);
output[1] = analogRead(A1);
for(int i = 2; i < 12; i++){
output[i] = digitalRead(output[i]);
}
for(int i = 0; i < 11; i++){
Serial.print(output[i]);
Serial.print(" ");
}
Serial.println(output[11]);
delay(100);
}
the prompt from serial moniter

639 641 1 1 1 1 1 1 1 1 1 259
639 641 1 1 1 1 1 1 1 1 1 259
639 641 1 1 1 1 1 1 1 1 1 259
639 641 1 1 1 1 1 1 1 1 1 0
639 641 1 1 1 1 1 1 1 1 1 259
638 641 1 1 1 1 1 1 1 1 1 259
639 641 1 1 1 1 1 1 1 1 1 259

This is using the value in output[i] as the pin number for the digitalRead, did you mean to use inputPIns[i]?

The pinMode() line in setup() also appears to be wrong, but the pins are already in input mode anyway.

2 Likes

Since the size 11 array uses 0...10 indexing, the test should be "i < 11"

1 Like

How are the switches wired to the Arduino? If they are connected between the Arduino pin and Vcc (5V or 3.3V depending on which Arduino), then you need to define the input pin mode as INPUT_PULLUP, not INPUT

[Edit: See post #5 and #6. Should have said "If they are connected between the Arduino pin and Gnd (not Vcc)]

Oops. Input pullup is used when the switch is used to pull the pin low.

1 Like

Sorry, thanks for catching that. I shouldn't type and listen to children at the same time :wink:

1 Like

Yep. Watching the news will do that every time!

thank you for the help, yep i seem to worng in the output section, but what is wrong in the pinmode statement?

yep it should be 11, but i got confused when initializing the array when i put 10 in the square it gave me an error.

they are wired from a breadboard of switches to the arduino, at first i thought it was some interference cause by the presence of uncertain wiring but it didnt go away when a made sure everything was right

  • Why not ?
    digitalRead(14);
    digitalRead(15);

  • Okay, these are analog inputs then ?

yep they are analog inputs from a joystick

  • On these two inputs do not turn on the internal pull up resistors

The pinMode statement is setting pins 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11 to input mode.
This only partially matches the pins that you are using as inputs, and pins 0 and 1 are used by Serial for the transmit and receive pins.

There is no need to set any pins to INPUT mode, they default to that mode when the Arduino resets.

1 Like

  • Wire switches as S3 is wired above, Note, internal pull-ups are turned ON.
    Reading an input gives a LOW if the switch is closed.

for(int i = 0; i < 12; i++){
pinMode(i, INPUT);
}


for(int i = 2; i < 11; i++){
pinMode(inputPins[i], INPUT_PULLUP);
}

1 Like

oh understood

thanks, but why use input_pullup?

can you give me more context on the diagram and where to find information on it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.