Advertisement
Guest User

Lab 8

a guest
Dec 11th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #include <LiquidCrystal.h>
  3. LiquidCrystal Lcd (11, 9, 5, 6, 7, 8);
  4. #include "ButtonDebounce.h" //includes Button Debounce
  5. #include "EncoderMonitor.h" //includes Encoder Monitor
  6. int EncoderTracking = encoderPosition + 2;
  7. unsigned long DisplayUpdateTimer; //displays lock onto LED
  8. #define LCD_INTERVAL 100
  9. #define LED_INTERVAL 500
  10. unsigned int LCDTimer = millis(); // sets up LCD Timer
  11. unsigned int LEDTimer = millis(); // sets up LED Timer
  12. #define NUMBER_DIGITS 4 // sets up number of digits in combination
  13. int SerialInPtr = 0; // sets up the starting digits, which display when code starts via DisplayUpdateTimer
  14. 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
  15. enum LockNextState {Locked, Unlocked,Code0, Code1, Code2, Code3}; //Declares the states of the lock
  16. LockNextState LState; // creates a variable to call each state
  17.  
  18. void DisplayLcd (void) // Displays the 4 digits onto the LCD
  19. {
  20. Lcd.clear();
  21. Lcd.home();
  22. Lcd.print( (int) Code[0] ); // 1st digit combo
  23. Lcd.print( (int) Code[1] ); // 2nd digit combo
  24. Lcd.print( (int) Code[2] ); // 3rd digit combo
  25. Lcd.print( (int) Code[3] ); // 4th digit combo
  26. }
  27.  
  28.  
  29.  
  30. void setup ()
  31. {
  32. ButtonInitialize (); // sets up the button press
  33. Lcd.begin (16, 2);
  34. Lcd.clear(); //clears the LCD
  35. Lcd.home(); //starting position for LCD
  36. DisplayUpdateTimer = millis();
  37. DisplayLcd(); // Displays the above
  38. Serial.begin(9600);
  39. 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
  40. {
  41. EEPROM.get(a, Combination[a]); //once a is equivalent to the combination for that digit, EEPROM retrieves that number and stores it.
  42. if (Combination[a] < 0 || Combination[a] > 9)
  43. {
  44. Combination[a] = a + 1;
  45.  
  46. }
  47. Serial.print( (int) Combination[a] ); // prints the integer selected
  48. }
  49. Serial.println (" ");
  50. }
  51.  
  52. /*void ClearCode()
  53. {
  54. for (int k = 0; k < NUMBER_DIGITS; k++ )
  55. Code[k]= 0 ;
  56. }//end clear code
  57. */
  58. int ComboVsCode()
  59. {
  60. for ( int k = 0; k < NUMBER_DIGITS ; k++)
  61. {
  62. if (Code[k] != Combination[k] )
  63. return 0;
  64. //return 1;
  65. }
  66. return 1;
  67. }
  68.  
  69. void LockNextState(int Button )
  70. {
  71. switch ( LState )
  72. {
  73. case Unlocked:
  74. if ( Button == 3 )
  75. LState = Locked;
  76. break;
  77. case Locked:
  78. if ( Button == 2)
  79. {
  80. LState = Code0;
  81. }
  82. else if (ComboVsCode() )
  83. {
  84. LState = Unlocked;
  85.  
  86. }
  87. break;
  88. case Code0: //change code 0
  89. if ( Button == 2 ) // short press
  90. LState = Code1;
  91. else if (Button == 3) // long press
  92. LState = Locked;
  93. break;
  94. case Code1: //change code 1
  95. if ( Button == 2 ) // short press
  96. LState = Code2;
  97. else if (Button == 3) // long press
  98. LState = Locked;
  99. break;
  100. case Code2: //change code 2
  101. if ( Button == 2 ) // short press
  102. LState = Code3;
  103. else if (Button == 3) // long press
  104. LState = Locked;
  105. break;
  106. case Code3: //change code 3
  107. if ( Button == 2 ) // short press
  108. LState = Locked;
  109. else if (Button == 3) // long press
  110. LState = Locked;
  111. break;
  112. } //ends switch
  113. } // end LockNextState
  114.  
  115. void AdjustCode( int UpDown )
  116. {
  117. int k = -1; //do nothing default
  118. switch ( LState )
  119. {
  120. case Unlocked:
  121. break;
  122. case Locked:
  123. break;
  124. case Code0:
  125. k=0;
  126. break;
  127. case Code1:
  128. k=1;
  129. break;
  130. case Code2:
  131. k=2;
  132. break;
  133. case Code3:
  134. k=3;
  135. break;
  136. }
  137. if ( k >= 0 && k <=9)
  138. {
  139. Code[k] += UpDown;
  140. if (Code[k] > 9 )
  141. Code[k] = 0;
  142. if (Code[k] < 0 )
  143. Code[k] = 9;
  144. }
  145. }
  146.  
  147. void loop(){
  148. {
  149. LockNextState(ButtonNextState( digitalRead( 4 ) ) );
  150. }
  151. if (EncoderTracking - encoderPosition >= 4) //
  152. {
  153. AdjustCode(-1); //decrement
  154. EncoderTracking -= 4;
  155. }
  156. else if (EncoderTracking - encoderPosition <= -4)
  157. {
  158. AdjustCode( +1 ); // increment
  159. EncoderTracking += 4;
  160. }
  161. //turn on led if unlocked
  162. if (millis() - LEDTimer >= LED_INTERVAL )
  163. {
  164. if ( LState = Unlocked )
  165. PORTB == 0x20; //turn off led
  166. else //otherwise
  167. PORTB != 0x20; //toggle led
  168. } // end of led flash
  169. //timer to update display
  170.  
  171. if (millis() - LEDTimer >= LCD_INTERVAL)
  172.  
  173. if (millis() - LCDTimer >= 1000) { //if 1 second has passed
  174. DisplayLcd(); //displays lcd
  175. LCDTimer += 1000; //increment timer
  176. }
  177.  
  178. if ( Serial.available() ) //check for input from serial
  179. {
  180. char in = Serial.read(); //read in char
  181. if (in > '0' && in <= '9' ) //if it is a digit greater than or equal to 0 and less than or equal to 9
  182. {
  183. SerialInput[SerialInPtr++] = in - '0'; //insert number into serial input
  184. if ( SerialInPtr == NUMBER_DIGITS ) //check if 4 digits have come in
  185. {
  186. for (int k = 0; k < NUMBER_DIGITS; k++ ) // recover combination
  187. {
  188. Serial.print (SerialInput[k] ); // If four digits have come in
  189. Combination[k] = SerialInput[k]; // Copy SerialInput to Combination
  190. EEPROM.put( k, Combination[k] ); //write to EEPROM
  191.  
  192. }
  193. }
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement