Uno - Button problem i don't understand

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

Pins 0 and 1 are the hardware Serial RX and TX pins. If you use Serial you should not use those pins for anything.

Do you have pull down resistors connected to the switch inputs? Without the pull down resistors, the input is floating (undefined state) when the button is not pressed.

See the state change tutorial.

Karma for code tags on first post. Not many bother to read the guidelines.

Your code is calling you a liar. It says the buttons are connected to pins 1 and 9. No pin 0.

Try it with pins 9 and 10 and report back.

BTW why do you have functions case0() and case1() which are never used? Just to confuse anyone trying to read your code?

Steve

Here is a better way to wire normally open momentary switches (buttons). You can use the internal pull up resistors this way and the cap adds debouncing. Note that the input will be HIGH when the button is not pressed and LOW when pressed. Adjust logic as necessary.

digin.jpg

groundFungus:
Pins 0 and 1 are the hardware Serial RX and TX pins. If you use Serial you should not use those pins for anything.

Do you have pull down resistors connected to the switch inputs? Without the pull down resistors, the input is floating (undefined state) when the button is not pressed.

See the state change tutorial.

Karma for code tags on first post. Not many bother to read the guidelines.

i did not use a pull down resistor since i did not need it for another project i did along with a tutorial - i will try this right away

slipstick:
Your code is calling you a liar. It says the buttons are connected to pins 1 and 9. No pin 0.

Try it with pins 9 and 10 and report back.

BTW why do you have functions case0() and case1() which are never used? Just to confuse anyone trying to read your code?

Steve

sorry, i seed to have copied the code when i was trying another pin to compare - that obviously did not make my code more readable.
the functions case0 and case1 are exempted in the main switch for testing, i left them in the code so whoever tried to read it would have more of an idea what i was trying to do… but i understand that that this makes the code more difficult to read - i will remember this when posting in the future

groundFungus:
Here is a better way to wire normally open momentary switches (buttons). You can use the internal pull up resistors this way and the cap adds debouncing. Note that the input will be HIGH when the button is not pressed and LOW when pressed. Adjust logic as necessary.

digin.jpg

i will look into this

thanks to all of you for responding so fast

To enable the internal pullups, use the pinMode(inputPin, INPUT_PULLUP) function.

thank you everone, the pull down resistor works,

next i will try to understand the diagram groundFungus posted

I will be happy to help you understand if you have questions.

edit: i think this belongs in a new thread

sorry, double post