Hi All,
It took a little while to get this combo lock up and running. I have an arduino hooked up to a "sprite" media player: Sprite Seamless Looping HD Triggerable Video Player
You enter the correct combo and it triggers one video on the player, incorrect triggers the other video.
// constants
// set pin numbers:
const int buttonPin1 = 7; // the number of the pushbutton pins
const int buttonPin2 = 8;
const int buttonPin3 = 9;
const int ledPin = 12; // the number of the LED pin
String input1 = "1"; // digits for each button
String input2 = "2";
String input3 = "3";
byte video1[1] = {0x01}; // hex value for first video
byte video2[1] = {0x02}; // hex value for second video
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int playing = 0; // video playing or not
int presscount = 0; //variable for button presses
String Combination = "123"; // change combo here
String Attempt; // store user input
void setup() {
// initialize the LED pin as an output to check for button press
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pins as input:
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
Serial.begin(9600); // start serial
delay(500); // setup
}
void loop() {
// read the state of the pushbuttons:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// check for pushbutton presses
if (buttonState1 == HIGH && playing == 0) {
digitalWrite(ledPin, HIGH); // turn led on
presscount++; // add a button press to the counter
Attempt = (Attempt + input1); // add this button input to the users combo
delay(500); // debounce the pushbutton to stop multiple inputs
digitalWrite(ledPin, LOW); // turn off led
}
if (buttonState2 == HIGH && playing == 0) {
digitalWrite(ledPin, HIGH);
presscount++;
Attempt = (Attempt + input2);
delay(500);
digitalWrite(ledPin, LOW);
}
if (buttonState3 == HIGH && playing == 0) {
digitalWrite(ledPin, HIGH);
presscount++;
Attempt = (Attempt + input3);
delay(500);
digitalWrite(ledPin, LOW);
}
if (Serial.read() == 0xEE){
playing = 0;
}
if (presscount == 3 && playing == 0) { // check combo after 3 button presses
if (Attempt == Combination){
playing = 1; // stop checking
Serial.write(video1, sizeof(video1)); // play video 001.xxx on sprite
} else{
playing = 1;
Serial.write(video2, sizeof(video2)); // play video 002.xxx on sprite
}
presscount = 0; // reset the count
Attempt = ""; // clear the users code
}
// **** video 000.xxx should constantly loop when not being triggered by arduino ****
// sprite set to play mode- video control, control mode- serial, baud 9600
}