End a Statement if a Button is Pressed?

Hi Everyone,

I have a question about an else statement that I have. So as you can see from looking at my code, I have a spot where if the button is not being pressed, it runs through some text on my LCD screen that I have as well as some Serial output for debugging. I am looking for a way to have the else statement cut off at any point if the button on pin 10 is pressed. Otherwise right now I have to keep the button pressed down until the else part of the loop ends.

Thanks in Advance
Max

Code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int buttonPin = 10;                     // The number of the pushbutton pin
const int relayPin1 = 11;                    // the number of the Relay pin 1
const int relayPin2 = 12;                    // The number of the Relay pin 2
const int relayPin3 = 13;                    // The number of the Relay pin 3

int buttonState = 0;                         // variable for reading the pushbutton status
int lcd_key     = 0;

void setup() {
  Serial.begin(9200);
  pinMode(relayPin1, OUTPUT);                // Initialize the Relay pin 1 as an output
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);  
  pinMode(buttonPin, INPUT);                 // Initialize the pushbutton pin as an input
  Serial.println("Setup Complete");
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.println("System Ready");
  Serial.println("System Ready");
  delay(3000);
}

void loop(){
  lcd.setCursor(0, 1);
  buttonState = digitalRead(buttonPin);      // read the state of the pushbutton value
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {      
    digitalWrite(relayPin1, HIGH);           // turn Relay 1 on
    Serial.println("Button Pressed!");
    lcd.clear();
    lcd.print("Cannon 1 Fired!");
    Serial.println("Relay 1 ON");
    delay(500);
    digitalWrite(relayPin2, HIGH);
    lcd.clear();
    lcd.print("Cannon 2 Fired!");
    Serial.println("Relay 2 ON");
    delay(500);
    digitalWrite(relayPin3, HIGH);
    lcd.clear();
    lcd.print("Cannon 3 Fired!");
    Serial.println("Relay 3 ON");
    delay(500);
    lcd.clear();
    lcd.print("Cannons Fired!");
    Serial.println("ALL relays ON!");
  }
  else {
    digitalWrite(relayPin1, LOW);            // turn Relay 1 off
    Serial.println("Relay 1 OFF");
    digitalWrite(relayPin2, LOW);            // turn Relay 2 off
    Serial.println("Relay 2 OFF");
    digitalWrite(relayPin3, LOW);            // turn Relay 3 off
    Serial.println("Relay 3 OFF");
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Ready");
    delay(900);
    lcd.setCursor(1, 1);
    lcd.print("Waiting .");
    delay(900);
    lcd.setCursor(1, 1);
    lcd.print("Waiting ..");
    delay(900);
    lcd.setCursor(1, 1);
    lcd.print("Waiting ...");
    Serial.println("Waiting for Button-Press");
    delay(900);
  }
}

Well, you can re-arrange you program to achieve the same thing, without all those delays.

If you want to detect the button as soon as you press it, you can implement an interrupt function triggered by the button, which can establish a status that the button has been pressed.

If you then want to abort the sequence of outputs in the else clause, you would then need to check whether the button has been pressed, before each output statement in that section of the code.

How would that look in code terms?
I have not worked with any interrupt functions at all yet.

This will not let you see anything on the Serial monitor, Serial.begin(9200); but 9600 will.

He doesn't really need to use interrupts, but what he can do is use a simple timer and some case statements.
Something like this. NOTE: You need to put timer and Inc_Case at the top of your code. timer is unsigned long, and Inc_Case is int.

if( millis() - timer >= 500) 
{
  timer = millis();
  Inc_Case > 3 ? Inc_Case = 0 : Inc_Case++; // condensed IF/ELSE statement.
  switch(Inc_Case)
  {
     case 0:
         digitalWrite(relayPin1, HIGH);           // turn Relay 1 on
         Serial.println("Button Pressed!");
         lcd.clear();
         lcd.print("Cannon 1 Fired!");
         Serial.println("Relay 1 ON");
         break;
    
     case 1:
         digitalWrite(relayPin2, HIGH);
         lcd.clear();
         lcd.print("Cannon 2 Fired!");
         Serial.println("Relay 2 ON");
         break;

     case 2:
         digitalWrite(relayPin3, HIGH);
         lcd.clear();
         lcd.print("Cannon 3 Fired!");
         Serial.println("Relay 3 ON");
         break;

    case 3:
        lcd.clear();
        lcd.print("Cannons Fired!");
        Serial.println("ALL relays ON!");
        break;
  }
}

Now you can do that same thing for your else statement, but instead of 500, it's 900.