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);
}
}