Advertisement
Guest User

ButtonMaze

a guest
Feb 18th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. const int correct[4] = {3,1,4,2};
  2. #define LEN 4
  3.  
  4. void setup()
  5. {
  6.   pinMode(12, INPUT_PULLUP);
  7.   pinMode(11, INPUT_PULLUP);
  8.   pinMode(10, INPUT_PULLUP);
  9.   pinMode(9, INPUT_PULLUP);
  10.   pinMode(8, INPUT_PULLUP);
  11.   pinMode(7, OUTPUT);
  12.   pinMode(6, OUTPUT);
  13.   pinMode(5, OUTPUT);
  14.   pinMode(4, OUTPUT);
  15.   Serial.begin(9600);
  16. }
  17.  
  18. bool checkButton(int num){
  19.   if(!digitalRead(num)){
  20.     delay(30);
  21.     if(!digitalRead(num))
  22.       return true;
  23.   }
  24.   return false;
  25. }
  26.  
  27. int getPress(){
  28.   while(true){
  29.     if(checkButton(12))
  30.       return 1;
  31.     if(checkButton(11))
  32.       return 2;
  33.     if(checkButton(10))
  34.       return 3;
  35.     if(checkButton(9))
  36.       return 4;
  37.     if(checkButton(8))
  38.       return 5;
  39.   }
  40. }
  41.  
  42. void loop()
  43. {
  44.   for(int i = 4; i <= 7; i++)
  45.     digitalWrite(i, LOW);
  46.   digitalWrite(13, LOW);
  47.   int i = 0;
  48.   while(true){
  49.     int c = getPress();
  50.     if(c == 6)
  51.       return;
  52.     else{
  53.         if(correct[i] != c)
  54.           return;
  55.       else{
  56.         digitalWrite(7-i, HIGH);
  57.         i++;
  58.         delay(300);
  59.       }
  60.     }
  61.     if(i == LEN-1){
  62.       digitalWrite(13, HIGH);
  63.       delay(1000);
  64.       return;
  65.     }
  66.   }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement