Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. int Green_LED = 5;
  2. int Red_LED = 4;
  3. int ButtonInput = 9;
  4. int ButtonConfirm = 8;
  5. int Digit = 0;
  6. int InputBegins = 0;
  7. uint32_t counter1 = 0;
  8. uint32_t counter2 = 0;
  9. int arrayDigits[4] = {0, 0, 0, 0};
  10. int Pos = 0;
  11. int arrayCorrectCode[4] = {2, 4, 3, 1};
  12. // first I set up all the needed integers. Also I used something I know from C, which is Array.
  13. // if I have an int , the array can store multiple ints which makes life easier when I have to do the code.
  14.  
  15.  
  16.  
  17.  
  18. void setup() {
  19. pinMode(Green_LED, OUTPUT);
  20. pinMode(Red_LED, OUTPUT);
  21. pinMode(ButtonInput, INPUT_PULLUP);
  22. pinMode(ButtonConfirm, INPUT_PULLUP);
  23. Serial.begin(9600);
  24. // of course I always need to set up my leds and buttons and if something goes wrong I need the Serial Monitor.
  25.  
  26. }
  27.  
  28. void loop() {
  29. if (digitalRead(ButtonInput) == LOW && InputBegins == 0) {
  30. digitalWrite(Green_LED, HIGH);
  31. digitalWrite(Red_LED, HIGH);
  32.  
  33. delay(1000);
  34. digitalWrite(Green_LED, LOW);
  35. digitalWrite(Red_LED, LOW);
  36. InputBegins = 1;
  37. // first as is shown in the instructions I have to make the LEDs light up and then peace out.
  38. }
  39. if (Pos < 4) {
  40. Serial.println(Pos);
  41.  
  42. if (digitalRead(ButtonInput) == LOW) {
  43. if (counter1 == 0) {
  44. Digit = Digit + 1;
  45. digitalWrite(Green_LED, HIGH);
  46. }
  47. counter1 += 1;
  48.  
  49.  
  50. } else {
  51. digitalWrite(Green_LED, LOW);
  52. counter1 = 0;
  53. }
  54. if (digitalRead(ButtonConfirm) == LOW) {
  55. if (counter2 == 0) {
  56. digitalWrite(Red_LED, HIGH);
  57.  
  58. arrayDigits[Pos] = Digit;
  59. Digit = 0;
  60. Pos += 1;
  61.  
  62.  
  63. }
  64. counter2 += 1;
  65. } else {
  66. digitalWrite(Red_LED, LOW);
  67. counter2 = 0;
  68.  
  69.  
  70.  
  71. }
  72. } else {
  73. digitalWrite(Red_LED, LOW);
  74. if (arrayDigits[0] == arrayCorrectCode[0] && arrayDigits[1] == arrayCorrectCode[1] && arrayDigits[2] == arrayCorrectCode[2] && arrayDigits[3] == arrayCorrectCode[3]) {
  75. digitalWrite(Green_LED, HIGH);
  76. delay(2000);
  77. digitalWrite(Green_LED, LOW);
  78. InputBegins = 0;
  79. arrayDigits[0] = 0;
  80. arrayDigits[1] = 0;
  81. arrayDigits[2] = 0;
  82. arrayDigits[3] = 0;
  83. Pos = 0;
  84. }
  85. else {
  86. digitalWrite(Red_LED, HIGH);
  87.  
  88. delay(2000);
  89. digitalWrite(Red_LED, LOW);
  90. InputBegins = 0;
  91. arrayDigits[0] = 0;
  92. arrayDigits[1] = 0;
  93. arrayDigits[2] = 0;
  94. arrayDigits[3] = 0;
  95. Pos = 0;
  96. // here is where I have used the array so that I can set up my code and then I can compare if the code matches or not.
  97. // if according to the array I have set up the code matches the LED at the end will be Green as in entry granted.
  98. // if it is wrong the LED will be red.
  99. }
  100. }
  101. delay(50);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement