Buzzer Game Help

I have been creating the buzzer game for my daughters to play and I have most of the game working with a few quirks. You know the game; you have to traverse a pipe that is bent in many directions with a want without touching it. I used washers for the start and finish contacts and obviously the bar is connected to the fail pin. Everything works pretty good. the start pin starts the game, the rod always fails the game, and the finish washer works if you get there. The first issue is that if I fail, I can just keep touching the ring to the fail rod and it will eventually start the game without having to go back to the start. Second, I added code so that a lock would release a small door on the cabinet so I could put a prize or candy in there for my kids. The lock does not release.

What I have tried

  1. I replaced the relay with another that is known to work and get the same results.
  2. I have tried it with both the power to the arduino coming from the computer and coming from a 9v dc power to the vin and ground.
  3. I have replaced the HIGH and LOW in the code
  4. My relay is slightly different than the one in the fritzing image as it has a high/low jumper and I have tried both positions for that as well.
  5. I have changed the signal pin to the relay to both Analog, and a few other digital pins. (grasping at straws, I know)
  6. I triple checked to make sure no contacts are touching anywhere on the board.

I will attach images of the relay, lock, game, and the fritzing diagram.

I get no errors when checking in arduino 1.8.12. see code below:

// CONSTANTS
// The "Start Zone" which players must touch to start the game each time
const byte startPin = 8;
// Touching any part of the wire itself causes a failure
const byte failPin = 9;
// The "Win zone" at the end of the wire
const byte endPin = 10;
// A piezo buzzer chirps to signify success/failure
const byte buzzerPin = 6;
// The relay lock that will unlock with successful traverse
const byte relayPin = 7; //relay pin

// GLOBALS
// Keep track of the current states of the game
enum GameState {FAILED, IN_PROGRESS, SUCCESS};
GameState gameState = GameState::FAILED;

void setup() {
// Set the pins to the correct mode
pinMode(startPin, INPUT_PULLUP);
pinMode(failPin, INPUT_PULLUP);
pinMode(endPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);

// Begin a serial connection for debugging
Serial.begin(9600);
}

void loop() {
switch(gameState) {

case GameState::IN_PROGRESS:
if(!digitalRead(endPin)) {
gameState = GameState::SUCCESS;
Serial.println("Congratulations!");
tone(buzzerPin, 440, 100);
delay(120);
tone(buzzerPin, 450, 100);
delay(120);
tone(buzzerPin, 510, 100);
delay(120);
tone(buzzerPin, 587, 250);
//This releases the lock on successful completion
digitalWrite(relayPin, LOW);
delay(200);
digitalWrite(relayPin, HIGH);
}
else if(!digitalRead(failPin)) {
gameState = GameState::FAILED;
Serial.println("FAILED");
tone(buzzerPin, 440, 200);
delay(200);
tone(buzzerPin, 415, 200);
delay(200);
tone(buzzerPin, 392, 200);
delay(200);
tone(buzzerPin, 370, 400);
}
break;

case GameState::FAILED:
case GameState::SUCCESS:
if(!digitalRead(startPin)) {
gameState = GameState::IN_PROGRESS;
Serial.println("New Game Started");
tone(buzzerPin, 440, 100);
delay(200);
tone(buzzerPin, 554, 100);
delay(200);
tone(buzzerPin, 659, 200);
}
break;
}

}

Lock.png

// Begin a serial connection for debugging
  Serial.begin(9600);

And..?

It prints out just like it says to do. but when it restarts the game from the fail rod, it prints "start new game".

I put a print line that had it print the word test after the lock was suppose to release and the print line works great, just the lock doesn't release. That shows me it runs the code.

I can run the game a few times and post the results of the printout but without a play by play of what I was doing, it won't mean much.

Follow UP!

In case anyone else ran into a similar problem, I finally figured out what happened. I needed to declare the maglock as an output device and digitalWrite it HIGH in the globals. I added the following code and it is now working quite well.

pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);