Hello,
I think I have a simple problem, but I'm struggling with knowing the right terminology.
The Background:
I have 3 buttons which will control a number of functions in a lighted suit (think Ironman):
Button 1: When pressed one time, this should close the helmet, illuminate the suit in blue, and allow for the use of Buttons 2 and 3 ("Suit Initialized"). When pressed 2 times it should open the helmet and turn off all the lights.
Button 2: When pressed one time, illuminate the suit in a pulsing red ("Attack Mode"). When pressed two times return from any previous mode to the Initialized Mode.
Button 3: When pressed by itself, will begin "Scanning" mode. When pressed in combination with Button 2, do an action based on the number of button 3 presses.
I have not yet coded the lighting and helmet servos below, nor initialized Button 3.
The problem:
I'd like the suit to remain in whichever mode it is in until another button is pressed, at which point it enters the appropriate mode. Currently it loops continuously through initialization over and over, then when I enter "Attack Mode" it alternates between the two. This is a problem as it means the servos and lights would be continuously off-and-on.
What is the appropriate way to do this? Is it a sub-loop which continuously checks the button inputs just like at the top and then exits the sub-loop?
const byte button1Pin = A0; // pin for button 1
const byte button2Pin = A1; // pin for button 2
const byte button3Pin = A2; // pin for button 3
int button1State = 0; // current state of the button
int lastButton1State = 0; // previous state of the button
int button2State = 0; // current state of the button
int lastButton2State = 0; // previous state of the button
int button3State = 0; // current state of the button
int lastButton3State = 0; // previous state of the button
int button1PushCounter = 0; // counter for the number of button presses
int button2PushCounter = 0; // counter for the number of button presses
int button3PushCounter = 0; // counter for the number of button presses
void setup() {
Serial.begin(9600);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// read the pushbutton input pin:
button1State = digitalRead(button1Pin);
// compare the buttonState to its previous state
if (button1State != lastButton1State) {
// if the state has changed, increment the counter
if (button1State == HIGH) {
// if the current state is HIGH then the button went from off to on:
button1PushCounter++;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButton1State = button1State;
// If the button 1 push counter is 1, check buttons 2 and 3
if (button1PushCounter == 1) {
Serial.println("Suit Initialized");
button2State = digitalRead(button2Pin);
if (button2State != lastButton2State) {
// if the state has changed, increment the counter
if (button2State == HIGH) {
// if the current state is HIGH then the button went from off to on:
button2PushCounter++;
Serial.print("number of button 2 pushes: ");
Serial.println(button2PushCounter);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// button3State = digitalRead(button3Pin);
if (button3State != lastButton3State) {
// if the state has changed, increment the counter
if (button3State == HIGH) {
// if the current state is HIGH then the button went from off to on:
button3PushCounter++;
Serial.print("number of button 3 pushes: ");
Serial.println(button3PushCounter);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// Here we define what to do!
if (button2PushCounter == 1) {
Serial.println("Enter Attack Mode");
}
if (button3PushCounter == 1) {
Serial.print("Enter Scanning Mode");
}
if (button2PushCounter == 2) {
Serial.println("Exit Current Mode");
}
if (button2PushCounter == 1 & button3PushCounter == 1) {
Serial.print("Enter Party Mode");
}
if (button2PushCounter == 1 & button3PushCounter == 2) {
Serial.print("Enter X Mode");
}
if (button2PushCounter == 1 & button3PushCounter == 4) {
Serial.print("Enter Y Mode");
}
// save the current state as the last state, for next time through the loop
lastButton2State = button2State;
lastButton3State = button3State;
}
if (button1PushCounter == 2) {
Serial.print("Suit Shutdown");
button2PushCounter = 0;
button3PushCounter = 0;
button1PushCounter = 0;
}
}