Advertisement
Guest User

Untitled

a guest
Jan 15th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. #include <DFRobotDFPlayerMini.h>
  5.  
  6. #define LED1 4
  7. #define LED2 5
  8. #define LED3 6
  9. #define LED4 7
  10. #define NUM_LIGHTS 60
  11. #define lightStripLen 2
  12.  
  13.  
  14. #define startPin A0
  15.  
  16. int dzwiek;
  17.  
  18.  
  19. #define outputPin 9
  20.  
  21. int state = 0;
  22.  
  23. //losowe kolory, moze ich byc wiecej, tylko trzeba to zmienic w kilku miejscach, jak cos to pisz
  24. int r1 [6] = {255, 255, 255, 255, 255, 255}; // czerwony
  25. int g1 [6] = {255, 255, 255, 255, 255, 255}; // niebieski
  26. int b1 [6] = {255, 255, 255, 255, 255, 255}; // zielony
  27.  
  28. int pozycje[4] = {0, 0, 0, 0};
  29. int kolory[4] = {0, 0, 0, 0};
  30.  
  31.  
  32. SoftwareSerial mySoftwareSerial(3, 2); // RX, TX
  33. DFRobotDFPlayerMini myDFPlayer;
  34.  
  35. Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUM_LIGHTS, LED1, NEO_GRB + NEO_KHZ800);
  36. Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUM_LIGHTS, LED2, NEO_GRB + NEO_KHZ800);
  37. Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(NUM_LIGHTS, LED3, NEO_GRB + NEO_KHZ800);
  38. Adafruit_NeoPixel strip4 = Adafruit_NeoPixel(NUM_LIGHTS, LED4, NEO_GRB + NEO_KHZ800);
  39.  
  40. Adafruit_NeoPixel list[4] = {strip1, strip2, strip3, strip4};
  41.  
  42. int num = 0;
  43. int32_t timeStart = 0;
  44. void setup()
  45. {
  46. mySoftwareSerial.begin(9600);
  47. Serial.begin(115200);
  48.  
  49. strip1.begin();
  50. strip1.show(); // Initialize all pixels to 'off'
  51. strip2.begin();
  52. strip2.show(); // Initialize all pixels to 'off'
  53. strip3.begin();
  54. strip3.show(); // Initialize all pixels to 'off'
  55. strip4.begin();
  56. strip4.show(); // Initialize all pixels to 'off'
  57.  
  58. if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
  59. Serial.println("podłączony dfplayer!");
  60. Serial.println(F("Unable to begin:"));
  61. Serial.println(F("1.Please recheck the connection!"));
  62. Serial.println(F("2.Please insert the SD card!"));
  63. while (true) {
  64. delay(0); // Code to compatible with ESP8266 watch dog.
  65. }
  66. }
  67. Serial.println(F("DFPlayer Mini online."));
  68.  
  69. myDFPlayer.volume(30); //Set volume value. From 0 to 30
  70.  
  71. strip1.begin();
  72. strip1.show(); // Initialize all pixels to 'off'
  73. strip2.begin();
  74. strip2.show(); // Initialize all pixels to 'off'
  75. strip3.begin();
  76. strip3.show(); // Initialize all pixels to 'off'
  77. strip4.begin();
  78. strip4.show(); // Initialize all pixels to 'off'
  79.  
  80.  
  81. dzwiek = 1;
  82.  
  83. pinMode(startPin, INPUT_PULLUP);
  84. pinMode(outputPin, OUTPUT);
  85. }
  86.  
  87. //funkcja mowiaca jakie opoznienie ma byc w zaleznosci od tego ktory cykl/ ktory raz ma byc wlaczone odczekiwanie
  88. int getDelay(int num)
  89. {
  90. if (num < 300)
  91. return 500 / (num + 1) + 40; // to dla pierwszych 300 cykli - hiperbola malejaca z czasem -> coraz mniejsze opoznienie z czasem/cyklam
  92. else
  93. return 10; // pod koniec stale opoznienie 10ms -> pociag ma stala predkosc wiec stale opoznienie miedzy cyklami
  94. }
  95.  
  96. void loop()
  97. {
  98. if (state == 1)
  99. {
  100. #define coile 180000
  101. if (millis() - timeStart > coile) //na 10 sekund
  102. {
  103. digitalWrite(outputPin, HIGH);
  104. delay(10000);
  105. timeStart = millis();
  106. digitalWrite(outputPin, LOW);
  107. num = 0;
  108.  
  109. }
  110. else
  111. {
  112. int randomColor = random(6);
  113. for (int i = 0; i < 4; i++)
  114. {
  115.  
  116. if (pozycje[i] == -1)
  117. {
  118. if (random(1000) < 5) // TUTAJ DECYDUJESZ JAKI PROCENT SZANSY JEST NA ZAPALENIE PASKA !!!!!!!!!!
  119. {
  120. for (int j = 0; j < 4; j++)
  121. {
  122. pozycje[j] = 0;
  123. kolory[j] = randomColor;
  124. }
  125. }
  126. }
  127. else
  128. {
  129. int len = 2; // tutaj decydujesz jak dlugi bedzie jeden przesuwajacy sie pasek (ilosc zapalonych na jednym stripie diod, ktore na raz moga byc zapalone)
  130. printNleds(&(list[i]), pozycje[i], len, kolory[i]);
  131. pozycje[i] += 1;
  132. if (pozycje[i] > NUM_LIGHTS)
  133. {
  134. pozycje[i] = -1;
  135. }
  136. }
  137. }
  138.  
  139. delay(getDelay(num));
  140. Serial.println(getDelay(num));
  141. num++;
  142. }
  143. }
  144.  
  145. if (digitalRead(startPin) == 0)
  146. {
  147. if (state == 0)
  148. timeStart = millis();
  149. state = 1;
  150. }
  151. else
  152. {
  153. state = 0;
  154. num = 0;
  155. }
  156.  
  157. if ((digitalRead(startPin) == 0) && (dzwiek == 1)) // dzwiek startu pociagu
  158. {
  159. myDFPlayer.play(1);
  160. dzwiek = 0;
  161. }
  162. }
  163.  
  164.  
  165. void printNleds(Adafruit_NeoPixel* x, int start, int amount, int n)
  166. {
  167. for (int i = 0; i < NUM_LIGHTS; i++)
  168. x->setPixelColor(i, x->Color(0, 0, 0));
  169.  
  170. for (int i = start; i < start + amount; i++)
  171. x->setPixelColor(i, x->Color(r1[n], g1[n], b1[n]));
  172.  
  173. x->show();
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement