3 buttons with multiple functions Arduino Uno

I have 3 buttons, button1, button2 and button3.

Each button is supposed to display a different output (in this case, its condensed, plum and milo).
However, if button1and button 2 is pressed one after another, I would like for it to have another output (we shall name it condensedPlum). Same case for button2 & button3 as well as button1 & button3.

This means if i were to click on button1, its output "condensed" is shown. If I were to click onto button2 next, its output "plum" is shown, and an output of "condensedPlum" is shown after.

This is the code that I have currently and I am having difficulty giving more functions to my button

const char button1 = 2;
const char button2 = 4;
const char button3 = 7;
bool pressed = false;
 
void setup() {
  Serial.begin(9600);
 
  // Setup pin modes
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
}
 
void loop() {
  // Read button
  bool condensed = digitalRead(button1);
  bool plum = digitalRead(button2);
  bool milo = digitalRead(button3);
 
  if (condensed == pressed) {
    Serial.println("condensed");
    while(digitalRead(button1) == pressed) {
      // Do nothing while button is pressed
    }
  }else if(plum == pressed){    
    Serial.println("plum");
    while(digitalRead(button2) == pressed) {
      // Do nothing while button is pressed
    }
      }else if(milo == pressed){    
    Serial.println("milo");
    while(digitalRead(button3) == pressed) {
      // Do nothing while button is pressed
    }
  }

}

Have a look at the "state change" sketch from the examples menu in the IDE.

Welcome to the forum.

Thank you for the code between code-tags and for the explanation :smiley:

The State Change Detection is also online: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection

Can you tell more ? When the next button is pressed, is the first button already released or are both pressed ? How much time is allowed between the buttons ?

To detect a sequence of events, you need to think in a logical way and put that in code.
Is it for school ?
For an experienced programmer, any sequence is no problem. The sequence can be put in data (in an array) or in code (with a Finite State Machine). For a beginner, you start to write down what it should do in a logical way.

Arduino has the setup() and loop() function. To keep it running smooth and responsive, the loop() has to run as often as possible, maybe hundreds or thousands times per second.
Waiting for something in a while-statement is not according to that.

Hello!

All buttons pressed would be released once pressed. There isn't a specific timing between the buttons but I thought of creating a code that I could use with touchdesigner for a personal project.

I would like audience to select two buttons in total, one after each, where a output would appear in between them. And the combination of 2 different buttons would result in another different output. I'm unsure of which function or how should I group them up logically.

I have this currently but I'm unsure if whatever is on it makes any sense


// setup pins
const char button1 = 2;
const char button2 = 4;
const char button3 = 7;
bool pressed = false;
 
int buttonState = 0;
int buttonPushcounter = 0;
int lastButtonpressed = 0; 

void setup() {
  Serial.begin(9600);
 
  // Setup pin modes
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
}
 
void loop() {
  // Read button
  bool condensed = digitalRead(button1);
  bool plum = digitalRead(button2);
  bool milo = digitalRead(button3);
  buttonState = condensed || plum || milo;

 if (buttonState != lastButtonpressed){
  if (condensed == pressed) {
    Serial.println("condensed");
    while(digitalRead(button1) == pressed) {
      // Do nothing while button is pressed
    }
  }else if(plum == pressed){    
    Serial.println("plum");
    while(digitalRead(button2) == pressed) {
      // Do nothing while button is pressed
    }
      }else if(milo == pressed){    
    Serial.println("milo");
    while(digitalRead(button3) == pressed) {
      // Do nothing while button is pressed
    }
  }
  delay(50);
  }

  lastButtonpressed = buttonState;

  if (buttonState != lastButtonpressed){
  if (condensed == pressed) {
    Serial.println("condensed");
    while(digitalRead(button1) == pressed) {
      // Do nothing while button is pressed
    }
  }else if(plum == pressed){    
    Serial.println("plum");
    while(digitalRead(button2) == pressed) {
      // Do nothing while button is pressed
    }
      }else if(milo == pressed){    
    Serial.println("milo");
    while(digitalRead(button3) == pressed) {
      // Do nothing while button is pressed
    }
  }
  delay(50);
  }else if(buttonState == lastButtonpressed){

  }

if (buttonPushcounter % 2 == 0){


}


}

You could add variables to make the code easier to understand.

void loop()
{
  // Part 1
  // Gather all information from buttons and input.
  // Put the result in variable(s).

  // Part 2
  // Process the information according to the variable(s).
}

If you have a global variable of the previous button, then you can check it against the current button.
For example:

// 0 = no button, 1 = condensed, 2 = plum, 3 = milo
int previousButton = 0;  

The code would be easier to read:

void loop()
{
  // Part 1
  // Gather all information from buttons and input.
  // Put the result in variable(s).
  ...

  // Part 2
  // Process the information according to the variable(s).
  if (previousButton == CONDENSED and currentButton == PLUM)
  {
    Serial.println("Condensed-Plum");
  }
}

That is just one way to do it, others write more condensed code than I do.

Are you using the Arduino IDE 2.0 ? Then please right-click in the sketch and select "Format document".

Hello bryanemerson

Here comes a sketch as proposal for you button project.

This is a button manager written in pure C. :face_vomiting:

// declare and init pins and variables
const int Button1 = 2;
const int Button2 = 4;
const int Button3 = 7; 
int button1State;
int button2State;
int button3State;

void setup()
{
  Serial.begin(9600);
  // configure pin modes
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
}

void loop()
{
  // Read buttons

  int stateNew = digitalRead(Button1) ? LOW : HIGH; // pinMode is set to INPUT_PULLUP
  delay(20); // Q&D debounce
  if (stateNew == digitalRead(Button1) ? LOW : HIGH)
  {
    if (stateNew != button1State)
    {
      button1State = stateNew;
      if (stateNew == HIGH) Serial.println(F("condensed"));
    }
  }
  stateNew = digitalRead(Button2) ? LOW : HIGH; // pinMode is set to INPUT_PULLUP
  delay(20); // Q&D debounce
  if (stateNew == digitalRead(Button2) ? LOW : HIGH)
  {
    if (stateNew != button2State)
    {
      button2State = stateNew;
      if (stateNew == HIGH) Serial.println(F("plum"));
    }
  }
  stateNew = digitalRead(Button3) ? LOW : HIGH; // pinMode is set to INPUT_PULLUP
  delay(20); // Q&D debounce
  if (stateNew == digitalRead(Button3) ? LOW : HIGH)
  {
    if (stateNew != button3State)
    {
      button3State = stateNew;
      if (stateNew == HIGH) Serial.println(F("milo"));
    }
  }
}

Have a nice day and enjoy coding in C++.

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