Advertisement
fastoch13

Untitled

Apr 1st, 2023
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.00 KB | None | 0 0
  1. #include "FastLED.h"
  2. #define NUM_LEDS 45
  3. #define DATA_PIN A0
  4. #define SCORE_PIN 6
  5. #define SCORE_LEDS 6
  6. #define BRIGHTNESS 55
  7. CRGB leds[NUM_LEDS];
  8. CRGB sleds[NUM_LEDS];
  9.  
  10. bool reachedEnd = false;
  11. byte gameState = 0;
  12. //byte ledSpeed = 0;
  13. int period = 1000;
  14. unsigned long time_now = 0;
  15. byte Position = 0;
  16. byte level = 0;
  17.  
  18. const byte ledSpeed[6] = {50, 40, 30, 20, 14, 7};
  19.  
  20. //Debounce
  21. bool findRandom = false;
  22. byte spot = 0;
  23.  
  24. void setup() {
  25.   // put your setup code here, to run once:
  26.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  27.   FastLED.addLeds<WS2812B, SCORE_PIN, GRB>(sleds, SCORE_LEDS);
  28.   pinMode(A3, INPUT_PULLUP);
  29.   Serial.begin(9600);
  30.   Serial.println("Reset");
  31. }
  32.  
  33. void loop() {
  34.   // put your main code here, to run repeatedly:
  35.   FastLED.setBrightness(BRIGHTNESS );
  36.   if (gameState == 0) {
  37.     fill_rainbow(leds, NUM_LEDS, 0, 20); //2 = longer gradient strip
  38.     fill_rainbow(sleds, SCORE_LEDS, 0, 40); //2 = longer gradient strip
  39.  
  40.     if (digitalRead(A3) == LOW) {
  41.       Position = 0;
  42.       findRandom = true;
  43.       delay(500);
  44.       for (byte i = 0; i < NUM_LEDS; i++) {
  45.         leds[i].setRGB(0, 0, 0);
  46.         delay(40);
  47.         FastLED.show();
  48.        int thisPitch = map (i, 45, 0, 100, 1500);
  49.        tone(9, thisPitch,120);
  50.       }
  51.       for (byte i = 0; i < SCORE_LEDS; i++) {
  52.         sleds[i].setRGB(0, 0, 0);
  53.         delay(100);
  54.         FastLED.show();
  55.       }
  56.       gameState = 1;
  57.     }
  58.     FastLED.show();
  59.   }
  60.   if (gameState == 1) {
  61.     period = ledSpeed[0];
  62.     if (millis() > time_now + period) {
  63.       time_now = millis();
  64.       if (findRandom) {
  65.         spot = random(56) + 3;
  66.         findRandom = false;
  67.       }
  68.       leds[spot - 1].setRGB(255, 140, 0);
  69.       leds[spot].setRGB(0, 255, 0);
  70.       leds[spot + 1].setRGB(255, 110, 0);
  71.       sleds[0].setRGB(0, 255, 0);
  72.       PlayGame(spot - 1, spot + 1);
  73.     }
  74.     if (digitalRead(A3) == LOW) {
  75.       delay(300);
  76.       findRandom = false;
  77.       if (Position > spot - 1 && Position < spot + 3) {
  78.         level = gameState;
  79.         gameState = 98;
  80.       } else {
  81.         gameState = 99;
  82.       }
  83.     }
  84.   }
  85.   if (gameState == 2) {
  86. //    period = 320;
  87.     period = ledSpeed[1];
  88.     if (millis() > time_now + period) {
  89.       time_now = millis();
  90.       if (findRandom) {
  91.         spot = random(56) + 3;
  92.         findRandom = false;
  93.       }
  94.       leds[spot - 1].setRGB(255, 190, 0);
  95.       leds[spot].setRGB(0, 255, 0);
  96.       leds[spot + 1].setRGB(255, 190, 0);
  97.       sleds[1].setRGB(255, 255, 0);
  98.       PlayGame(spot - 1, spot + 1);
  99.     }
  100.     if (digitalRead(A3) == LOW) {
  101.       delay(300);
  102.       if (spot - 1 && Position < spot + 3) {
  103.         level = gameState;
  104.         gameState = 98;
  105.       } else {
  106.         gameState = 99;
  107.       }
  108.     }
  109.   }
  110.   if (gameState == 3) {
  111.     period = ledSpeed[2];
  112.     if (millis() > time_now + period) {
  113.       time_now = millis();
  114.       if (findRandom) {
  115.         spot = random(56) + 3;
  116.         findRandom = false;
  117.       }
  118.       leds[spot].setRGB(0, 255, 0);
  119.       sleds[2].setRGB(255, 50, 0);
  120.       PlayGame(spot, spot);
  121.     }
  122.     if (digitalRead(A3) == LOW) {
  123.       delay(300);
  124.       if (Position == spot+1) {
  125.         level = gameState;
  126.         gameState = 98;
  127.       } else {
  128.         gameState = 99;
  129.       }
  130.     }
  131.   }
  132.   if (gameState == 4) {
  133.     period = ledSpeed[3];
  134.     if (millis() > time_now + period) {
  135.       time_now = millis();
  136.       if (findRandom) {
  137.         spot = random(56) + 3;
  138.         findRandom = false;
  139.       }
  140.       leds[spot].setRGB(0, 255, 0);
  141.       sleds[3].setRGB(255, 0, 0);
  142.       PlayGame(spot, spot);
  143.     }
  144.     if (digitalRead(A3) == LOW) {
  145.       delay(300);
  146.       if (Position == spot+1) {
  147.         level = gameState;
  148.         gameState = 98;
  149.       } else {
  150.         gameState = 99;
  151.       }
  152.     }
  153.   }
  154.  
  155.   if (gameState == 5) {
  156.     period = ledSpeed[4];
  157.     if (millis() > time_now + period) {
  158.       time_now = millis();
  159.       if (findRandom) {
  160.         spot = random(56) + 3;
  161.         findRandom = false;
  162.       }
  163.       leds[spot].setRGB(0, 255, 0);
  164.       sleds[4].setRGB(0, 50, 255);
  165.       PlayGame(spot , spot);
  166.     }
  167.     if (digitalRead(A3) == LOW) {
  168.       delay(300);
  169.       if (Position == spot+1) {
  170.         level = gameState;
  171.         gameState = 98;
  172.       } else {
  173.         gameState = 99;
  174.       }
  175.     }
  176.   }
  177.  
  178.   if (gameState == 6) {
  179.     period = ledSpeed[5];
  180.     if (millis() > time_now + period) {
  181.       time_now = millis();
  182.       if (findRandom) {
  183.         spot = random(56) + 3;
  184.         findRandom = false;
  185.       }
  186.       leds[spot].setRGB(0, 255, 0);
  187.       sleds[5].setRGB(0, 150, 255);
  188.       PlayGame(spot , spot);
  189.     }
  190.     if (digitalRead(A3) == LOW) {
  191.       delay(300);
  192.       if (Position == spot+1) {
  193.         level = gameState;
  194.         gameState = 98;
  195.       } else {
  196.         gameState = 99;
  197.       }
  198.     }
  199.   }
  200.  
  201.   if (gameState == 98) {
  202.     winner();
  203.   }
  204.   if (gameState == 99) {
  205.     loser();
  206.   }
  207. }
  208. void PlayGame(byte bound1, byte bound2) {
  209.   leds[Position].setRGB(255, 0, 0);
  210.   if (Position < bound1 + 1 || Position > bound2 + 1) {
  211.     leds[Position - 1].setRGB(0, 0, 0);
  212.   }
  213.   FastLED.show();
  214.   Position++;
  215.   if (Position >= NUM_LEDS) {
  216.     leds[Position - 1].setRGB(0, 0, 0);
  217.     Position = 0;
  218.   }
  219. }
  220.  
  221. void winner() {
  222.   for (byte i = 0; i < 3; i++) {
  223.     for (byte j = 0; j < NUM_LEDS; j++) {
  224.       leds[j].setRGB(0, 255, 0);
  225.         tone(9, 1000, 250);
  226.     }
  227.     FastLED.show();
  228.     delay(500);
  229.     clearLEDS();
  230.     FastLED.show();
  231.     delay(500);
  232.  
  233.   }
  234.   findRandom = true;
  235.   Position = 0;
  236.  
  237.   gameState = level + 1;
  238.   if (gameState > 6) {
  239.     gameState = 0;
  240.   }
  241. }
  242. void loser() {
  243.   for (byte i = 0; i < 3; i++) {
  244.     for (byte j = 0; j < NUM_LEDS; j++) {
  245.       leds[j].setRGB(255, 0, 0);
  246.       tone(9, 200, 250);
  247.     }
  248.     FastLED.show();
  249.     delay(500);
  250.     clearLEDS();
  251.     FastLED.show();
  252.     delay(500);
  253.   }
  254.   gameState = 0;
  255. }
  256. void clearLEDS() {
  257.   for (byte i = 0; i < NUM_LEDS; i++) {
  258.     leds[i].setRGB(0, 0, 0);
  259.   }
  260. }
  261. void winAll(){
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement