# Can someone silplify this simple code?

Hey

I'm making an escape room and I'm at a point I can do what I want, but as it is I have to code every puzzle from scratch and don't have universal code for it. I know this is easily done, but I don't know how.

Here is an example of a puzzle with 5 buttons. You need to press 3 of them in right order to solve it.

``````//Checks if the right switches are pressed down (green)

int GreenSwitch1 = 50;
int GreenSwitch2 = 51;
int GreenSwitch3 = 52;
int YellowSwitch1 = 30;
int YellowSwitch2 = 31;
int ledPin = 22;
boolean lastGreenSwitch1 = LOW;
boolean lastGreenSwitch2 = LOW;
boolean lastGreenSwitch3 = LOW;
boolean lastYellowSwitch1 = LOW;
boolean lastYellowSwitch2 = LOW;
int puzzleCount = 0;

void setup() {
// Set up switches as INPUTS and the LED as OUTPUT
pinMode(GreenSwitch1, INPUT);
pinMode(GreenSwitch2, INPUT);
pinMode(GreenSwitch3, INPUT);
pinMode(YellowSwitch1, INPUT);
pinMode(YellowSwitch2, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{

// Green buttons need to be pressed in order 1,2,3. Wrong order or any yellow button resets the puzzle.

//Green Switch 1
if (digitalRead(GreenSwitch1) == HIGH && lastGreenSwitch1 == LOW) //check if the button was pressed
{
delay(5); //Delay and check again for debounce
if (digitalRead(GreenSwitch1) == HIGH && lastGreenSwitch1 == LOW)
{
lastGreenSwitch1 = HIGH; //set the last known status to pressed
if (puzzleCount == 0) //check if the puzzle count is at right number for the button to be pressed
{
puzzleCount++; //advance the puzzle and...
Serial.println("Puzzle advanced to count: "); //...send message to console
Serial.println(puzzleCount);
}
else //wrong button pressed
{
puzzleCount = 0; //reset the puzzle and...
Serial.println("You failed. Puzzle reset."); //...send message to console
}
}
}
else
{
lastGreenSwitch1 = digitalRead(GreenSwitch1); //set the last known status to current status of the button
}

//Green Switch 2
if (digitalRead(GreenSwitch2) == HIGH && lastGreenSwitch2 == LOW)
{
delay(5); //Delay and check again to ensure debounce
if (digitalRead(GreenSwitch2) == HIGH && lastGreenSwitch2 == LOW)
{
lastGreenSwitch2 = HIGH;
if (puzzleCount == 1)
{
puzzleCount++;
Serial.println("Puzzle advanced to count: ");
Serial.println(puzzleCount);
}
else
{
puzzleCount = 0;
Serial.println("You failed. Puzzle reset.");
}
}
}
else
{
}

//Green Switch 3
if (digitalRead(GreenSwitch3) == HIGH && lastGreenSwitch3 == LOW)
{
delay(5); //Delay and check again to ensure debounce
if (digitalRead(GreenSwitch3) == HIGH && lastGreenSwitch3 == LOW)
{
lastGreenSwitch3 = HIGH;
if (puzzleCount == 2)
{
Serial.println("Puzzle Cleared!");
digitalWrite(ledPin, HIGH);
}
else
{
puzzleCount = 0;
Serial.println("You failed. Puzzle reset.");
}
}
}
else
{
}

//Yellow Switch 1 (Resets the puzzle)
if (digitalRead(YellowSwitch1) == HIGH && lastYellowSwitch1 == LOW)
{
delay(5); //Delay and check again to ensure debounce
if (digitalRead(YellowSwitch1) == HIGH && lastYellowSwitch1 == LOW)
{
lastYellowSwitch1 = HIGH;
puzzleCount = 0;
Serial.println("You failed. Puzzle reset.");
}
}
else
{
}

//Yellow Switch 2 (Resets the puzzle)
if (digitalRead(YellowSwitch2) == HIGH && lastYellowSwitch2 == LOW)
{
delay(5); //Delay and check again to ensure debounce
if (digitalRead(YellowSwitch2) == HIGH && lastYellowSwitch2 == LOW)
{
lastYellowSwitch2 = HIGH;
puzzleCount = 0;
Serial.println("You failed. Puzzle reset.");
}
}
else
{
}
}
``````

As it is now it's working as intended, but when I need to make another puzzle I cannot just copy paste it and just set up the right sequence in setup. How should I code it so I can use a string or something to put the right sequence in it? Each switch that gets triggered it would just check if it was the next in order according to that string? If a wrong button is pressed it would reset.

Is there also a better way code what each switch does so I don't have to code/copy paste the code for each button? A loop that checks after a switch is triggered what switch it was? This way it would be much easier to add buttons and other switches to the puzzle.

Try searching the forum for 'combination lock'.

dougp:
Try searching the forum for 'combination lock'.

Ty! I did try to find it before posting without any success. GOt it now.

Generally when you start adding numbers to your variable names you should consider using an array instead. For example:

``````int GreenSwitch[] = {50, 51, 52};
``````