Serial.Print question?

Hello, I have a simple project I started just an experiment to learn a few things. I was hoping for help with what should be a simple thing.

I have a arduino uno. Hooked to it are 4 buttons, and 4 LEDs. I just wanted to have each button toggle on and off the corresponding LED. I used the example that Jeremy Blum put together on his site, just added three more buttons.

Link to his tutorial; // it's great.

It works great, and I added his denouncing function. I'll post my code at the end of this message.

My first question is. Do I need a separate function for each debounced button? Or, can I write one function to debounce each. Right now in the code it's 4 separate functions. One for each button.

Next question. I would like it to print out the serial port the state of the button like "Button 1 ON" or OFF. I can get it to kind of work, but it constantly writes it out over and over. I figured I'd need some sort of for or while loop to do this but just can's seem to figure it out. I looked at the included example with the Arduino IDE under digital>Stage Change Detection. I could kind of get it to work. It would print out that the led was on, but also right after it would say it was off.

Here's the code without the Serial.print stuff, could someone please show me how they would do it.

Thank you very much in advance for any help.

int switchPin_1 = 2;
int switchPin_2 = 4;
int switchPin_3 = 7;
int switchPin_4 = 8;

int ledPin_1 = 3;
int ledPin_2 = 5;
int ledPin_3 = 6;
int ledPin_4 = 9;

boolean lastButton_1 = LOW;
boolean lastButton_2 = LOW;
boolean lastButton_3 = LOW;
boolean lastButton_4 = LOW;

boolean currentButton_1 = LOW;
boolean currentButton_2 = LOW;
boolean currentButton_3 = LOW;
boolean currentButton_4 = LOW;

boolean ledOn_1 = false;
boolean ledOn_2 = false;
boolean ledOn_3 = false;
boolean ledOn_4 = false;

void setup(){
  
  pinMode(switchPin_1, INPUT);
  pinMode(switchPin_2, INPUT);
  pinMode(switchPin_3, INPUT);
  pinMode(switchPin_4, INPUT);
  
  pinMode(ledPin_1, OUTPUT);
  pinMode(ledPin_2, OUTPUT);
  pinMode(ledPin_3, OUTPUT);
  pinMode(ledPin_4, OUTPUT);

  Serial.begin(9600);
  Serial.print("Serial Ready!");
  
}


void loop(){
  
  currentButton_1 = debounce_1(lastButton_1);    
  if (lastButton_1 == LOW && currentButton_1 == HIGH) {
    ledOn_1 = !ledOn_1;
  }
  lastButton_1 = currentButton_1;

  digitalWrite(ledPin_1, ledOn_1);

    
  currentButton_2 = debounce_2(lastButton_2);
  if (lastButton_2 == LOW && currentButton_2 == HIGH) {
    ledOn_2 = !ledOn_2;
  }
  lastButton_2 = currentButton_2;
  
  digitalWrite(ledPin_2, ledOn_2);

  

  currentButton_3 = debounce_3(lastButton_3);
  if (lastButton_3 == LOW && currentButton_3 == HIGH) {
    ledOn_3 = !ledOn_3;
  }
  lastButton_3 = currentButton_3;
  
  digitalWrite(ledPin_3, ledOn_3);

  

  currentButton_4 = debounce_4(lastButton_4);
  if (lastButton_4 == LOW && currentButton_4 == HIGH) {
    ledOn_4 = !ledOn_4;
  }
  lastButton_4 = currentButton_4;
  
  digitalWrite(ledPin_4, ledOn_4);
  

}

// debounce functions for each button.

boolean debounce_1(boolean last){
  
  boolean current = digitalRead(switchPin_1);
  if (last != current) {
    delay(5);
    current = digitalRead(switchPin_1); 
  }
  return current;    
}

boolean debounce_2(boolean last){
  
  boolean current = digitalRead(switchPin_2);
  if (last != current) {
    delay(5);
    current = digitalRead(switchPin_2);
  }
  return current; 
}

boolean debounce_3(boolean last){
  
  boolean current = digitalRead(switchPin_3);
  if (last != current) {
    delay(5);
    current = digitalRead(switchPin_3);
  }
  return current;  
}

boolean debounce_4(boolean last){
  
  boolean current = digitalRead(switchPin_4);
  if (last != current) {
    delay(5);
    current = digitalRead(switchPin_4);
  }
  return current;  
}

printing can be done this way

 if (lastButton_1 == LOW && currentButton_1 == HIGH) {
    ledOn_1 = !ledOn_1;
    if(ledOn_1==1){//add this code
      Serial.println("led one on");}
      else{
        Serial.println("led one off");
      }
  }
  lastButton_1 = currentButton_1;

as for the debounce that's a little harder. It can be done if you change your wiring to use pull_up

this involves connecting the arduino input to one side of the button and the other side of the button goes to the negative rail.

the code I posted has the changes made (I changed the highs and low using find and replace so you will have to proof read the program as I only checked the input side)

int switchPin_1 = 2;
int switchPin_2 = 4;
int switchPin_3 = 7;
int switchPin_4 = 8;

int ledPin_1 = 3;
int ledPin_2 = 5;
int ledPin_3 = 6;
int ledPin_4 = 9;

boolean lastButton_1 = HIGH;
boolean lastButton_2 = HIGH;
boolean lastButton_3 = HIGH;
boolean lastButton_4 = HIGH;

boolean currentButton_1 = HIGH;
boolean currentButton_2 = HIGH;
boolean currentButton_3 = HIGH;
boolean currentButton_4 = HIGH;

boolean ledOn_1 = false;
boolean ledOn_2 = false;
boolean ledOn_3 = false;
boolean ledOn_4 = false;

void setup() {

  pinMode(switchPin_1, INPUT_PULLUP);
  pinMode(switchPin_2, INPUT_PULLUP);
  pinMode(switchPin_3, INPUT_PULLUP);
  pinMode(switchPin_4, INPUT_PULLUP);

  pinMode(ledPin_1, OUTPUT);
  pinMode(ledPin_2, OUTPUT);
  pinMode(ledPin_3, OUTPUT);
  pinMode(ledPin_4, OUTPUT);

  Serial.begin(9600);
  Serial.print("Serial Ready!");

}


void loop() {

  currentButton_1 = debounce(lastButton_1, switchPin_1);
  // Serial.println(currentButton_1);
  // Serial.println(" ");
  // Serial.println(lastButton_1);
  if (lastButton_1 == HIGH && currentButton_1 == LOW) {
    ledOn_1 = !ledOn_1;
    if (ledOn_1 == 1) {
      Serial.println("led one on");
    }
    else {
      Serial.println("led one off");
    }
  }
  lastButton_1 = currentButton_1;

  digitalWrite(ledPin_1, ledOn_1);


  currentButton_2 = debounce(lastButton_2, switchPin_2);
  if (lastButton_2 == HIGH && currentButton_2 == LOW) {
    ledOn_2 = !ledOn_2;
    if (ledOn_2 == 1) {
      Serial.println("led two on");
    }
    else {
      Serial.println("led two off");
    }
  }
  lastButton_2 = currentButton_2;

  digitalWrite(ledPin_2, ledOn_2);



  currentButton_3 = debounce(lastButton_3, switchPin_3);
  if (lastButton_3 == HIGH && currentButton_3 == LOW) {
    ledOn_3 = !ledOn_3;
    if (ledOn_3 == 1) {
      Serial.println("led three on");
    }
    else {
      Serial.println("led three off");
    }
  }
  lastButton_3 = currentButton_3;

  digitalWrite(ledPin_3, ledOn_3);



  currentButton_4 = debounce(lastButton_4, switchPin_4);
  if (lastButton_4 == HIGH && currentButton_4 == LOW) {
    ledOn_4 = !ledOn_4;
    if (ledOn_4 == 1) {
      Serial.println("led four on");
    }
    else {
      Serial.println("led four off");
    }
  }
  lastButton_4 = currentButton_4;

  digitalWrite(ledPin_4, ledOn_4);


}

// debounce functions for each button.

boolean debounce(boolean last, int switchPin_A) {

  boolean current = digitalRead(switchPin_A);
  if (last != current) {

    current = digitalRead(switchPin_A);
  }
  return current;
}

Thank you very much. It works perfectly. I should have been able to figure this out, and felt foolish for asking. But, it fells great to have a forum like this where you can ask simple questions and get some great help.

You are awesome. Thanks again.

Have a blessed day. :slight_smile: