Trouble Separating Button Presses

Hello,

I would like to separate these two button presses. For example when I press Button 10 I Would like it to run the top section of code, then when I press button 11, I would like it to run the bottom section, right now when you press button 10 or 11, it starts with that section of code but then runs all of the code.

int run;
int BUTTON1;
int BUTTON2;

#define RELAY1  2                        

#define RELAY2  3                        

#define RELAY3  4                        

#define RELAY4  5

#define RELAY5  6

#define RELAY6  7

#define RELAY7  8

#define RELAY8  9

void setup()
{
  run = 0; //starts stopped
   BUTTON1 = 10;
   BUTTON2 = 11;//whatever pin your button is plugged into

  pinMode(BUTTON1, INPUT_PULLUP);
  
  pinMode(BUTTON2, INPUT_PULLUP);

  pinMode(RELAY1, OUTPUT);       

  pinMode(RELAY2, OUTPUT);

  pinMode(RELAY3, OUTPUT);

  pinMode(RELAY4, OUTPUT);

  pinMode(RELAY5, OUTPUT);

  pinMode(RELAY6, OUTPUT);

  pinMode(RELAY7, OUTPUT);

  pinMode(RELAY8, OUTPUT);
}

void loop()
{
  //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run

  //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
  if(digitalRead(BUTTON1) == LOW) //funcitons based off of button pulling input pin LOW
  {
     if(run == 0)
     {
         run = 255;
     }
     else
     {
         run = 0;
     }
  }

  if(run > 0)
  {

     digitalWrite(RELAY1,LOW);
   digitalWrite(RELAY2,LOW);
   digitalWrite(RELAY3,LOW);
   digitalWrite(RELAY4,LOW);
   digitalWrite(RELAY5,LOW);
   digitalWrite(RELAY6,LOW);
   digitalWrite(RELAY7,LOW);
   digitalWrite(RELAY8,LOW);   // Turns ON Relays 1

   delay(300);                                      // Wait 2 seconds


   digitalWrite(RELAY3,HIGH);          // Turns Relay Off

 

  

   delay(300);                                      // Wait 2 seconds

   digitalWrite(RELAY4,HIGH);          // Turns Relay Off 





   delay(300);                                      // Wait 2 seconds

   digitalWrite(RELAY5,HIGH);          // Turns Relay Off 


   delay(300);                                      // Wait 2 seconds

   digitalWrite(RELAY6,HIGH);          // Turns Relay Off 

  

   delay(300);                                      // Wait 2 seconds

   digitalWrite(RELAY7,HIGH);          // Turns Relay Off 

 

   delay(300);                                      // Wait 2 seconds

   digitalWrite(RELAY8,HIGH);          // Turns Relay Off 

   delay(300);

   digitalWrite(RELAY8,LOW);

   delay(300);

   digitalWrite(RELAY8,HIGH);

   delay(300);

   digitalWrite(RELAY8,LOW);

   delay(300);

  digitalWrite(RELAY8,HIGH);

   delay(750);
     //code you only run if button was pressed, stops running when button pressed again, so forth...
  }

  if(digitalRead(BUTTON2) == LOW) //funcitons based off of button pulling input pin LOW
  {
     if(run == 0)
     {
         run = 255;
     }
     else
     {
         run = 0;
     }
  }

  if(run > 0)
  {

     digitalWrite(RELAY1,LOW);
   digitalWrite(RELAY2,LOW);
   digitalWrite(RELAY3,LOW);
   digitalWrite(RELAY4,LOW);
   digitalWrite(RELAY5,LOW);
   digitalWrite(RELAY6,LOW);
   digitalWrite(RELAY7,LOW);
   digitalWrite(RELAY8,LOW);   

   delay(400);                                      


   digitalWrite(RELAY5,HIGH);
   digitalWrite(RELAY6,HIGH);

   delay(400);

   digitalWrite(RELAY4,HIGH);
   digitalWrite(RELAY7,HIGH);

   delay(400);

   digitalWrite(RELAY8,HIGH);
   digitalWrite(RELAY3,HIGH);

   delay(300);

   digitalWrite(RELAY8,LOW);
   digitalWrite(RELAY3,LOW);

   delay(300);

   digitalWrite(RELAY8,HIGH);
   digitalWrite(RELAY3,HIGH);

   delay(300);

   digitalWrite(RELAY8,LOW);
   digitalWrite(RELAY3,LOW);

   delay(300);

   digitalWrite(RELAY8,HIGH);
   digitalWrite(RELAY3,HIGH);

   delay(500);

     //code you only run if button was pressed, stops running when button pressed again, so forth...
  }
  
}

You need to read both buttons and save the values to variables and then decide what to do - something like

button1Val = digitalRead(button1pin);
button2Val = digitalRead(button2pin);

combinedVal = button1Val * 10 + button2Val;

if (combinedVal == 10) {

}
else if (combinedVal == 11) {

}
//etc

...R

Sorry,

This is my first project and I'm just learning C++, I am not sure how I would go about it after I had those combined values.

  if (digitalRead(BUTTON1) == LOW)  //assumes normally held HIGH and taken LOW when pressed
  {
    //run this code
  }
  else if (digitalRead(BUTTON2) == LOW)
  {
    //run this code
  }

BrendanE:
I am not sure how I would go about it after I had those combined values.

I'm confused. I thought you already knew how to do that part and it was just reading the buttons you needed help with.

What have you tried? Post your best attempt.

By the way I have assumed that when you said in your Original Post " For example when I press Button 10" that you wanted something to happen when the first button is pressed and the second is not. Because you do not have a button numbered 10 - only 1 and 2.

Maybe i misinterpreted.

...R

Thanks for your help,

My best attempt is pretty much what I have in my initial post. It works, but when I press button 10, it runs both the code I have under button 10, then proceedes to run the code under button 11. When I press either button 10 or 11, I would like it to loop the code under said button. And not run the other button.

Below a simplified version of your code to extract the logic flow

  if (digitalRead(BUTTON1) == LOW) //funcitons based off of button pulling input pin LOW
  {
    ...
    ...
  }

  if (run > 0)
  {
    ...
    ...
  }

  if (digitalRead(BUTTON2) == LOW) //funcitons based off of button pulling input pin LOW
  {
    ...
    ...
  }

  if (run > 0)
  {
    ...
    ...
  }

So if button1 is pressed (the first time), you set run to 255 and hence the block of the first if (run > 0) is executed; next you check button 2 (let's assume it's not pressed so the run variable does not change) and as a result the block of the second if (run > 0) is executed.

What is the thought behind the use of the 'run' variable?

BrendanE:
but when I press button 10,

What do you mean by "button 10"?

The code in your Original Post only has

int BUTTON1;
int BUTTON2;

Help us to help you.

...R

Robin2:
What do you mean by "button 10"?

The code in your Original Post only has

int BUTTON1;

int BUTTON2;




Help us to help you.

...R

look at the setup code :wink: No idea why OP uses that construction.

UKHeliBob:

  if (digitalRead(BUTTON1) == LOW)  //assumes normally held HIGH and taken LOW when pressed

{
    //run this code
  }
  else if (digitalRead(BUTTON2) == LOW)
  {
    //run this code
  }

These lines of code work perfectly, the only problem is that once the button is pressed, I would like it to run continuously until either it is turned off or a stop button is pressed. That is the other thing, I would like to be able to press button one, then transfer over to the code under button 2, right now if you press button one, you cannot run the code under button two unless you reset the arduino.

Edit

If I do press button one after its run its cycle, i only get the last couple lines of code to run. The top 3/4 dosent run, same thing if i press button one after its run once already.

once the button is pressed, I would like it to run continuously until either it is turned off or a stop button is pressed

Add a boolean variable, let's name it okToRun, and set it to false.
Alter the program to change the state of the variable when a button press is detected rather than run the code block
Alter the program to run the required code depending on the state of the variable (true or false)