SHARE
TWEET

01Assignment

a guest Sep 18th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* LIBRALIES */
  2. #include "Display.h"                                                                                                                                                                          
  3.  
  4. /* DIGITS */
  5. const int RedLed = 4;
  6. const int GreenLed = 5;
  7. const int ConfirmButton = 8;
  8. const int InputButton = 9;
  9.  
  10. /* VARIABLES */
  11. int input_button_counter = 0;
  12. int confirm_button_counter = 1; // digit_pos
  13. int d1=0; //first digit
  14. int d2=0; //second digit
  15. int d3=0; //third digit
  16. int d4=0; //fourth digit
  17. bool input_flag = true;
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.  
  22.   pinMode(GreenLed, OUTPUT);
  23.   pinMode(RedLed, OUTPUT);
  24.   pinMode(ConfirmButton, INPUT_PULLUP);
  25.   pinMode(InputButton, INPUT_PULLUP);
  26. }
  27.  
  28. int getValue(int num){
  29.  
  30.     if(num >= 4){
  31.       num = 1;
  32.     } else {
  33.       num++;    
  34.    }
  35.    return num;
  36. }
  37.  
  38. bool isCorrect(){
  39.     if(d1 == 2 && d2 == 1 && d3 == 3 && d4 == 4){
  40.          
  41.         return true;
  42.     }
  43.              
  44.    return false;
  45. }
  46.  
  47. void loop() {
  48.  
  49.   // INPUT BUTTON
  50.  
  51.   if(digitalRead(InputButton) == LOW ){
  52.     if(input_flag == true){
  53.                  
  54.          for(int i = 0; i < 4; i++){
  55.           digitalWrite (GreenLed, HIGH);
  56.           digitalWrite (RedLed, HIGH);
  57.           delay(250);
  58.           digitalWrite (GreenLed, LOW);
  59.           digitalWrite (RedLed, LOW);
  60.           delay(250);
  61.         }
  62.  
  63.         input_flag = false;
  64.     }else{
  65.  
  66.        digitalWrite (GreenLed, HIGH);
  67.        delay(250);
  68.        digitalWrite (GreenLed, LOW);
  69.    
  70.     }
  71.  
  72.     input_button_counter = getValue(input_button_counter);
  73.   }
  74.  
  75.   // CONFIRM BUTTON
  76.  
  77.   if (digitalRead(ConfirmButton) == LOW && input_flag == false)
  78.   {
  79.     bool result = false;
  80.    
  81.     switch(confirm_button_counter){
  82.       case 1: d1 = input_button_counter;
  83.         break;
  84.       case 2: d2 = input_button_counter;
  85.         break;
  86.       case 3: d3 = input_button_counter;
  87.         break;
  88.       case 4: d4 = input_button_counter;
  89.              result = isCorrect();
  90.              input_flag = true;
  91.        break;
  92.     }
  93.    
  94.     Serial.print("[");
  95.     Serial.print(confirm_button_counter);
  96.     Serial.print("] ");
  97.     Serial.print("Confirm button: ");
  98.     Serial.println(input_button_counter);
  99.    
  100.     if(result == false && input_flag == true){
  101.      
  102.       Serial.println("Password is wrong!");
  103.       digitalWrite (RedLed, HIGH);
  104.       delay(2000);
  105.       digitalWrite (RedLed, LOW);
  106.      
  107.     }else if(result == false){
  108.      
  109.       digitalWrite (RedLed, HIGH);
  110.       delay(250);
  111.       digitalWrite (RedLed, LOW);
  112.        
  113.     }else if(result == true){
  114.       Serial.println("Password is correct!");
  115.         for(int i = 0; i < 8; i++){
  116.           digitalWrite (GreenLed, HIGH);
  117.           delay(250);
  118.           digitalWrite (GreenLed, LOW);
  119.         }
  120.    }
  121.  
  122.  
  123.     if(input_flag == true){
  124.       input_button_counter = 0;
  125.     }
  126.    
  127.       confirm_button_counter = getValue(confirm_button_counter);
  128.    
  129.   }
  130.  
  131.  Display.show(input_button_counter);
  132.  delay(50);
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top