// Amanda Grutza
// Keypad Magnetic Reveal
#include <VarSpeedServo.h>
// Name we give servo
VarSpeedServo magnet_servo;
// servo on pin 2
int servo_pin = 2;
// up position
int up = 0;
// down position
int down = 55;
// Keypad
#include <Keypad.h>
#include <Password.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {9, 10, 11, 12};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// set passcode
Password passcode = Password( "6273" );
// Miscellaneous Stuff
// led on pin 13
int led = 13;
// magnet relay on pin 4
int relay = 4;
int red = A5;
int green = A4;
void setup() {
// active serial communication
Serial.begin(9600);
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
pinMode(green, OUTPUT);
digitalWrite(green, LOW);
// set servo pin 5 (from above)
magnet_servo.attach(servo_pin);
// set servo position to start
// position, speed 0 to 255, true = now or false = wait for next
magnet_servo.write(up, 150, true);
// tell arduino this pin goes out
pinMode(led, OUTPUT);
// set led off
// LOW = off HIGH = on
digitalWrite(led, LOW);
// ditto buddy
pinMode(relay, OUTPUT);
// set magnet off
digitalWrite(relay, LOW);
// Keypad Listener
keypad.addEventListener(keypadEvent);
// Change This!!!
Serial.println("Enter the code now! ..or else!");
}
void loop() {
keypad.getKey();
delay(5);
}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.println(eKey);
button();
switch (eKey) {
case '#': check_passcode(); break;
case '*': passcode.reset(); break;
default: passcode.append(eKey);
}
}
}
void check_passcode() {
if (passcode.evaluate()) {
reveal();
} else {
digitalWrite(red, HIGH);
delay(1000);
digitalWrite(red, LOW);
}
}
void reveal() {
// send servo down
magnet_servo.write(down, 70, true);
// wait for it to get down, ya'll
delay(2000);
// turn on magnet
digitalWrite(relay, HIGH);
// give it a second to power on
delay(500);
// raise servo
magnet_servo.write(up, 70, true);
// turn on led
digitalWrite(led, HIGH);
// wait 6 seconds
delay(6000);
// turn off magnet
digitalWrite(relay, LOW);
//lower servo
magnet_servo.write(down, 70, true);
delay(1000);
// turn off led
digitalWrite(led, LOW);
// raise servo to middle
magnet_servo.write(up, 70, true);
delay(1000);
passcode.reset();
}
void button() {
digitalWrite(green, HIGH);
delay(250);
digitalWrite(green, LOW);
}
Comments
Please log in or sign up to comment.