serial.print junk while counting button presses

Physwiz:
I’ve some new switches coming to see if they are snappier, ie capture the state change more cleanly.

The usual method is to add debouncing code. Essentially, if the button changes state, then you ignore any other changes for - say - 50ms.

byte buttonState;
uint32_t buttonChangeTime;

loop() {

  if(millis() - buttonChangeTime >= 50) {
    byte buttonStateNow = digitalRead(pin);
    if(buttonStateNow != buttonState) {
      buttonChangeTime = millis();
      buttonState = buttonStateNow;
    }
  }

  // and continue on from here;
}

What value of resister do you suggest? I’m using 1K at present.

The arduino has an internal pull-up resistor of (I think) 5k. You use it by selecting pinMode INPUT_PULLUP. This means that rather than your button going between the pin and 5v, the button shorts the pin to ground. This is a useful way to do things because ground is usually not very far away, especially if your project is in a metal box.

This means that your button is pressed when it is LOW, rather than when it is HIGH.

But if you already have a pull-down resistor, then 1k is fine, and your problem is not noise.