Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2014 05:57
Show Gist options
  • Select an option

  • Save anonymous/c4876df631f38d1c9d1f to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/c4876df631f38d1c9d1f to your computer and use it in GitHub Desktop.
#include <Wire.h> // Included in Arduino Libraries
#include <EEPROM.h> // Included in Arduino Libraries
#include <LiquidCrystal_I2C.h> // Can be found at !!!
#include <LiquidCrystal.h> // Can be found at !!!
#include <CustomStepper.h> // Can be found at http://playground.arduino.cc/Main/CustomStepper#Installation
#include <Keypad.h> // Can be found at !!!
#define num4 29 // First Lock Code on example
#define num5 15 // Second Lock Code on example
#define num6 9 // Third Lock Code on example
#define sppr 4075.7728395 // Steps Per Perfect Rotation
#define appn 9 // Angle Per Perfect Number
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Code for mini LCD
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{ '1', '4', '7', '*' },
{ '2', '5', '8', '0' },
{ '3', '6', '9', '#' },
{ 'A', 'B', 'C', 'D' }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins used by Keypad. High to low, 1st set
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 13, 12, 11, 10 }; // Pins used by Keypad. High to low, 2nd set
// Create the Keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Creates the Keypad
CustomStepper stepper(2, 3, 4, 5, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CCW);
int num = 0; // Resets code
String myCode; // Makes string for code
boolean codeEntered = false; // Has the code been entered yet?
String myCombo; // Makes string for readable code
String codeA; // NOT READY: Remembers one lock code
String codeB; // NOT READY: Remembers one lock code
String codeC; // NOT READY: Remembers one lock code
void setup()
{
lcd.begin(16,2); // Amount of spaces on LCD
lcd.backlight(); // Turns on backlight
lcd.setCursor(0, 0); // Sets cursor to First column, first row
lcd.print("Last Code Used:");
lcd.setCursor(0, 1); // Sets cursor to First column, second row
for(int i=0;i<6;i++){ // Prints six digit code in readable form
int value = EEPROM.read(i); // Reads from data
value -= 48; // Ascii values to alphabet values
lcd.print(value);
myCombo+=value;
if(i==1 || i==3){
lcd.print('-');
myCombo+='-';
}
}
lcd.blink(); // start blinking cursor
delay(3000); // delay 3 seconds
lcd.clear(); // clear display, set cursor position to zero
lcd.setBacklight(HIGH); // Backlight on
lcd.setCursor(0,0); //Sets cursor to col 0 of line 1
lcd.print("Enter Lock Code:");
lcd.setCursor(0,1); // Sets cursor to col 0 of line 2
stepper.setRPM(12); // Set motor speed
stepper.setSPR(4075.7728395); // Set steps per rotation
Serial.begin(9600); // Starts readout for Keypad to print to
}
boolean reset1 = false;
boolean reset2 = false;
boolean turn1 = false;
boolean turn2 = false;
boolean turn3 = true;
boolean done1 = false;
void wipeLines() { // Wipes all lines
for (int y = 0; y < 2; y++) {
for (int x = 0; x < 20; x++) {
lcd.setCursor (x,y);
lcd.print(" ");
delay(10);
}
}
lcd.setCursor(0,0);
}
void wipeCode(){ // Wipes only bottom line
for (int x=0;x<20;x++){
lcd.setCursor(x,1);
lcd.print(" ");
delay(10);
}
lcd.setCursor(0,1);
}
void setCode() // Loooong code to change code into three sections, then open lock
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
String a1 = "";
String a2 = "";
String a3 = "";
for (int i=0;i<2;i++){
a1 += myCode[i];
}
num1 = a1.toInt();
Serial.print(num);
for (int i=2; i<4;i++){
a2 += myCode[i];
}
num2 = a2.toInt();
for (int i=4; i<6;i++){
a3 += myCode[i];
}
num3 = a3.toInt();
wipeCode();
lcd.print("Cut Code");
delay(3000);
wipeCode();
while(done1 == false)
{
if(stepper.isDone() && reset1 == false && turn3 == true)
{
stepper.run();
stepper.rotate(2);
reset1 = true;
turn3 = false;
wipeCode();
lcd.print("Reset1");
delay(5000);
stepper.run();
}
if(stepper.isDone() && turn1 == false && reset1 == true)
{
stepper.rotateDegrees(360-(appn*num1));
turn1 = true;
reset1 = false;
lcd.print("2");
} // Theorized
if(stepper.isDone() && reset2 == false && turn1 == true)
{
stepper.setDirection(CW); // Up in Numbers
stepper.rotate(1);
reset2 = true;
turn1 = false;
}
if(stepper.isDone() && turn2 == false && reset2 == true)
{
stepper.rotateDegrees(360-(appn*num2)+(appn*num1));
turn2 = true;
reset2 = false;
} // Theorized
if(stepper.isDone() && turn3 == false && turn2 == true)
{
stepper.setDirection(CCW);
stepper.rotateDegrees(360-(appn*num3)+(appn*num2));
turn3 = true;
turn2 = false;
done1 = true;
wipeCode();
lcd.print("WORKED");
delay(3000);
} // Theorized
stepper.run();
}
done1 = false;
}
void loop()
{
char waitForKey(); // Waits for a Keypad click
char key = kpd.getKey(); // Gets key clicked
if(key=='*' || codeEntered==true) // Wipes code in case of * pressed or code entered
{
wipeCode();
num=0;
codeEntered = false;
myCode="";
}
else if(key=='#'){ // Shows code if # is pressed
wipeCode();
lcd.print(myCombo);
}
else if(key!=NO_KEY) // If any other key is pressed
{
lcd.print(key); // Shows key on LCD
num++; // Adds 1 to digit, waits for 6
myCode += key; // Appends key to code
if(key=='A' || key=='B' || key=='C' || key=='D'){ // If any of these keys are pressed
wipeLines(); // Wipes scren
switch(key){ // Chooses key that was clicked
case 'A':
codeA=myCode; // Saves code to codeA
lcd.print("Saved to A");
break;
case 'B':
codeB=myCode; // Saves code to codeB
lcd.print("Saved to B");
break;
case 'C':
codeC=myCode; // Saves code to codeC
lcd.print("Saved to C");
break;
default:
break;
}
lcd.setCursor(0,1);
lcd.print(myCombo); // Shows combination
delay(1500);
wipeLines();
lcd.setCursor(0,0);
lcd.print("Enter Lock Code:");
num=0;
myCode=0;
wipeCode();
}
else if(key-48>=4 && (num==1 || num==3 || num==5)){ // If number put in is 40 or greater (Lock is 0-39)
wipeCode();
lcd.print("Can't be > 40"); // Alerts user the combination doesn't exist
delay(2000);
num=0; // Resets code
myCode=""; // Resets code
wipeCode();
}
else if(num==6){ // If full number was inputted (6 digits entered to code)
wipeCode();
codeEntered = true;
lcd.print("Code Entered.");
delay(1500);
wipeCode();
lcd.print("Open lock?"); // Asks user to open lock or not
key=kpd.getKey();
while(key==NO_KEY){
key=kpd.getKey();
} // Waits for user to answer
if(key=='D'){ // If key D was pressed
wipeCode();
setCode(); // Code for motor
lcd.print("STUCK");
}
delay(1500);
wipeCode();
myCombo="";
for(int i=0;i<6;i++){ // Writes to memory code in case of shutdown
myCombo+=myCode[i];
EEPROM.write(i,myCode[i]);
if(i==1 || i==3){
myCombo+='-';
}
}
lcd.setCursor(0,1);
lcd.print(myCombo);
delay(2000);
num=0;
}
else if(num%2==0){ // Adds '-' to separate digits
lcd.print("-");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment