void setup()
{
//Initialisations
Serial.begin(115200); //Serial Tx
//Pin Modes
pinMode(5, OUTPUT); //Relay 1 // BOOKS magnets
pinMode(6, OUTPUT); //Relay 2 // DOOR
pinMode(A4, INPUT_PULLUP); //REED Switch 1
pinMode(A1, INPUT_PULLUP); //REED Switch 2
pinMode(A2, INPUT_PULLUP); //REED Switch 3
pinMode(A3, INPUT_PULLUP); //REED Switch 4
pinMode(A0, INPUT_PULLUP); //All Wrong REED Switchs are connected in series
}
void loop()
{
startover:
digitalWrite(6, LOW); //DOOR IS LOCKED
digitalWrite(5, HIGH); //BOOKS are reset to position
delay(1000);
digitalWrite(5, LOW); //BOOKS Magnet is Active
Serial.println("Start the game");
//-------------------------- The First book @ A0---------------------------
for(;;){
if(digitalRead(A1)==HIGH) // Sensing the Reed switch if LOW then it will test if its the right sequesnce
{
Serial.println("A1 is ON");
delay(2000); //Delay 2 sec
break;
}
else if (digitalRead(A0)==HIGH || digitalRead(A2)==HIGH || digitalRead(A3)==HIGH || digitalRead(A4)==HIGH) { //RESET ALL RELAYS if this book picked unsequenced
Serial.println("RESET from A0");
digitalWrite(5,HIGH);
delay(1000);
goto startover;
}
}
//-------------------------- The Second book @ A1---------------------------
for(;;){
if(digitalRead(A2)==HIGH) // Sensing the Reed switch if LOW then it will test if its the right sequesnce
{
Serial.println("A2 is ON");
delay(2000); //Delay 2 sec
break;
}
else if (digitalRead(A0)==HIGH || digitalRead(A3)==HIGH || digitalRead(A4)==HIGH){ //RESET ALL RELAYS if this book picked unsequenced
Serial.println("RESET from A1");
digitalWrite(5,HIGH);
delay(1000);
goto startover;
}
}
//-------------------------- The Third book @ A2---------------------------
for(;;){
if(digitalRead(A3)==HIGH) // Sensing the Reed switch if LOW then it will test if its the right sequesnce
{
Serial.println("A3 is ON");
delay(2000); //Delay 2 sec
break;
}
else if (digitalRead(A0)==HIGH || digitalRead(A4)==HIGH) { //RESET ALL RELAYS if this book picked unsequenced
Serial.println("RESET from A2");
digitalWrite(5,HIGH);
delay(1000);
goto startover;
}
}
//--------------------FINAL ANSWER -- The Fourth book @ A3---------------------------
for(;;){
if(digitalRead(A4)==HIGH) // Sensing the Reed switch if LOW then it will test if its the right sequesnce
{
delay(2000); //Delay 2 sec
Serial.println("A4 is ON");
Serial.println("DOOR OPEN");
digitalWrite(6, HIGH);
delay(2000);
break;
}
else if (digitalRead(A0)==HIGH) { //RESET ALL RELAYS if this book picked unsequenced
Serial.println("RESET from A3");
digitalWrite(5,HIGH);
delay(1000);
goto startover;
}
}
}