Help with using multiple sensors for an escape room puzzle

Hello,

My wife and I are trying to design a Barbie-themed escape room for Halloween. We have a puzzle planned, where there are 4 blank Ken portraits, and you have to follow a logic puzzle to place the correct prop on each one. This will trigger an LCD to display the 4 digits necessary to get into a lock.

Because we already use magnets on the portraits, and magnets in the props (like pin the tail on the donkey) we can't use magnet sensors to trigger the correct sequence, so we're trying to use LDR sensors instead. That way, once all 4 props are placed in the correct spots, the LDR sensors will be covered up, and the LCD will display the code.

We're prototyping our idea now, and so far we've figured out how to get one LDR to light up one LED once the light is at the correct value.

The problem we're having is getting 3 more LDR's programmed into the code. Then getting the trigger to occur once the value of all 4 LDR's is at a certain number to trigger the LCD display.

Here is the code we have running for 1 LDR to 1 LED. It's working exactly fine, but we're stuck when trying to get our second LDR to work.

int ldrPin = A0;              // LDR pin
int ldrVal = 0;               // Value of LDR
int ledPin = 2;     // Build in LED pin

void setup() {
  Serial.begin(9600);         // Initialise the serial monitor
  pinMode(2, OUTPUT);
}

void loop() {
  
  ldrVal = analogRead(ldrPin);    // Read the analog value of the LDR
  Serial.println(ldrVal);         // Show the value in the serial monitor

  if (ldrVal < 700) {             // If the LDR value is lower than 700
    digitalWrite(ledPin, HIGH);   // Turn buildin LED on
  } else {
    digitalWrite(ledPin, LOW);    // Turn buildin LED off
  }

  delay(100);                     // Pause 100ms
  
}

What I'm imagining is a statement that says something akin to "if all ldrVal <700" digitalWrite LED on".

Hopefully, that makes sense. My wife and I are brand spanking new to this whole Arduino game. We're currently working our way through the Elegoo kit, but so much of this code stuff is still greek to me haha.

Thanks for reading this dissertation of a post! Any help is greatly appreciated!

-Brad

How many analog pins does your Arduino have? You need one per LDR.

Hello,
Just let me explain what I understood so you’ll know if what I propose is correct for you

You want to detect WHEN the 4 LDR’s have been triggered so that you can light up the LED right? Otherwise the LED is off

If it’s the case; then just think in human langage!
-> if the 4 LDR’s detect low luminosity
-> then you turn on the light
-> else the led is off

So you need, in your setup(), to declare more pins as input (3 more) on analogPin
Then you need to test their value in the loop() with AND operator such as:

if ( (s̶t̶a̶t̶e̶m̶e̶n̶t̶  condition 1) && (s̶t̶a̶t̶e̶m̶e̶n̶t̶  condition 2) )
{
  // LED ON
}
else
{
  // LED OFF
}

The AND operator is written && in Arduino
I let you give it a try by yourself and if you need then I can correct you future code! Or give you the answer but you won’t learn…

Wow. We usually aren't asked for help w/ Halloween stuff until around the 22nd or 23rd of October. :expressionless:

The noob approach here will work well. You can literally copy and paste the code for one LDR three times and edit each copy so it is using its own analog input pin and its own variable to store the reading.

Then as @Anthony_P points out, the four variables can be used as conditions (not statements, really) in one if statement, using the logical and operator &&.

String all four together as the condition that determines whether the code in the if body is executed or not.

If you make quick progress we can talk about other ways to do it, but this will work fine for a first sketch.

HTH

a7

3 Likes

Magnetic fields drop by distance cubed. The magnets you would use don't flood the room and TBH weak fridge magnets should be better than button supermagnets.

But light blocking is maybe a better way. But will the player block the light? If you use IR leds and IR sensors you could do what you want with visible light.

But if the leds and detectors are behind the prop, the IR can reflect off the back to detect.

You're right, I edited the "statement" to "condition"
Thanks

Do you have any code that represents your attempt to use a second LDR?

It's an Arduino Nano, so it has 7 analog slots and a bunch of digital ones.

Thanks @Anthony_P ! We got it running!

The && operator was super helpful.

I had a few other mistakes in there mostly because I didn't know what was what, but with enough context, I was able to get it working!

So far we've only got it with 2 light sensors, but as @alto777 pointed out, we'll be able to just copy it 2 more times and use the && operator to connect them.

Haha yeah, this is literally the 2nd thing we've ever used arduino for (other than Mark Rober explaining how to use a button to turn on and LED and how to twist a servo motor) so we are trying to start early! :smiley:

We do! We just got it running with the second one now, so I'm going to post it at the bottom here for everyone to see.

Hokay! Here's the update!

With everyone's help, I was able to figure out how to define each of the separate LDR's in the code, and use the && operator to only light up the LED if both features are true.

Here is the code that I ended up using so far.

int ldrPin1st = A0;
int ldrPin2nd = A6;
int ldr1Val = 0;
int ldr2Val = 6;
int ledPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode (2, OUTPUT);
}


void loop() {


  ldr1Val = analogRead(ldrPin1st);    // Read the analog value of the 1st LDR
  Serial.println(ldr1Val);         // Show the value in the serial monitor
  ldr2Val = analogRead(ldrPin2nd);    // Read the analog value of the 2nd LDR
  Serial.println(ldr2Val);         // Show the value in the serial monitor

  if ( (ldr1Val < 700) && (ldr2Val <500) ) {             // If the LDR value is lower than 700
    digitalWrite(ledPin, HIGH);   // Turn buildin LED on
  } else {
    digitalWrite(ledPin, LOW);    // Turn buildin LED off
  }

  delay(100);                     // Pause 100ms
  
}

Now, the plan is to modify this later to output to 4 LED strips around 4 picture frames.

So, if anyone is wondering how the puzzle will work:
• There will be 4 portraits on the wall each with a "hidden in plain sight" number in each portrait.
• There will be magnetic pieces of clothing and accessories that can be added to each portrait which will cover up the light sensors if they are placed in the right spot.
• Players will solve a logic puzzle to understand which piece of clothing goes on each portrait.
• Once ALL accessories are placed in the correct positions, it will trigger the border around each portrait to glow a certain color.
• In another part of the room there will be a 4-digit lock that matches the designated colors.
• If players take the hidden number and the color from their portrait to the lock, it will give them the correct combination to get into the box

So that's it! We'll have to modify things in the future to work with programmable RGB LED's but that part shouldn't be too hard.

We're so grateful for everyone's help. I'll come back in a few weeks with a short video to show everyone how we got things up and running.

Thanks again!

Brad

1 Like

Yeah, we got some good neodymiums that we can embed into the props, but we don't want to run into the problem of a player feeling the magnet and just getting it to stick to the board in the correct position by accident.

We thought about using magnet sensors and velcro to attach the props, but we figured it would be too easy to just look at the shape of the prop, and the shape of the velcro rather than solving the logic puzzle.

1 Like

Hello!
Glad to hear that I’ve been able to help you!
Be carfuls in your code

  1. In the setup you use ‘2’ and not the name of the variable
  2. You don’t initialize its value (to zero here, or LOW)
  3. You don’t declare your analog inputs as inputs in the setup (even if as far as I remember, for analog input it isn’t required)
  4. You don’t use the same value when comparing ldr1 and ldr2 (one compared to 700 and the other to 500)

But with pleasure I’ll watch your videos in a few days

Don’t forget to mention a post as a solution in case other members try something similar

Have fun!!

You on the way. You coukd just use 1, 2, 3 and so forth as suffixes, save a bit of typing. Usually when this technique is used, it shows up as single names with an appended digit.

int ldrPin1 = A0;
int ldrPin2 = A6;

No large deal.


Famous last words! We here, just continue to plan as far in advance of the dead line as possible. Murthy's Law and all. :skull_and_crossbones:


When we do see code that has been copy/paste/edited to increase the number of whatevers, someone will recommend that when learning time comes, array variables and for loops are your best new friends to make, along with getting to know how to use functions to make a single instance of a coded solution flexible enough to handle any whatever you throw at it.

You will like that, promise. Even if you don't make a career out of it. :expressionless:

a7

To me, code is creative writing in logic.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.