Advertisement
belrey10

Vault Opened

Apr 20th, 2021
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1.  #define CLK 6
  2.  #define DT 7
  3.  #define SW 5
  4.  #define BLUE 11
  5.  #define GREEN 10
  6.  #define YELLOW 9
  7.  #define RED 8
  8.  int counter = 0;
  9.  int aState;
  10.  int aLastState;
  11.  int randNumber;
  12.  int scoreCounter = 4;
  13.  int scoreHolder = 0;
  14.  
  15.  void setup()
  16.  {
  17.    pinMode (CLK,INPUT);
  18.    pinMode (DT,INPUT);
  19.    pinMode (SW,INPUT);
  20.    pinMode (BLUE,OUTPUT);
  21.    pinMode (GREEN,OUTPUT);
  22.    pinMode (YELLOW,OUTPUT);
  23.    pinMode (RED,OUTPUT);
  24.    
  25.    Serial.begin (9600);
  26.    // Reads the initial state of the outputA
  27.    aLastState = digitalRead(CLK);
  28.  
  29.    randomSeed(analogRead(A0));
  30.    randNumber = random(100);
  31.    Serial.println(randNumber);
  32.    
  33.    digitalWrite(BLUE, LOW);
  34.    digitalWrite(GREEN, LOW);
  35.    digitalWrite(YELLOW, LOW);
  36.    digitalWrite(RED, HIGH);
  37.  }
  38.  
  39.  void loop()
  40.  {
  41.  
  42.   if (digitalRead(SW) == LOW)
  43.      {
  44.      GetScore(counter);
  45.      randNumber = random(100);
  46.      CalculateDiff(counter);
  47.  
  48.       if (scoreHolder == scoreCounter)
  49.       {
  50.         Serial.println("Congratulations! You unlocked the safe");
  51.         scoreHolder = 0;
  52.       }
  53.      }
  54.      
  55.    aState = digitalRead(CLK); // Reads the "current" state of the outputA
  56.    // If the previous and the current state of the outputA are different, that means a Pulse has occured
  57.    if (aState != aLastState)
  58.    {    
  59.      // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
  60.      if (digitalRead(DT) != aState)
  61.      {
  62.        counter ++;
  63.      }
  64.       else
  65.      {
  66.        counter --;
  67.      }
  68.  
  69.      if (counter > 100)
  70.      {
  71.       counter = 0;
  72.      }
  73.      else if (counter < 0)
  74.      {
  75.       counter = 100;
  76.      }
  77.  
  78.      CalculateDiff(counter);
  79.      Serial.println(counter);
  80.      Serial.println(randNumber);
  81.    }
  82.    
  83.    aLastState = aState; // Updates the previous state of the outputA with the current state
  84.  }
  85.  
  86.  
  87.  int CalculateDiff(int count)
  88.  {
  89.   int Diff = randNumber - count;
  90.   Serial.println(Diff);  
  91.      if (Diff < 10 || Diff > -10)
  92.      {
  93.       digitalWrite(YELLOW, HIGH);
  94.       digitalWrite(RED, LOW);
  95.       digitalWrite(GREEN, LOW);
  96.      }
  97.      
  98.      if(Diff > 10 || Diff < -10)
  99.      {
  100.       digitalWrite(YELLOW, LOW);
  101.       digitalWrite(GREEN, LOW);
  102.       digitalWrite(RED, HIGH);
  103.      }
  104.  
  105.      if (Diff == 0)
  106.      {
  107.       digitalWrite(GREEN, HIGH);
  108.       digitalWrite(RED, LOW);
  109.       digitalWrite(YELLOW, LOW);
  110.      }
  111.    return Diff;  
  112.  }
  113.  
  114.  void GetScore(int counterValue)
  115.  {
  116.    if (CalculateDiff(counterValue) == 0)
  117.       {
  118.         digitalWrite(BLUE, HIGH);
  119.         digitalWrite(GREEN, LOW);
  120.         delay (500);
  121.         digitalWrite(BLUE, LOW);
  122.         scoreHolder = scoreHolder + 1;
  123.         Serial.print("Score:");
  124.         Serial.println(scoreHolder);
  125.       }
  126.       else
  127.       {
  128.         digitalWrite(BLUE, HIGH);
  129.         digitalWrite(GREEN, HIGH);
  130.         digitalWrite(YELLOW, HIGH);
  131.         digitalWrite(RED, HIGH);
  132.         delay (500);
  133.         digitalWrite(BLUE, LOW);
  134.         digitalWrite(GREEN, LOW);
  135.         digitalWrite(YELLOW, LOW);
  136.         digitalWrite(RED, LOW);
  137.         scoreHolder = 0;
  138.         Serial.print("Score:");
  139.         Serial.println(scoreHolder);
  140.       }
  141.  }
  142.  
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement