Macro keypad: pull down resistor code to avoid floating input

Hi, I'm trying to use an Arduino Pro Micro as a macro keypad and started by following to tutorial here: Make Your Own Custom Shortcut Buttons With an Arduino

I'm getting random values which I've come to find are due to a floating input. The schematic shows a pulldown resistor being used, however there is no mention of it in the code. This is the first time I've attempted a project like this and would appreciate any guidance regarding how and where to add the pulldown code, the last time I wrote any form of programming was 20 years ago in C++ at a very basic level.

const int buttons[] = {2,3,4,5,6,7,8,9,10}; // array of all button pins

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
for(int i = 2; i < 11; ++i) {
if(digitalRead(i) == HIGH) {
// software de-bounce improves accuracy
delay(10);
if(digitalRead(i) == HIGH) {
// check switches
Serial.print ("input");
Serial.println(i);
delay(250);
}
}
}
}

See the 10K resistors here (Brown Black Orange)

Read these:
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

FYI
Your switches are wired as S1 in this schematic.

.

Why would you expect there be a mention of pull-downs in the code?

This is what I had as my current test circuit with external pull down resistors.

Reading the gammon article it seems that I don't need any additional code unless I want to use internal resistors. So, theoretically my current circuit shouldn't give me random values?

You have ONE pull-down resistor but read many pins?

You need one pull-down resistor per input.

Are you getting the +V for the buttons from the RAW pin? That may be your problem, try the VCC pin instead.
Well, I can see how that "schematic" could cause confusion, terrible. Where does the red wire from button #6 connect on the Mini? Etc.

Forget about using inconvenient external pulldown resistors and use
pinMode(pinNumber, INPUT_PULLUP); to activate the built in pullup resistors.

Wire the switches to take the inputs LOW when pressed and change the program logic to test for LOW when pressed.

Much neater

@sterretje: yes, just one for now. I wanted to test one pin/switch to see if I had set everything up ok.

@edgemoron: yes, I'm getting it from the RAW pin. I'll give the VCC pin a go.

@UKHeliBob: thanks for the suggestion, I'll try that too.

Thanks for the tips and feedback, electronics / programming has always been challenging to understand so I appreciate it!

Let's say I get this working with one of the suggested configurations (internal/external pull up/pulldown) and the appropriate code. How likely is it to get a false readout?

How likely is it to get a false readout?

Unlikely.

In a very noisy environment more likely.

.

Moved to the VCC pin from RAW, still getting random outputs. I don't quite understand why this is happening.

However, I changed the code to use the internal resistor - removed my external resistor - and now I'm getting the correct output from every pin. While I've solved my problem I'd still like to know why the external resistors didn't work.

Thanks for all your input!

Did you read the links in post #1?

If, after reading these two posts, you still don't understand the need for these resistors you have two choices.

  • just accept the requirement
  • learn electronics so you can understand what is happening.

.

Yes Larry, I did. I understand the need for resistors. My question isn't asking about the need for resistors. I'm asking why my external pulldown resistors didn't work. Based on all the information that was provided, they were wired correctly and the code was correct.