Hello Everyone,
as you will see in a few moments when you read my code i am a total noob at coding,
right now i am trying to wrap my head around something i do not quite understand.
so i have a very simple setup where i would like to have two buttons connected to digtial 0 and digital 1
the button connects the pin to ground.
this seems to work fine for pin 0, but when i try to do the same thing on pin 1 it does not work
when i have the code below running with button 1 connected to pin 0 i can switch between the set cases in the main switch statement. but when i change the same button pin to 1 the serial output shows me that it is cycling through all the modes by itself without any button interaction.
//constants that won't change:
const int buttonPin = 9; // the pin that the pushbutton1 is attached to
const int buttonPin2 = 1; // the pin that the pushbutton2 is attached to
// Variables will change:
int buttonPushCounter = 0; // counters for the number of button presses
int buttonPushCounter2 = 0;
int buttonState = 0; // current states of the buttons
int buttonState2 = 0;
int lastButtonState = 0; // previous states of the buttons
int lastButtonState2 = 0;
void setup() {
// initialize the button pins as inputs:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
}
}
switch (buttonPushCounter)
{
case 0:
Serial.println("Case 0");
//case0();
break;
case 1:
Serial.println("Case 1");
//case1();
break;
case 2:
Serial.println("Case 2");
break;
case 3:
Serial.println("Case 3");
break;
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == 4)
buttonPushCounter=0;
}
// ########## Case 0 ##############
void case0()
{
buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter2++;
}
}
switch (buttonPushCounter2)
{
case 0:
Serial.println("Case 0-0");
break;
case 1:
Serial.println("Case 0-1");
break;
}
lastButtonState2 = buttonState2;
if (buttonPushCounter2 == 2)
buttonPushCounter=0;
}
// ########## Case 1 ##############
void case1()
{
// compare the buttonState to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter2++;
}
}
switch (buttonPushCounter2)
{
case 0:
Serial.println("Case 1-0");
break;
case 1:
Serial.println("Case 1-1");
break;
}
lastButtonState2 = buttonState2;
if (buttonPushCounter2 == 2)
buttonPushCounter=0;
}
i do not understand why this is happening...
thank you for your help
edit: this does also happen on other digtial pins