I am looking to build a lock box for part of a student Escape Room exercise, the idea is to use a hinged box with either a solenoid or servo to latch it. As it will only be used for short durations power consumption is not an issue.
The box will have 8 single pole double throw toggle switches (SPDT) for the binary number and one momentary action push button to "test" the switch positions. I can work out most of the code for alarms, delays and release the latch etc. I what I don't know is how to read the switch positions for comparison with the target value.
I am struggling with what to search for, so I am hoping some kind person can give me a push in the right direction.
Ultimately I will want a setup code that allows the teacher to set the binary number before the class starts so they can use clues to solve the number.
Gather all the switch readings one-by-one into a byte variable. When the 'open' button is pressed compare that byte to the previously stored solution code, also a byte. If they match...
Here is a tedious way to read the switches and get them in a byte. The method is to read the first switch into the low bit of the result, then shift the bit up and add the next bit into the low bit into the result. Repeat until all switches are read.
// Ugly way to read a set of switchs
//
const byte bit0 = 2; // use pin 2 for low order bit
const byte bit1 = 3;
const byte bit2 = 4;
const byte bit3 = 5;
const byte bit4 = 6;
const byte bit5 = 7;
const byte bit6 = 8;
const byte bit7 = 9; // use pin 9 for high order bit
const byte read_bits = 10; // configure so that it is grounded when pressed
byte code; // code read from switches
void setup() {
// put your setup code here, to run once:
pinMode(bit0,INPUT_PULLUP);
pinMode(bit1,INPUT_PULLUP);
pinMode(bit2,INPUT_PULLUP);
pinMode(bit3,INPUT_PULLUP);
pinMode(bit4,INPUT_PULLUP);
pinMode(bit5,INPUT_PULLUP);
pinMode(bit6,INPUT_PULLUP);
pinMode(bit7,INPUT_PULLUP);
pinMode(read_bits,INPUT_PULLUP);
}
void loop() {
// some code
if (!digitalRead(read_bits)){ // is button pressed
// yes read switches
code = digitalRead(bit7); //read high order bit
code = (code << 1) + digitalRead(bit6); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit5); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit4); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit3); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit2); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit1); // move previous bit left an add in next bit
code = (code << 1) + digitalRead(bit0); // move previous bit left an add in low order bit
}
// more code here
}
SPST switches connected between input and ground, pinMode set to INPUT_PULLUP (or if that turns out to be unreliable, 4k7 pullups to another common pin).
Why do I mention pullups to another pin? Well, if as I suspect, you propose to operate this from batteries, you only pull that pin HIGH for long enough to read the switches - no more than every half second - and then set it LOW to critically minimise current draw. Similarly switching from INPUT_PULLUP to INPUT (or in fact, writing the input pin LOW) disables the internal pullup.
So you want a sort of keyswitch (or another secret code) to put it in "programming" mode to enter the code on the same switches. If you want it to stay over power down. you need to use the EEPROM.
That leads to the possibility of each switch being "set" more than once!
Thanks this is helpful, I won't be trying to control the order they are set, too complicated for me and difficult for students too.
The idea is for them to find clues to determine the correct number to unlock the box, incorrect numbers will probably sound a buzzer and/or flash a LED
Use the same switches to set the solution and to enter the guess. Internal button only accessible to the instructor triggers the storage of the solution; exterior button used by students to open.