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;
}