ChicagoMadePunk

CatchTheFly

Oct 19th, 2020
1,347
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define BUTTON_1 2
  2. #define BUTTON_2 3
  3. #define BUTTON_3 4
  4. #define BUTTON_4 5
  5. #define LED_1 8
  6. #define LED_2 9
  7. #define LED_3 10
  8. #define LED_4 7
  9. #define RED_LED 11
  10. #define POT A0
  11.  
  12. int state_1;
  13. int state_2;
  14. int state_3;
  15. int state_4;
  16. int fadeAmount;
  17. int currentIntensity;
  18. int gameOver;
  19. int start;
  20. int count;
  21.  
  22. //Function for game initialization
  23. void initialize();
  24. //Function used to fade the red led
  25. void ledFading();
  26. //
  27. void buttonState();
  28.  
  29. void setup() {
  30.  
  31.   Serial.begin(9600);
  32.   //Input setup
  33.   pinMode(BUTTON_1, INPUT);
  34.   pinMode(BUTTON_2, INPUT);
  35.   pinMode(BUTTON_3, INPUT);
  36.   pinMode(BUTTON_4, INPUT);
  37.   pinMode(POT, INPUT);
  38.  
  39.   //Output setup
  40.   pinMode(LED_1, OUTPUT);
  41.   pinMode(LED_2, OUTPUT);
  42.   pinMode(LED_3, OUTPUT);
  43.   pinMode(LED_4, OUTPUT);
  44.   pinMode(RED_LED, OUTPUT);
  45.  
  46.   Serial.println("TEST");
  47.   //Game initialization
  48.   initialize();
  49. }
  50.  
  51. void loop() {
  52.   buttonState();
  53.   count++;
  54.   /*if(count == 50000) {
  55.     gameOver = 1;
  56.   }*/
  57.   if(start == 1) {
  58.     if(state_1 == LOW) {
  59.       ledFading();
  60.     } else if(state_1 == HIGH) {
  61.       digitalWrite(RED_LED, LOW);
  62.       start = 0;
  63.     }
  64.   } else if(start == 0) {
  65.     if(gameOver == 1) {
  66.       Serial.println("Game over");
  67.       initialize();
  68.     }
  69.   }
  70. }
  71.  
  72. void initialize() {
  73.   gameOver = 0;
  74.   start = 1;
  75.   currentIntensity = 0;
  76.   fadeAmount = 5;
  77.   Serial.println("Welcome to the Track the LED fly game. Press key T1 to start");
  78. }
  79.  
  80. void ledFading() {
  81.   analogWrite(RED_LED, currentIntensity);
  82.   currentIntensity += fadeAmount;
  83.   if(currentIntensity == 0 || currentIntensity == 255) {
  84.     fadeAmount = -fadeAmount;
  85.   }
  86.   delay(20);
  87. }
  88.  
  89. void buttonState() {
  90.   state_1 = digitalRead(BUTTON_1);
  91.   state_2 = digitalRead(BUTTON_2);
  92.   state_3 = digitalRead(BUTTON_3);
  93.   state_4 = digitalRead(BUTTON_4);
  94. }
RAW Paste Data