Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Display.h> // Include the Display library
- #include <IRremote.h> // Include the IR Remote library
- #define CLK 10 // Define the CLK pin
- #define DIO 11 // Define the DIO pin
- DisplayHardware display(CLK, DIO); //Create a new display variable
- const int BTN_R = 8, LED_R = 4; // Create the buttons and lights variables
- const int BTN_L = 9, LED_G = 5;
- const int LED_B = 6;
- const int buzzer = 3; // Create the buzzer variable
- int CODE[] = {1,2,3,4}; // Create a variable for the code
- int input[] = {0,0,0,0}; // Create a variable for the user input
- int newPass[] = {1,2,3,4}; // Create a variable for storing the new code
- bool lock_l = false, lock_r = false; // Create two variables for the button locks
- bool entering = false; // Create a variable to check if the user is in input mode
- bool editing = false; // Create a variable to check if the user is in change code mode
- int digit = 0; // Create a variable for storing the currently edited digit's index
- int numbersRec = 0; // Create a variable for counting how many digits are recieved from the remote
- IRreceive irrecv(2); // Create a new IR receiver variable
- decode_results results; // Create a variable for storing the recieved information
- /* Set all pin modes, display and IR reciever */
- void setup() {
- pinMode(LED_R, OUTPUT);
- pinMode(BTN_R, INPUT_PULLUP);
- pinMode(LED_G, OUTPUT);
- pinMode(BTN_L, INPUT_PULLUP);
- pinMode(LED_B, OUTPUT);
- display.show("0000");
- irrecv.enable();
- }
- /* Called when a new number is received from the remote */
- void edit_digit(int digit){
- display.showCharAt(numbersRec, 48 + digit); // Show the number on the display at the correct place
- newPass[numbersRec] = digit; // Change the digit in the new code
- numbersRec++; // Increase the number of received digits
- tone(buzzer, 2000); // Enable the buzzer
- delay(100);
- noTone(buzzer);
- }
- /* Main functionality */
- void loop(){
- if (irrecv.decode(&results)){ // If the board receives an IR signal
- irrecv.resume(); // Receive the next value
- if(results.value == 0xFF15EA && !entering){ // If the signal is the "Play" button and the user is not in input mode
- if(!editing){ // If we're not in edit mode
- digitalWrite(LED_B, HIGH); // Turn on the blue LED
- display.show(" "); // Clear the display
- editing = true; // Change to edit mode
- }else{ // If we're in edit mode
- digitalWrite(LED_B, LOW); // Turn off the blue LED
- for(int i=0; i<4; i++)CODE[i] = newPass[i]; // Set the code to the newly written one
- display.show("0000"); // Clear the display
- editing = false; // Turn off edit mode
- numbersRec = 0; // Reset the received numbers counter back to zero
- }
- }
- }
- if(editing){ // If we're in edit mode
- if(numbersRec < 4){ // And we haven't yet received all four numbers of the new code
- if(irrecv.decode(&results)){ // Start listening for an IR signal
- switch(results.value){ // If the signal corresponds to a number on the remote
- case 0xFF16E9: // 0
- edit_digit(0);
- break;
- case 0xFF0CF3: // 1
- edit_digit(1);
- break;
- case 0xFF18E7: // 2
- edit_digit(2);
- break;
- case 0xFF5EA1: // 3
- edit_digit(3);
- break;
- case 0xFF08F7: // 4
- edit_digit(4);
- break;
- case 0xFF1CE3: // 5
- edit_digit(5);
- break;
- case 0xFF5AA5: // 6
- edit_digit(6);
- break;
- case 0xFF42BD: // 7
- edit_digit(7);
- break;
- case 0xFF52AD: // 8
- edit_digit(8);
- break;
- case 0xFF4AB5: // 9
- edit_digit(9);
- break;
- }
- }
- }else{ // If we recieved four numbers loop back to the first one in case of changes
- numbersRec = 0;
- }
- }
- if(!digitalRead(BTN_L) && !editing){ // If the "Input" button is pressed while not changing the code
- if(!lock_l){ // And it's not locked
- lock_l = true; // Lock it, until released
- if(entering){ // If the user is entering digits
- if(++input[digit] == 10)input[digit] = 0; // Increment the digit at the correct index and reset it back to one if it reaches ten
- digitalWrite(LED_G, HIGH); // Blink the green light
- delay(200);
- digitalWrite(LED_G, LOW);
- display.showDigitAt(digit, input[digit]); // Update the changed digit on the display
- }else{ // If the user is not entering digits
- digitalWrite(LED_G, HIGH); // Light both LEDs for 1sec
- digitalWrite(LED_R, HIGH);
- delay(1000);
- digitalWrite(LED_G, LOW);
- digitalWrite(LED_R, LOW);
- entering = true; // Start entering digits from the next button press
- }
- if(digitalRead(BTN_L))lock_l = false; // If the button has been released, unlock the lock
- }
- }else{
- lock_l = false; // If the button is not pressed, make sure it's unlocked
- }
- if(!digitalRead(BTN_R) && !editing){ // If the "Confirm" button is pressed while not changing the code
- if(!lock_r){ // And it's not locked
- lock_r = true; // Lock it, until released
- if(entering){ // If the user is entering digits
- if(++digit == 4){ // Increment the edited digit's index until it reaches above the last one
- digit = 0; // When that happens, set the edited digit's index back to the first one
- bool correct = true; // A new variable for checking if the input is correct
- for(int i=0; i<4; i++){ // Compare each digit of the input to each digit of the code
- if(input[i] != CODE[i]){ // If there's discrepancy, stop checking
- correct = false;
- break;
- }
- }
- if(correct){ // If the input is the same as the code light up the green LED for 2s and play the buzzer
- digitalWrite(LED_G, HIGH);
- tone(buzzer, 1500);
- delay(200);
- tone(buzzer, 1800);
- delay(700);
- noTone(buzzer);
- delay(1100);
- digitalWrite(LED_G, LOW);
- }else{ // If the input differs from the code light up the red LED for 2s and play the buzzer
- digitalWrite(LED_R, HIGH);
- tone(buzzer, 900);
- delay(600);
- tone(buzzer, 400);
- delay(500);
- noTone(buzzer);
- delay(900);
- digitalWrite(LED_R, LOW);
- }
- entering = false; // Stop the user from entering
- for(int i=0; i<4; i++)input[i] = 0; // Reset the user input variable
- display.show("0000"); // Reset the display
- }else{ // If the incremented digit's index hasn't reached the last one
- digitalWrite(LED_R, HIGH); // Blink the red LED
- delay(200);
- digitalWrite(LED_R, LOW);
- }
- }
- if(digitalRead(BTN_R))lock_r = false; // If the button has been released, unlock the lock
- }
- }else{
- lock_r = false; // If the button is not pressed, make sure it's unlocked
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement