Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include <LiquidCrystal.h>
- LiquidCrystal Lcd (11, 9, 5, 6, 7, 8);
- #include "ButtonDebounce.h" //includes Button Debounce
- #include "EncoderMonitor.h" //includes Encoder Monitor
- int EncoderTracking = encoderPosition + 2;
- unsigned long DisplayUpdateTimer; //displays lock onto LED
- #define LCD_INTERVAL 100
- #define LED_INTERVAL 500
- unsigned int LCDTimer = millis(); // sets up LCD Timer
- unsigned int LEDTimer = millis(); // sets up LED Timer
- #define NUMBER_DIGITS 4 // sets up number of digits in combination
- int SerialInPtr = 0; // sets up the starting digits, which display when code starts via DisplayUpdateTimer
- char Combination[NUMBER_DIGITS], SerialInput[NUMBER_DIGITS], Code[NUMBER_DIGITS]; // Combination is goal, Serial Input is manually edited, Code is used to compare to Cmbination
- enum LockNextState {Locked, Unlocked,Code0, Code1, Code2, Code3}; //Declares the states of the lock
- LockNextState LState; // creates a variable to call each state
- void DisplayLcd (void) // Displays the 4 digits onto the LCD
- {
- Lcd.clear();
- Lcd.home();
- Lcd.print( (int) Code[0] ); // 1st digit combo
- Lcd.print( (int) Code[1] ); // 2nd digit combo
- Lcd.print( (int) Code[2] ); // 3rd digit combo
- Lcd.print( (int) Code[3] ); // 4th digit combo
- }
- void setup ()
- {
- ButtonInitialize (); // sets up the button press
- Lcd.begin (16, 2);
- Lcd.clear(); //clears the LCD
- Lcd.home(); //starting position for LCD
- DisplayUpdateTimer = millis();
- DisplayLcd(); // Displays the above
- Serial.begin(9600);
- for ( int a = 0; a < NUMBER_DIGITS; a++) // a starts at 0, if it is lower than the number of digits, it goes up by 1
- {
- EEPROM.get(a, Combination[a]); //once a is equivalent to the combination for that digit, EEPROM retrieves that number and stores it.
- if (Combination[a] < 0 || Combination[a] > 9)
- {
- Combination[a] = a + 1;
- }
- Serial.print( (int) Combination[a] ); // prints the integer selected
- }
- Serial.println (" ");
- }
- /*void ClearCode()
- {
- for (int k = 0; k < NUMBER_DIGITS; k++ )
- Code[k]= 0 ;
- }//end clear code
- */
- int ComboVsCode()
- {
- for ( int k = 0; k < NUMBER_DIGITS ; k++)
- {
- if (Code[k] != Combination[k] )
- return 0;
- //return 1;
- }
- return 1;
- }
- void LockNextState(int Button )
- {
- switch ( LState )
- {
- case Unlocked:
- if ( Button == 3 )
- LState = Locked;
- break;
- case Locked:
- if ( Button == 2)
- {
- LState = Code0;
- }
- else if (ComboVsCode() )
- {
- LState = Unlocked;
- }
- break;
- case Code0: //change code 0
- if ( Button == 2 ) // short press
- LState = Code1;
- else if (Button == 3) // long press
- LState = Locked;
- break;
- case Code1: //change code 1
- if ( Button == 2 ) // short press
- LState = Code2;
- else if (Button == 3) // long press
- LState = Locked;
- break;
- case Code2: //change code 2
- if ( Button == 2 ) // short press
- LState = Code3;
- else if (Button == 3) // long press
- LState = Locked;
- break;
- case Code3: //change code 3
- if ( Button == 2 ) // short press
- LState = Locked;
- else if (Button == 3) // long press
- LState = Locked;
- break;
- } //ends switch
- } // end LockNextState
- void AdjustCode( int UpDown )
- {
- int k = -1; //do nothing default
- switch ( LState )
- {
- case Unlocked:
- break;
- case Locked:
- break;
- case Code0:
- k=0;
- break;
- case Code1:
- k=1;
- break;
- case Code2:
- k=2;
- break;
- case Code3:
- k=3;
- break;
- }
- if ( k >= 0 && k <=9)
- {
- Code[k] += UpDown;
- if (Code[k] > 9 )
- Code[k] = 0;
- if (Code[k] < 0 )
- Code[k] = 9;
- }
- }
- void loop(){
- {
- LockNextState(ButtonNextState( digitalRead( 4 ) ) );
- }
- if (EncoderTracking - encoderPosition >= 4) //
- {
- AdjustCode(-1); //decrement
- EncoderTracking -= 4;
- }
- else if (EncoderTracking - encoderPosition <= -4)
- {
- AdjustCode( +1 ); // increment
- EncoderTracking += 4;
- }
- //turn on led if unlocked
- if (millis() - LEDTimer >= LED_INTERVAL )
- {
- if ( LState = Unlocked )
- PORTB == 0x20; //turn off led
- else //otherwise
- PORTB != 0x20; //toggle led
- } // end of led flash
- //timer to update display
- if (millis() - LEDTimer >= LCD_INTERVAL)
- if (millis() - LCDTimer >= 1000) { //if 1 second has passed
- DisplayLcd(); //displays lcd
- LCDTimer += 1000; //increment timer
- }
- if ( Serial.available() ) //check for input from serial
- {
- char in = Serial.read(); //read in char
- if (in > '0' && in <= '9' ) //if it is a digit greater than or equal to 0 and less than or equal to 9
- {
- SerialInput[SerialInPtr++] = in - '0'; //insert number into serial input
- if ( SerialInPtr == NUMBER_DIGITS ) //check if 4 digits have come in
- {
- for (int k = 0; k < NUMBER_DIGITS; k++ ) // recover combination
- {
- Serial.print (SerialInput[k] ); // If four digits have come in
- Combination[k] = SerialInput[k]; // Copy SerialInput to Combination
- EEPROM.put( k, Combination[k] ); //write to EEPROM
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement