Two button two option

Hi, i have made this part of code:

  //Btn1 = 8, Btn2 = 12
  while(digitalRead(8) == LOW && digitalRead(12) == LOW){
    Btn1Stato = digitalRead(Btn1);
    Btn2Stato = digitalRead(Btn2);
    Serial.print("loop ");
  }
  
  delay(1000);
  Btn1Stato = digitalRead(Btn1);
  Btn2Stato = digitalRead(Btn2);
  delay(200);
  Serial.print("after loop ");
  if(Ispressed(Btn2) == true){
    Serial.print("choosen option 2");
    imp2();
  }else{ 
    Serial.print("choosen option 1");
    imp1_1();         
  }

But the problem is that don't work as i want, i would that if i press Btn1 does something, and if i click Btn2 does somethingelse. Here is the function "Ispressed":

bool Ispressed(int pin_button){
  int stato = 0;
  delay(50);
  stato = digitalRead(pin_button);
  delay(50);
  if(stato == HIGH){
    return true;  
  }
}

So, if you have a better idea you can help me remake this part of code. What i want is that with 2 buttons, if i click btn2, start function "imp2();", and if i click btn1, start function "imp1_1();", but i want that the code stop untill one of the two button is clicked, thanks.

What value is returned if stato is not HIGH?

mmm, now im gonna try add "else false"

nothing special, if i add this the two button go to function imp1_1()

I think the problem is that only button btn1 is HIGH and btn2 no, like if it stuck

consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

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

            delay (10);     // debounce

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

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

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

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

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

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

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

So, very interesting your answer, i tryed to apply making the necessary changes. But after the twentieth internet search I found this video: https://www.youtube.com/watch?v=ZsSJdGdLF-A&t=8s&ab_channel=TasteTheCode

In practice he reads the voltage that passes between the buttons and based on what is pressed Arduino reads the voltage and determines which button was pressed through a very simple IF:

void loop()
{
  int value = analogRead(A0);
  if (value > 800 && value < 1000) {
    Serial.println("Button1");
  } else if (value > 750 && value < 800) {
    Serial.println("Button2");
  } else if (value > 650 && value < 750) {
    Serial.println("Button3");
  } else if (value > 480 && value < 650) {
    Serial.println("Button4");
  } else if (value < 480) {
    Serial.println("Button5");
  }
  delay(500);
}

Hope this is usefull for somebody trying do make a menu with submenu. So i created this:

  while(analogRead(A1) > 700){
    Serial.println("inloop");
  }
  
  delay(1000);
  rifai:
  btn = analogRead(A1);
  Serial.println("out");
  if(btn > 480 && btn < 650){ //sinistra
    Serial.println("1");
    imp1_1();
  }else if (btn < 480){
    Serial.println("2");
    imp2();         
  }
  else
    goto rifai;

Yeah, i know, goto are bad... but i was lazy <3 Bye

the buttons connect different size resistors to a voltage divider where the voltage is measured using a single pin. i've seen this on vacuum cleaners

don't you have buttons connected to separate pins?

I just remaked this part of code, unplugging the two pins. Now I just use one pin of analog

the code above could be simpler

  int value = analogRead(A0);

  if (800 < value)
    Serial.println("Button1");
  else if (750 < value)
    Serial.println("Button2");
  else if (650 < value)
    Serial.println("Button3");
  else if (480 < value)
    Serial.println("Button4");
  else
    Serial.println("Button5");

    delay (50);

@Tiziano_Faverzani
A: 1. You have a system with two buttons that are connected with external pull-down resistors. Correct? (The buttons can be connected with internal pull-up resistors enabled, Fig-1.)

2. Your system should not do anything until one of the buttons is activated/pressed. Correct?

3. If Button1 is pressed you execute the routine named: imp1_1(). Correct?
4. If Button2 is pressed you execute the routine named: imp2(). Correct?

B: Then why don't you proceed to wrtite the following sequential codes to realize your objectives?
1. Connect your buttons as per Fig-1 with internal pull-up resistors.

sw2led2
Figure-1:

2. Let us agree that L (Built-in LED of UNO) will blink for three times when Button-1 is closed. This is your function named: imp1_1().

3. Let us agree that LED1 will blink for five times when Button-2 is closed. This is your function named: imp2().

4. Upload the following sketch and check the functionality of the circuit by pressing Button1 and Button2.

#define L 13
#define LED2 6

void setup() 
{
  Serial.begin(9600);
  pinMode(8, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(L, OUTPUT);
  pinMode(LED2, OUTPUT);
  Serial.println(!Button1.read(), BIN);
}

void loop() 
{
  while((digitalRead(8) == HIGH) && (digitalRead(12) == HIGH))
  {
    ;    //wait until one of the buttons is closed
  }
  if(digitalRead(8) == LOW)
  {
    imp(L, 3);  //Button1 is pressed, blink L 3 times
  }
  if(digitalRead(12)== LOW)
  {
    imp(LED2, 5);   //Button2 is pressed, blink LED2 for 5 times
  }
}

void imp(int m, int n)
{
  for(int i=0; i<n; i++)
  {
    digitalWrite(m, HIGH);
    delay(1000);
    digitalWrite(m, LOW);
    delay(1000);
  }
}

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