ChicagoMadePunk

ArduinoFly

Oct 21st, 2020
518
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <MiniTimerOne.h>
  2.  
  3. #define BUTTON_1 2
  4. #define BUTTON_2 3
  5. #define BUTTON_3 4
  6. #define BUTTON_4 5
  7. #define LED_1 8
  8. #define LED_2 9
  9. #define LED_3 10
  10. #define LED_4 7
  11. #define RED_LED 11
  12. #define POT A0
  13.  
  14. #define L_1 127
  15. #define L_2 255
  16. #define L_3 383
  17. #define L_4 511
  18. #define L_5 639
  19. #define L_6 767
  20. #define L_7 895
  21. #define L_8 1023
  22. #define MULTIPLIER 8
  23. #define MIN 0
  24. #define MAX 3
  25. #define MICROS 1000000
  26.  
  27. int state_1;
  28. int state_2;
  29. int state_3;
  30. int state_4;
  31. int fadeAmount;
  32. int currentIntensity;
  33. int gameOver;
  34. int start;
  35. int count;
  36. float tMin;
  37. int leds[4];
  38. int currPos;
  39. int isStop;
  40.  
  41.  
  42. //Function for game initialization
  43. void initialize();
  44. //Function used to fade the red led
  45. void ledFading();
  46. //
  47. void buttonState();
  48.  
  49. float setDifficulty();
  50.  
  51. void generateRandomPos();
  52.  
  53. void setGameOver();
  54.  
  55. void setup() {
  56.  
  57.   Serial.begin(9600);
  58.   //Input setup
  59.   pinMode(BUTTON_1, INPUT);
  60.   pinMode(BUTTON_2, INPUT);
  61.   pinMode(BUTTON_3, INPUT);
  62.   pinMode(BUTTON_4, INPUT);
  63.   pinMode(POT, INPUT);
  64.  
  65.   //Output setup
  66.   pinMode(LED_1, OUTPUT);
  67.   pinMode(LED_2, OUTPUT);
  68.   pinMode(LED_3, OUTPUT);
  69.   pinMode(LED_4, OUTPUT);
  70.   pinMode(RED_LED, OUTPUT);
  71.  
  72.   leds[0] = LED_1;
  73.   leds[1] = LED_2;
  74.   leds[2] = LED_3;
  75.   leds[3] = LED_4;
  76.  
  77.   MiniTimer1.init();
  78.   MiniTimer1.attachInterrupt(setGameOver);
  79.  
  80.   //Game initialization
  81.   initialize();
  82. }
  83.  
  84. void loop() {
  85.   buttonState();
  86.   if(start == 1) {
  87.     if(state_1 == LOW) {
  88.       ledFading();
  89.     } else if(state_1 == HIGH) {
  90.       digitalWrite(RED_LED, LOW);
  91.       randomSeed(millis());
  92.       currPos = random(MIN,MAX + 1);
  93.       Serial.println(currPos);
  94.       start = 0;
  95.       tMin = setDifficulty();
  96.       MiniTimer1.setPeriod(tMin);
  97.     }
  98.      
  99.   } else if(start == 0) {
  100.     if(isStop == 0) {
  101.       MiniTimer1.setPeriod(tMin);
  102.       MiniTimer1.start();
  103.       digitalWrite(leds[currPos], HIGH);
  104.       isStop = 1;
  105.     } else if (isStop == 1) {
  106.     //generateRandomPos();
  107.       if(gameOver == 1) {
  108.         MiniTimer1.stop(); //da mettere nel controllo bottone
  109.         Serial.println("Game over");
  110.         digitalWrite(RED_LED, HIGH);
  111.         delay(2000);
  112.         digitalWrite(RED_LED, LOW);
  113.         initialize();
  114.        }
  115.     }
  116.   }
  117. }
  118.  
  119. void initialize() {
  120.   gameOver = 0;
  121.   start = 1;
  122.   currentIntensity = 0;
  123.   fadeAmount = 5;
  124.   isStop = 0;
  125.   Serial.println("Welcome to the Track the LED fly game. Press key T1 to start");
  126. }
  127.  
  128. void ledFading() {
  129.   analogWrite(RED_LED, currentIntensity);
  130.   currentIntensity += fadeAmount;
  131.   if(currentIntensity == 0 || currentIntensity == 255) {
  132.     fadeAmount = -fadeAmount;
  133.   }
  134.   delay(20);
  135. }
  136.  
  137. void buttonState() {
  138.   state_1 = digitalRead(BUTTON_1);
  139.   state_2 = digitalRead(BUTTON_2);
  140.   state_3 = digitalRead(BUTTON_3);
  141.   state_4 = digitalRead(BUTTON_4);
  142. }
  143.  
  144. float setDifficulty() {
  145.   float value = analogRead(POT);
  146.       Serial.println(value);
  147.      
  148.       if(value <= L_1) {
  149.         Serial.println("L 1");
  150.         return 60 * MICROS;
  151.       }
  152.       if(value > L_1 && value <= L_2) {
  153.         Serial.println("L 2");
  154.         return 55 * MICROS;
  155.       }
  156.       if(value > L_2 && value <= L_3) {
  157.         Serial.println("L 3");
  158.         return 50 * MICROS;
  159.       }
  160.       if(value > L_3 && value <= L_4) {
  161.         Serial.println("L 4");
  162.         return 45 * MICROS;
  163.       }
  164.       if(value > L_4 && value <= L_5) {
  165.         Serial.println("L 5");
  166.         return 40 * MICROS;
  167.       }
  168.       if(value > L_5 && value <= L_6) {
  169.         Serial.println("L 6");
  170.         return 35 * MICROS;
  171.       }
  172.       if(value > L_6 && value <= L_7) {
  173.         Serial.println("L 7");
  174.         return 30 * MICROS;
  175.       }
  176.       if(value > L_7 && value <= L_8) {
  177.         Serial.println("L 8");
  178.         return 25 * MICROS;
  179.       }
  180.       return 0;
  181. }
  182.  
  183. void generateRandomPos() {
  184.   randomSeed(millis());
  185.   int dir = random(0,2);
  186.   Serial.print("DIRECTION: ");
  187.   Serial.println(dir);
  188.   if(dir == 0) {
  189.     if(currPos == MIN) {
  190.       currPos = MAX;
  191.     } else {
  192.       currPos--;
  193.     }
  194.   } else if(dir == 1) {
  195.     if(currPos == MAX) {
  196.       currPos = MIN;
  197.     } else {
  198.       currPos++;
  199.     }
  200.   }
  201. }
  202.  
  203. void setGameOver() {
  204.   digitalWrite(leds[currPos], LOW);
  205.   gameOver = 1;
  206.   isStop = 0;
  207. }
  208.  
RAW Paste Data