I am trying to build a Whack-a Mole like game using servos to move the heads up and down. I have been practicing with Arduino for about a year. This is the first time that I am trying to use "loops within loops" and I have tried multiple ways (if,else-if, else...switch...for...while...and do-while) opportunities to get a result. I have referenced multiple texts and read multiple examples on the forum.
The hardware is 3 servos, and 4 pushbuttons, and I am using an Arduino Uno r3. The desired output is that when the player is initially prompted to do so, they will press each of the three buttons (reading 1 through 3) and the heads will each raise for one second and then drop. This part of the code works fine.
The 4th push button is used to start a loop. when the player is presented with a case (which is on a laptop). The player pushes the 4th button, and the heads begin to move up and down, until a choice is made on one of the three choice buttons (reading 1 through 3). At which point all of the heads drop, and the loop is broken so that even if the number of heads routines planned is not completed, the heads all drop (ideally it would also raise the selected choice, and I have code written for this, but I can't seem to break the loop). Then the loop should return for the next case, where the player would choose to press pushbutton 4 again, and the whole cycle repeats, waiting for a choice (reading 1-3).
My issue is that my code executes the loop (DO-WHILE), but I am unable to break the loop (IF statements with BREAKS), when the buttons are pressed. It just continues to loop until the number of loops (heads) completes.
Any help is truly appreciated
/* This sketch controls a servo-controlled Whack a Mole game with 3 servos and 3 push buttons and a starter button with an ARDUINO UNO
by Chad Jackson
It is controlled by four push buttons
Pressing Button A raises Head A
Pressing Button B raises Head B
Pressing Button C raises Head C
Pressing Button D starts the heads moving up and down individually for 15 seconds
*/
#include<Servo.h> // include Servo library
int heads = 0;
int inPin1 = 1; // the switch is wired to Arduino D1 pin
int inPin2 = 2; // the switch is wired to Arduino D2 pin
int inPin3 = 4; // the switch is wired to Arduino D4 pin
int inPin4 = 7; // the switch is wired to Arduino D7 pin
int reading1; // the current reading from the input pin 1 Swtich A
int reading2; // the current reading from the input pin 2 Switch B
int reading3; // the current reading from the input pin 4 Switch C
int reading4; // the current reading from the input pin 7 Switch to initiate heads moving
Servo myservoA; // create servo motor object A
Servo myservoB; // create servo motor object B
Servo myservoC; // create servo motor object C
void setup()
{
myservoA.attach(3); // attach servo motor to pin 3 of Arduino
myservoB.attach(5); // attach servo motor to pin 5 of Arduino
myservoC.attach(6); // attach servo motor to pin 6 of Arduino
pinMode(inPin1, INPUT); // make pin 1 an input
pinMode(inPin2, INPUT); // make pin 2 an input
pinMode(inPin3, INPUT); // make pin 4 an input
pinMode(inPin4, INPUT); // make pin 7 an input
myservoA.write(55); // rotate servo motor A to 45 degrees or HOME
myservoB.write(55); // rotate servo motor B to 45 degrees or HOME
myservoC.write(55); // rotate servo motor C to 45 degrees or HOME
}
void loop() { //WAITING FOR SWTICH INPUT
reading1 = digitalRead(inPin1); // store digital data in variable
reading2 = digitalRead(inPin2); // store digital data in variable
reading3 = digitalRead(inPin3); // store digital data in variable
reading4 = digitalRead(inPin4); // store digital data in variable
myservoA.write(55); // servo motor Lowers Head A
myservoB.write(55); // servo motor Lowers Head B
myservoC.write(55); // servo motor Lowers Head C
if (reading1 == HIGH) { // check to see if switch ONE has been pushed
//Raise Head ONE
myservoA.write(135); // servo motor rotates 150 degrees to Raise Head ONE
delay(1000); // wait 1000ms for rotation
myservoA.write(55); // servo motor Lowers Head A
myservoB.write(55); // servo motor Lowers Head B
myservoC.write(55); // servo motor Lowers Head C
}
if (reading2 == HIGH) { // check to see if switch TWO has been pushed
//Raise Head TWO
myservoB.write(135); // servo motor rotates 150 degrees to Raise Head Two
delay(1000); // wait 1000ms for rotation
myservoA.write(55); // servo motor Lowers Head A
myservoB.write(55); // servo motor Lowers Head B
myservoC.write(55); // servo motor Lowers Head C
}
if (reading3 == HIGH) { // check to see if switch THREE has been pushed
//Raise Head Three
myservoC.write(135); // servo motor rotates 150 degrees to Raise Head THREE
delay(1000); // wait 1000ms for rotation
myservoA.write(55); // servo motor Lowers Head A
myservoB.write(55); // servo motor Lowers Head B
myservoC.write(55); // servo motor Lowers Head C
}
if (reading4 == HIGH) { //Moves heads up and down
int heads = 1;
do {
if (reading1 == HIGH)
break;
if (reading2 == HIGH)
break;
if (reading3 == HIGH) // check to see if a switch has been pushed
break;
//MOVES Servos
myservoA.write(135); // servo motor Raises Head A
delay(500); // wait 500ms for raising head
myservoA.write(55); // servo motor Lowers Head A
delay(500); // wait 500ms for lowering head
myservoB.write(135); // servo motor Raises Head B
delay(500); // wait 500ms for raising head
myservoB.write(55); // servo motor Lowers Head B
delay(500); //wait 500ms for lowering head
myservoC.write(135); // servo motor Raises Head C
delay(500); // wait 500ms for rotation
myservoC.write(55); // servo motor Lowers Head C
delay(500); //wait 500ms for lowering head
heads = heads + 1;
} while (heads != 5);
//Program loops to top, waits for Button
}
}