Serial monitor spamming

// Define the pin number where the buttons are connected
int button = 2;
int button_2 = 4;   
int button_3 = 6;
bool but_st = false;
bool but2_st = false;
bool but3_st = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  pinMode(button, INPUT);
  pinMode(button_2, INPUT);
  pinMode(button_3, INPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(button);
  if (reading != but_st) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    but_st = reading;

    reading = digitalRead(button_2);
    if (reading != but2_st) {
      lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {
      but2_st = reading;

      reading = digitalRead(button_3);
      if (reading != but3_st) {
        lastDebounceTime = millis();
      }

      if ((millis() - lastDebounceTime) > debounceDelay) {
        but3_st = reading;

        if (but_st == HIGH) {
          Serial.println("Button 1 pressed");
        } else if (but2_st == HIGH) {
          Serial.println("Button 2 pressed");
        } else if (but3_st == HIGH) {
          Serial.println("Button 3 pressed");
        }
      }
    }
  }

  
}

its a code to verify buttons but its just spamming in serial monitor "button pressed"

Welcome to the forum

How are your input pins wired ?

Is there anything keeping them LOW when the buttons are not pressed or are they floating at un unknown, possibly HIGH voltage ?

Read up on pullup and pulldown resistors and consider using INPUT_PULLUP in the pinMode()s for the button pins

You’ve got a real problem,. nowhere in your code does it print “Button pressed” by itself.

Maybe you haven’t uploaded the current code ?

thanks for responding, the pins are wired good and i use the resistor i will try use INPUT_PULLUP,thanks

Your sketch looks like it is taken from the Debounce example IDE >> FILES >> EXAMPLES >> 02.DIGITAL >> DEBOUNCE... but for that example to work, the button needs to be normally tied LOW, and when pressed results in a HIGH. You are probably seeing "pressed" because your button pin is tied HIGH.

You can see their drawing here:
schematic

The reference...

1 Like

Unless there is a VERY good reason to conect an input to Vcc (there isn't) you should always make the connection to gound, and use INPUT-PULLUP.

As explained here

Then a LOW will indicate the button has been pressed.

if you want to use millis() instead of a simple delay(), consider the following

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

// check multiple buttons and toggle LEDs

const byte    PinLeds [] = { 10, 11, 12 };
const byte    PinButs [] = { A1, A2, A3 };
const int     Nbut       =  sizeof(PinButs);

byte          butState [Nbut];
unsigned long msecButs [Nbut];

const unsigned long DebounceMsec = 20;

// -----------------------------------------------------------------------------
#define DebounceMsec    10
int
chkButtons ()
{
     unsigned long  msec = millis ();

    for (unsigned n = 0; n < sizeof(PinButs); n++)  {
        if (msecButs [n] && (msec - msecButs [n]) < DebounceMsec)
            continue;
        msecButs [n] = 0;

        byte but = digitalRead (PinButs [n]);
        if (butState [n] != but)  {
            butState [n] = but;

            msecButs [n] = msec ? msec : 1;      // make sure non-zero

            if (LOW == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (PinLeds [2], ! digitalRead (PinLeds [2]));
        break;

    case 1:
        digitalWrite (PinLeds [1], ! digitalRead (PinLeds [1]));
        break;

    case 0:
        digitalWrite (PinLeds [0], ! digitalRead (PinLeds [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(PinButs); n++)  {
        pinMode (PinButs [n], INPUT_PULLUP);
        butState [n] = digitalRead (PinButs [n]);
    }

    for (unsigned n = 0; n < sizeof(PinLeds); n++)  {
        digitalWrite (PinLeds [n], HIGH);       // off
        pinMode      (PinLeds [n], OUTPUT);
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.