Escape Room Game - What am I doing wrong?

Using Arduino Programming Questions

I am working on an escape room game, the objective is to turn the lights on using push 6 pads {1, 1, 0, 1, 0, 1} 1 = on 0 = off and once those LEDs are on then the Magnetic Lock opens, currently the magnetic is always open for some reason.

I cant figure this out is someone could look at it with fresh eyes maybe? Maybe i missed a comma? I attached some pictures to show everything.

Any help would be amazing I think im just too new or too close to this to realize what im doing wrong.

// DEFINES
// Provides debugging information over serial connection
#define DEBUG

// CONSTANTS
// The number of switches.
const byte numSwitches = 6;
// The pins to which switches are connected
const byte switchPins[numSwitches] = {2, 3, 4, 5, 6, 7};
// These pins are used to light up LEDs to show the star lit by the switch.
const byte ledPins[numSwitches] = {13, 12, 11, 10, 9, 8};
// The desired solution state
bool solution[numSwitches] = {1, 1, 0, 1, 0, 1};
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;

// GLOBALS
// An array to record the last known state of every switch
bool lastSwitchState[numSwitches];

// Setup function runs once when first starting (or resetting) the board
void setup() {

// Initialise the input pins that have switches attached
for(int i=0; i< numSwitches; i++){
pinMode(switchPins*, INPUT_PULLUP);*

  • }*

  • // Initialise the LED pins that show progress through the sequence*

  • for(int i=0; i< numSwitches; i++){*
    _ pinMode(ledPins*, OUTPUT);_
    _ digitalWrite(ledPins, HIGH);
    }
    // Set the lock pin as output and secure the lock*
    * pinMode(lockPin, OUTPUT);
    digitalWrite(lockPin, HIGH);
    #ifdef DEBUG*
    * // Open communications on serial port*
    * Serial.begin(9600);
    Serial.println(F("Serial communication started"));
    #endif*
    }
    // The main program loop runs continuously
    void loop() {
    for(int i=0; i< numSwitches; i++){
    if (digitalRead(switchPins*) == LOW)
    {
    digitalWrite(ledPins, HIGH);
    }
    if (digitalRead(switchPins) == HIGH)
    {
    digitalWrite(ledPins, LOW);
    }
    // Check whether the puzzle has been solved*

    {
    if (switchPins == solution*){*
    * Serial.print ("element "); Serial.print (i); Serial.println (" matches");
    }
    else {
    Serial.print ("element "); Serial.print (i); Serial.println (" does not match");
    }
    }
    {
    int switchValue = digitalRead(switchPins);
    if(switchValue != lastSwitchState){
    Serial.print("Switch ");
    Serial.println(switchPins);
    Serial.print(" changed to ");
    Serial.println(switchValue);
    lastSwitchState = (bool)switchValue;
    }
    }
    if (lastSwitchState[numSwitches] = solution[numSwitches])
    // Release the lock*

    * digitalWrite(lockPin, LOW);
    }
    }_






    *

 for(int i=0; i< numSwitches; i++){

The i in that for statement turns on italics in the IDE. causes the forum software to turn on italics.

if (digitalRead(switchPins) == LOW)
    {
      digitalWrite(ledPins, HIGH);
    }

So everything after is in italics and your array indexes disappear. Read the how to use the forum stickies to see how to post code so that the IDE forum software does not screw it up. It is hard to evaluate code that is missing pieces.

if (switchPins == solution){

You cannot compare arrays like that. That actually compares the addresses of the arrays, not the contents and not what you want to do. Look up memcmp().

// DEFINES
// Provides debugging information over serial connection
#define DEBUG

// CONSTANTS
// The number of switches. 
const byte numSwitches = 6;
// The pins to which switches are connected
const byte switchPins[numSwitches] = {2, 3, 4, 5, 6, 7};
// These pins are used to light up LEDs to show the star lit by the switch.
const byte ledPins[numSwitches] = {13, 12, 11, 10, 9, 8};
// The desired solution state
bool solution[numSwitches] = {1, 1, 0, 1, 0, 1};
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;

// GLOBALS
// An array to record the last known state of every switch
bool lastSwitchState[numSwitches];

// Setup function runs once when first starting (or resetting) the board
void setup() {

  // Initialise the input pins that have switches attached
  for(int i=0; i< numSwitches; i++){
    pinMode(switchPins, INPUT_PULLUP);
  }
  
  // Initialise the LED pins that show progress through the sequence 
  for(int i=0; i< numSwitches; i++){
    pinMode(ledPins, OUTPUT);
    digitalWrite(ledPins, HIGH);
  }

  // Set the lock pin as output and secure the lock
  pinMode(lockPin, OUTPUT);
  digitalWrite(lockPin, HIGH);

  #ifdef DEBUG
    // Open communications on serial port
    Serial.begin(9600);
    Serial.println(F("Serial communication started"));
  #endif
}

// The main program loop runs continuously
void loop() {

for(int i=0; i< numSwitches; i++){
    if (digitalRead(switchPins) == LOW)
    {
      digitalWrite(ledPins, HIGH);
    }
    if (digitalRead(switchPins) == HIGH)
    {
      digitalWrite(ledPins, LOW);
    }


  // Check whether the puzzle has been solved
{
if (switchPins == solution){
    Serial.print ("element ");  Serial.print (i); Serial.println (" matches");
    }
else {
    Serial.print ("element ");  Serial.print (i); Serial.println (" does not match");
    }
}

  {
    int switchValue = digitalRead(switchPins);
    if(switchValue != lastSwitchState){
      Serial.print("Switch ");
      Serial.println(switchPins);
      Serial.print(" changed to ");
      Serial.println(switchValue);
      lastSwitchState = (bool)switchValue;
    }
  }
  if (lastSwitchState[numSwitches] = solution[numSwitches])
 // Release the lock
  digitalWrite(lockPin, LOW);
}
}

Is that really the your code. It will not compile as posted.

pinMode(switchPins, INPUT_PULLUP);

Is missing an indexer for the array, should be:

pinMode(switchPins[i], INPUT_PULLUP);
pinMode(ledPins, OUTPUT);

Same here. All the indexers in your for loops appear to be missing.

 if (digitalRead(switchPins) == LOW)

Yup, indexer missing.

Did you copy the code from your previous post or copy from your IDE?

Serial.println(switchPins);

This will throw an error, too. For an array that is not null terminated, you need to print the array element at a time (use a for loop).

You say that the lock never closes. How are you powering it? Is it wired directly to the arduino, or is pin A0 connected to something that drives the lock?

@groundFungus - So I need to add (see below) to all of those basically? and no its not just mine I purchased the code from some one and thought I could modify it as it didnt work

 [i]

Yes you need to add the indexers in the for loops where necessary. See the language reference for arrays for more information.

Here is the code with the indexers added where I think they are needed. I am not able to test the code as I do not have the hardware, but the code does compile.

// DEFINES
// Provides debugging information over serial connection
#define DEBUG

// CONSTANTS
// The number of switches.
const byte numSwitches = 6;
// The pins to which switches are connected
const byte switchPins[numSwitches] = {2, 3, 4, 5, 6, 7};
// These pins are used to light up LEDs to show the star lit by the switch.
const byte ledPins[numSwitches] = {13, 12, 11, 10, 9, 8};
// The desired solution state
bool solution[numSwitches] = {1, 1, 0, 1, 0, 1};
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;

// GLOBALS
// An array to record the last known state of every switch
bool lastSwitchState[numSwitches];

// Setup function runs once when first starting (or resetting) the board
void setup()
{

   // Initialise the input pins that have switches attached
   for (int i = 0; i < numSwitches; i++)
   {
      pinMode(switchPins[i], INPUT_PULLUP);
   }

   // Initialise the LED pins that show progress through the sequence
   for (int i = 0; i < numSwitches; i++)
   {
      pinMode(ledPins[i], OUTPUT);
      digitalWrite(ledPins[i], HIGH);
   }

   // Set the lock pin as output and secure the lock
   pinMode(lockPin, OUTPUT);
   digitalWrite(lockPin, HIGH);

#ifdef DEBUG
   // Open communications on serial port
   Serial.begin(9600);
   Serial.println(F("Serial communication started"));
#endif
}

// The main program loop runs continuously
void loop()
{
   for (int i = 0; i < numSwitches; i++)
   {
      if (digitalRead(switchPins[i]) == LOW)
      {
         digitalWrite(ledPins[i], HIGH);
      }
      if (digitalRead(switchPins[i]) == HIGH)
      {
         digitalWrite(ledPins[i], LOW);
      }


      // Check whether the puzzle has been solved
      {
         if (switchPins[i] == solution[i])  // this is legal because it compares elements of the arrays
         {
            Serial.print ("element ");  Serial.print (i); Serial.println (" matches");
         }
         else
         {
            Serial.print ("element ");  Serial.print (i); Serial.println (" does not match");
         }
      }

      {
         int switchValue = digitalRead(switchPins[i]);
         if (switchValue != lastSwitchState[i])
         {
            Serial.print("Switch ");
            Serial.println(switchPins[i]);
            Serial.print(" changed to ");
            Serial.println(switchValue);
            lastSwitchState[i] = (bool)switchValue;
         }
      }
      if (lastSwitchState[numSwitches] = solution[numSwitches])
         // Release the lock
         digitalWrite(lockPin, LOW);
   }
}