button push counter with 4 switches

As a newbee I have trying to writing a program button push counters with 4 switches.
but I do not manage to make a 4 switch counter which creates a column with the values ​​of the switches each time when switch 1, 2, 3 or 4 is pressed. I have attached the program. My program will not stop after I press a button and it generates random values. What do I wrong. Can somebody gif me a hint.

The column to be created should look like this, every time when a switch is pressed

switch1 switch2 switch3 switch4
1 0 0 0
0 1 0 0
0 0 0 1

My program

================================================================

// constant
const int buttonPin1 = 2; // the pin that the pushbutton is attached to
const int buttonPin2 = 3; // the pin that the pushbutton is attached to
const int buttonPin3 = 4; // the pin that the pushbutton is attached to
const int buttonPin4 = 12; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button

int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0; // previous state of the button

int buttonPushCounter3 = 0; // counter for the number of button presses
int buttonState3 = 0; // current state of the button
int lastButtonState3 = 0; // previous state of the button

int buttonPushCounter4 = 0; // counter for the number of button presses
int buttonState4 = 0; // current state of the button
int lastButtonState4 = 0; // previous state of the button

void setup() {
// initialize the button pin1 as a input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);

// initialize the LED as an output:
pinMode(ledPin, OUTPUT);

// initialize serial communication:
Serial.begin(9600);
Serial.print("Switch nr1: ");
Serial.print("Switch nr2: ");
Serial.print("Switch nr3: ");
Serial.print("Switch nr4:");
Serial.println();
}

void loop() {
// read the pushbutton1 input pin:
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH) {
buttonPushCounter1 = 1;
Serial.print(" ");
Serial.print(buttonPushCounter1);
Serial.print(" ");
Serial.print(buttonPushCounter2);
Serial.print(" ");
Serial.print(buttonPushCounter3);
Serial.print(" ");
Serial.print(buttonPushCounter4);
Serial.println();
}
else {
buttonPushCounter1 = 0;
}
// Delay a little bit to avoid bouncing
delay(100);
lastButtonState1 = buttonState1;

// read the pushbutton2 input pin:
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH) {
buttonPushCounter2 = 1;
Serial.print(" ");
Serial.print(buttonPushCounter1);
Serial.print(" ");
Serial.print(buttonPushCounter2);
Serial.print(" ");
Serial.print(buttonPushCounter3);
Serial.print(" ");
Serial.print(buttonPushCounter4);
Serial.println();
}
else {
buttonPushCounter2 = 0;
}
// Delay a little bit to avoid bouncing
delay(100);
lastButtonState2 = buttonState2;

// read the pushbutton3 input pin:
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
buttonPushCounter3 = 1;
Serial.print(" ");
Serial.print(buttonPushCounter1);
Serial.print(" ");
Serial.print(buttonPushCounter2);
Serial.print(" ");
Serial.print(buttonPushCounter3);
Serial.print(" ");
Serial.print(buttonPushCounter4);
Serial.println();
}
else {
buttonPushCounter3 = 0;
}
// Delay a little bit to avoid bouncing
delay(100);
lastButtonState3 = buttonState3;

// read the pushbutton4 input pin:
buttonState4 = digitalRead(buttonPin4);
if (buttonState4 == HIGH) {
buttonPushCounter4 = 1;
Serial.print(" ");
Serial.print(buttonPushCounter1);
Serial.print(" ");
Serial.print(buttonPushCounter2);
Serial.print(" ");
Serial.print(buttonPushCounter3);
Serial.print(" ");
Serial.print(buttonPushCounter4);
Serial.println();
}
else {
buttonPushCounter4 = 0;
}
// Delay a little bit to avoid bouncing
delay(100);
lastButtonState4 = buttonState4;
}

The first thing that I would suggest is that you use arrays to simplify the code

Secondly, how are the inputs wired ?
Do you have pulldown resistors or are the inputs floating at random voltages ?

First, you need a pull-down or pull-up resistor on the inputs. The easy way to do this is to use INPUT_PULLUP mode and to wire the button to ground. If you don’t do this, you can get random noise.

Second, when a button is pushed it will stay pushed for a small amount of time. Rather than simply checking if the button is HIGH, you need to check if the button has just become HIGH. To do this, you store the previous state of the button in a variable outside your loop() function (or use a static inside it).

Finally, please read the sticky posts on this forum which explain how to correctly post code.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

My excuse for the inconvenience the way I have sent my program. Next time I will do it according to the rules. Sorry :confused:

In response to your question I use 10 k pull-down resistors at the inputs 2.3.4 and 13 of the Arduino.

Connected to arduino.png

Hi,
Ops circuit.
Connected to arduino.png

Can you post a complete circuit please?

If you do not push any buttons, your monitor still shows buttons being pushed?

Can you post a picture of your project please?
So we can see your component layout.

Thanks.. Tom.. :slight_smile: