How can I use a reed switch and Arduino to display a a code on an LCD

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
Ok, this is going to be a long one but hopefully I can get it all. Let me start off with the fact that I have never worked with Arduino or LCD paired with Arduino for that matter. I'm designing an escape room for my local museum to open in October for the week of Halloween. For one of the puzzels, I would like to have a globe with an LCD impeded into it and have reed switched around the globe in certain spots. One reed switch will be located in the place that the infection broke out (its a zombie-themed room). I want to design it so that when they put the pin with a magnet at the end of it over the right switch it displays a code for a lock on the LCD. I was also thinking it would be cool if when they put the pin over the wrong spot and trigger a switch it displays WRONG on the LCD and a 1 minute timer starts and they can't try again for that amount of time time to help prevent the escapers from just running the magnet around the globe until they find it. If anyone can help me with this project it would be greatly appreciated.
 

djsfantasi

Joined Apr 11, 2010
9,156
So what help do you need?

It’s a very doable project.

First, can you program? Any language? The Arduino language is a variant of C with some C++ features. Note that your application is not going to require heavy programming, but you or someone you know, had to be at least familiar.

Do you have an Arduino? The Arduino Uno will be sufficient for your needs, unless you’re planning on lots and lots of reed relays. How many are you planning on having?

Since you’re unfamiliar with Arduinos, download the free IDE (integrated development environment) and connect
the Arduino to it.

Start without the LCD and use some of the sample programs to get used to the IDE and loading a program (“sketch” in the Arduino world). Blink is the first sketch and teaches you how to use the IDE and upload a sketch to the Arduino. Your application is going to have to use the digital input/output pins, so look for the digitalRead or digital IO sample programs.

Do you have the LCD? Look for one that’s compatible with the Arduino. You can get bare displays, but then need to add circuits that the Arduino can talk to and you need to write specialized software.

Or you can go to Sparkfun, Adafruit or Arduino web sites to get your LCD. Some are even multicolored! When you buy from these sites, they will provide pre-written software to drive the LCD. And, they’ll also give you detailed hookup instructions as well as a software library. Takes a bit of playing, but it’s not impossible.

Since you are programming the Arduino, implementing a penalty for incorrect answers is pretty easy.

Let us know what you have and any additional questions.
 

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
So what help do you need?

It’s a very doable project.

First, can you program? Any language? The Arduino language is a variant of C with some C++ features. Note that your application is not going to require heavy programming, but you or someone you know, had to be at least familiar.

Do you have an Arduino? The Arduino Uno will be sufficient for your needs, unless you’re planning on lots and lots of reed relays. How many are you planning on having?

Since you’re unfamiliar with Arduinos, download the free IDE (integrated development environment) and connect
the Arduino to it.

Start without the LCD and use some of the sample programs to get used to the IDE and loading a program (“sketch” in the Arduino world). Blink is the first sketch and teaches you how to use the IDE and upload a sketch to the Arduino. Your application is going to have to use the digital input/output pins, so look for the digitalRead or digital IO sample programs.

Do you have the LCD? Look for one that’s compatible with the Arduino. You can get bare displays, but then need to add circuits that the Arduino can talk to and you need to write specialized software.

Or you can go to Sparkfun, Adafruit or Arduino web sites to get your LCD. Some are even multicolored! When you buy from these sites, they will provide pre-written software to drive the LCD. And, they’ll also give you detailed hookup instructions as well as a software library. Takes a bit of playing, but it’s not impossible.

Since you are programming the Arduino, implementing a penalty for incorrect answers is pretty easy.

Let us know what you have and any additional questions.
Thanks for such a quick reply. I don't have any coding background other than simple html i tried to teach myself. I have a sparkfun RedBoard and the sparkfun lcd to go with it so they should be perfectly compatible. I only plan to have about 6-8 switches with only 1 being the correct switch.
 

djsfantasi

Joined Apr 11, 2010
9,156
You’re going to have to learn some additional coding skills. Since the Redboard is Arduino compatible and is programmed with the Arduino IDE, I may be able to help. Sparkfun has one of the best sets of tutorials out there. Since you have a Sparkfun LCD, I’m sure that very detailed instructions on how to use it are on their site.

As far as wiring, you need power for the Redboard (D’oh).

Wire all of your reed switches to ground on one side and to an GPIO pin on the other. For coding simplicity, I’d pick six consecutive pins on the microprocessor.

Connect the LCD per the Sparkfun tutorial. You may have to move your input pins as the LCD may conflict.

Upload and run the LCD sample sketch (Arduino-speak for program). Make sure the display is working. Modify the sample sketch (Arduino-speak for program) to display your text.

There are two phases in an Arduino sketch. “setup()” is used to initialize the sketch and hardware. You’ll see when using the Sparkfun LCD libraries that you need to call some functions to ready the LCD. You’ll also gave to define your input pins here. The command is
“pinMode(myPin,INPUT_PULLUP);​
“INPUT_PULLUP” sets the default value to “1” when nothing is connected. The reed switch will ground the input, thus setting the value to “0”. Without this feature, you’d have to add extra resistors for each switch.

I’m assuming from your description that only one reed relay will be activated at s time. Your sketch will have to read all six pins. I’d check that only one is activated, and if not, execute the Fail Logic. Otherwise, someone could sneak in their own magnets and scan the globe.

If there is only one and it’s incorrect, execute the Fail Logic. The fail logic will display “WRONG”, and do nothing for one minute (the command to do this is “delay(1000)”. The delay command takes an argument in ms and does nothing for that amount of time)

If the activated reed switch is correct, then your sketch will display the lock code. You can use another “delay” here to limit the time the code is shown.

This satisfies your basic requirements. In this version 0, you’d have to hardcode the correct answer in the sketch. If you can get this far, let’s open a discussion of how to change the code periodically without having to upload a new sketch.

Let me know what you think? As far as creating a sketch for the Redboard, I can provide extensive help once I’m at my laptop.
 

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
You’re going to have to learn some additional coding skills. Since the Redboard is Arduino compatible and is programmed with the Arduino IDE, I may be able to help. Sparkfun has one of the best sets of tutorials out there. Since you have a Sparkfun LCD, I’m sure that very detailed instructions on how to use it are on their site.

As far as wiring, you need power for the Redboard (D’oh).

Wire all of your reed switches to ground on one side and to an GPIO pin on the other. For coding simplicity, I’d pick six consecutive pins on the microprocessor.

Connect the LCD per the Sparkfun tutorial. You may have to move your input pins as the LCD may conflict.

Upload and run the LCD sample sketch (Arduino-speak for program). Make sure the display is working. Modify the sample sketch (Arduino-speak for program) to display your text.

There are two phases in an Arduino sketch. “setup()” is used to initialize the sketch and hardware. You’ll see when using the Sparkfun LCD libraries that you need to call some functions to ready the LCD. You’ll also gave to define your input pins here. The command is
“pinMode(myPin,INPUT_PULLUP);​
“INPUT_PULLUP” sets the default value to “1” when nothing is connected. The reed switch will ground the input, thus setting the value to “0”. Without this feature, you’d have to add extra resistors for each switch.

I’m assuming from your description that only one reed relay will be activated at s time. Your sketch will have to read all six pins. I’d check that only one is activated, and if not, execute the Fail Logic. Otherwise, someone could sneak in their own magnets and scan the globe.

If there is only one and it’s incorrect, execute the Fail Logic. The fail logic will display “WRONG”, and do nothing for one minute (the command to do this is “delay(1000)”. The delay command takes an argument in ms and does nothing for that amount of time)

If the activated reed switch is correct, then your sketch will display the lock code. You can use another “delay” here to limit the time the code is shown.

This satisfies your basic requirements. In this version 0, you’d have to hardcode the correct answer in the sketch. If you can get this far, let’s open a discussion of how to change the code periodically without having to upload a new sketch.

Let me know what you think? As far as creating a sketch for the Redboard, I can provide extensive help once I’m at my laptop.
Awesome! all of this information is great. Been talking back in forth with another friend who is an electrical engineer and he was saying that ill basically only have to have 2 pins for all of the reed switches since all of the incorrect places will display the same thing. I would have one pin that will go to the correct reed switch location and the other pin will split off to all of the other reed switched basically allowing as many incorrect locations as I please with one pin. Would this work?
 

djsfantasi

Joined Apr 11, 2010
9,156
Awesome! all of this information is great. Been talking back in forth with another friend who is an electrical engineer and he was saying that ill basically only have to have 2 pins for all of the reed switches since all of the incorrect places will display the same thing. I would have one pin that will go to the correct reed switch location and the other pin will split off to all of the other reed switched basically allowing as many incorrect locations as I please with one pin. Would this work?
Yes, that would work! Your friend is correct. However, I went for one pin per switch for several reasons
  1. You can defeat anyone who triggers all the switches in an attempt to defeat the puzzle
  2. You can change the correct answer simply. That way, if a person returns, he won’t know the correct answer. Similarly, people who have been through your escape room can’t pass on the correct answer to people in the line.
 

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
Yes, that would work! Your friend is correct. However, I went for one pin per switch for several reasons
  1. You can defeat anyone who triggers all the switches in an attempt to defeat the puzzle
  2. You can change the correct answer simply. That way, if a person returns, he won’t know the correct answer. Similarly, people who have been through your escape room can’t pass on the correct answer to people in the line.
The only problem with that is we are on a tight time frame (15 minutes reset time) and we are going to have a set location for the outbreak of the infection which will include posters on the wall and a news recording.
 

djsfantasi

Joined Apr 11, 2010
9,156
The only problem with that is we are on a tight time frame (15 minutes reset time) and we are going to have a set location for the outbreak of the infection which will include posters on the wall and a news recording.
I understand. There is no time to defeat multiple magnets.

Changing the correct location is not a priority. I can see that. I ran a haunted house and we shut it down for ten minutes every hour in order to provide drinks and food to the actors inside. If I had a scene like yours, I’d attach the posters with magnets or Velcro. Thus, I’d have more than one set of supporting material. Takes but a couple of seconds to change posters. Depending on how your news recording is played, another couple of seconds to change an SD card or tape. A ten minute (or less!) break will give you plenty of time.

I’m assuming that there is a backstage entrance to each room. This saves you time as well.

There are many such considerations in your design, that allow you to customize your clients experience. From experience, I can state that this will enhance your revenue. Having multiple experiences encourage repeat visitors. Just three rooms with three options means your clients would have to go through more than 27 times to experience all you have to offer. Disney has used this concept in several attractions. Star Tours in Hollywood had hundreds of different experiences, all chosen at random. Guests will ride multiple times and still be surprised.
 

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
I understand. There is no time to defeat multiple magnets.

Changing the correct location is not a priority. I can see that. I ran a haunted house and we shut it down for ten minutes every hour in order to provide drinks and food to the actors inside. If I had a scene like yours, I’d attach the posters with magnets or Velcro. Thus, I’d have more than one set of supporting material. Takes but a couple of seconds to change posters. Depending on how your news recording is played, another couple of seconds to change an SD card or tape. A ten minute (or less!) break will give you plenty of time.

I’m assuming that there is a backstage entrance to each room. This saves you time as well.

There are many such considerations in your design, that allow you to customize your clients experience. From experience, I can state that this will enhance your revenue. Having multiple experiences encourage repeat visitors. Just three rooms with three options means your clients would have to go through more than 27 times to experience all you have to offer. Disney has used this concept in several attractions. Star Tours in Hollywood had hundreds of different experiences, all chosen at random. Guests will ride multiple times and still be surprised.
We only have one room and one entrance. This being a one week only fundraiser for our museum the possibility of multiple experiences with having to come after the fact. I could easily change the FM frequency that they have to tune into to find the location but changing everything might be too much. I could, however, change the location from day to day but that's as simple as going into the globe and moving where the correct reed switch is.
 

djsfantasi

Joined Apr 11, 2010
9,156
We only have one room and one entrance. This being a one week only fundraiser for our museum the possibility of multiple experiences with having to come after the fact. I could easily change the FM frequency that they have to tune into to find the location but changing everything might be too much. I could, however, change the location from day to day but that's as simple as going into the globe and moving where the correct reed switch is.
Sounds interesting! Good luck
 
You could do a look-up table too, where only you know the next code or location. If the room becomes empty like overnight, start with a brand new set.

In some languages you can have the computer pick a pseudo random number. It picks one from 0-1.

To get an integer from 1 to 10 it's Z=INT(10*RND)+1; RND is between 0 and 1

==

You can also use hall effect switches.
 

Thread Starter

Jacob_1080

Joined Jul 15, 2017
24
Or since this is an Arduino, to pick a random number from 1 to 10:
Z = random(1,11);​
RND isn’t a function in Arduino C.
I've managed to get a great prototype for this project with just 2 normal buttons and connecting everything up with a breadboard but it's working great. Perfect proof of concept. now just to get the reed switches and other materials and put it all together in a globe. Here is my first draft of my Arduino sketch. thanks a lot for the help :D If you see anything that might need to be changed in the future let me know and I'll add it.

#include <LiquidCrystal.h>

const int rs = 3, en = 2, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int CorrectPin = 8; // Correct Zone
const int WrongPin = 9; // Wrong Zone
int GameState = 0; // variable for reading the pushbutton status


void setup() {
pinMode(CorrectPin, INPUT);
pinMode(WrongPin, INPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.home();
lcd.print("Welcome");
lcd.setCursor(1,3);
lcd.print("Please Wait...");
delay (2000);
}

void loop() {
delay(1000);
lcd.clear();
lcd.home();
lcd.print("Locate Outbreak");
lcd.setCursor(1,3);
lcd.print(" Origin");
GameState = digitalRead(CorrectPin);// Check If Right Zone Has Been Pinned
if(GameState == HIGH)
{
// Correct
CorrectGuess();
}
GameState = digitalRead(WrongPin); // Check If Wrong Zone Has been pinned
if(GameState == HIGH)
{
// Wrong!
WrongGuess();
}
}
void CorrectGuess()
{
lcd.clear();
lcd.home();
lcd.print(" Found Zone");
lcd.setCursor(1,2);
lcd.print(" Code: 4765");
delay(10000);

}

void WrongGuess()
{
lcd.clear();
lcd.home();
lcd.print(" Wrong Zone ");
delay(2000);
Timer();
}

void Timer()
{
int Seconds = 60;
while(Seconds>0)
{
lcd.clear();
lcd.home();
lcd.print(" LOCKOUT!");
lcd.setCursor(1,2);
lcd.print(" ");
lcd.print(Seconds);
lcd.print(" Seconds");
delay(1000);
Seconds--;
}
}
 

djsfantasi

Joined Apr 11, 2010
9,156
Looks good, Jacob!

You didn’t provide any details as to how you wired up the push buttons. Hence, my only comment below...

Arduino’s pinMode() command has three different modes. OUTPUT is self-explanatory. But you also have INPUT and INPUT_PULLUP.

The latter is used when your push button connects to ground when pressed and you won’t need any pull down or pull up resistors. There is a pull up resistor inside the Ardunio. When the button isn’t pressed, it will return a HIGH. And vice versa.

The INPUT mode requires another resistor externally. Without the external resistor, when the push button isn’t pressed, the pin is left “floating” and when the sketch reads it, the value returned is unpredictable. It could be HIGH. It could be LOW. Kinda like Russian Roulette.

It’s easy to modify your code depending on the default state of your push buttons.

Do you have pull down resistors on your push buttons? This sketch needs them.
 
Top