Advertisement
Guest User

Untitled

a guest
Jul 17th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3. #include <avr/power.h>
  4. #endif
  5.  
  6. #define DATA_PIN 19
  7. #define NUM_LEDS 300
  8. int NUM_RAINBOW_LEDS = 300;
  9. const int potentiometerPin = 15;
  10. const int potentiometerPinPlus1 = potentiometerPin + 1;
  11. const int potentiometerPinMinus1 = potentiometerPin - 1;
  12. const int patternSwitchPin = 4;
  13. const int whiteSwitchPin = 3;
  14. const int protocolSwitchPin = 2;
  15.  
  16. // Parameter 1 = number of pixels in strip
  17. // Parameter 2 = Arduino pin number (most are valid)
  18. // Parameter 3 = pixel type flags, add together as needed:
  19. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  20. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  21. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  22. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  23. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  24. Adafruit_NeoPixel *strip;
  25.  
  26. int patternSwitchState = 1;
  27. int whiteSwitchState = 1;
  28. int protocolSwitchState = 1;
  29.  
  30. void setup() {
  31.  
  32. pinMode(patternSwitchPin, INPUT_PULLUP);
  33. pinMode(potentiometerPin, INPUT_PULLUP);
  34. pinMode(potentiometerPinPlus1, OUTPUT);
  35. pinMode(potentiometerPinMinus1, OUTPUT);
  36. pinMode(whiteSwitchPin, INPUT_PULLUP);
  37. pinMode(5, OUTPUT); // debugging LED - remove later
  38.  
  39. digitalWrite(potentiometerPinPlus1, LOW); // leave this low to give a ground pin conveniently next to the potentiometer input pin
  40. digitalWrite(potentiometerPinMinus1, HIGH); // leave this high to give a 5 V pin conveniently next to the potentiometer input pin
  41.  
  42. if (digitalRead(whiteSwitchPin) == HIGH) {
  43. whiteSwitchState = 1;
  44. } else {
  45. whiteSwitchState = 0;
  46. }
  47.  
  48. strip = new Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, (whiteSwitchState ? NEO_GRB : NEO_RGBW) + NEO_KHZ800);
  49.  
  50. strip->begin();
  51. strip->show(); // Initialize all pixels to 'off'
  52. strip->setBrightness(2); // Set BRIGHTNESS to about 1/5 (max = 255)
  53. }
  54.  
  55. void loop() {
  56. NUM_RAINBOW_LEDS = map(analogRead(potentiometerPin), 0, 1023, 1, 60);
  57.  
  58. patternSwitchState = digitalRead(patternSwitchPin);
  59.  
  60. if (patternSwitchState == 1) { // for debugging LED - remove later
  61. digitalWrite(5, HIGH);
  62. }
  63. else {
  64. digitalWrite(5, LOW);
  65. }
  66.  
  67. if ((patternSwitchState == 0) && (whiteSwitchState == 0)) {
  68. solidColoursRGB();
  69. } else if ((patternSwitchState == 1) && (whiteSwitchState == 0)) {
  70. rainbowCycle(2);
  71. } else if ((patternSwitchState == 0) && (whiteSwitchState == 1)) {
  72. solidColoursRGBW();
  73. } else if ((patternSwitchState == 1) && (whiteSwitchState == 1)); {
  74. whiteOverRainbow(20, 75, (NUM_RAINBOW_LEDS / 8 + 1));
  75. }
  76. }
  77.  
  78. void solidColoursRGB() {
  79. // Clear the existing led values, then set all to green according to potentimeter reading.
  80. strip->clear();
  81. for (int led = 0; led < NUM_LEDS; led++) {
  82. strip->setPixelColor(led, 255, 0, 0);
  83. }
  84. strip->show();
  85. delay(250);
  86. strip->show();
  87. delay(250);
  88. strip->show();
  89. delay(250);
  90. strip->show();
  91. delay(250);
  92. strip->show();
  93. delay(250);
  94. strip->show();
  95. delay(250);
  96. strip->show();
  97. delay(250);
  98. strip->show();
  99. delay(250);
  100.  
  101. // Clear the existing led values, then set all to red according to potentimeter reading.
  102. strip->clear();
  103. for (int led = 0; led < NUM_LEDS; led++) {
  104. strip->setPixelColor(led, 0, 255, 0);
  105. }
  106. strip->show();
  107. delay(250);
  108. strip->show();
  109. delay(250);
  110. strip->show();
  111. delay(250);
  112. strip->show();
  113. delay(250);
  114. strip->show();
  115. delay(250);
  116. strip->show();
  117. delay(250);
  118. strip->show();
  119. delay(250);
  120. strip->show();
  121. delay(250);
  122.  
  123. // Clear the existing led values, then set all to blue according to potentimeter reading.
  124. strip->clear();
  125. for (int led = 0; led < NUM_LEDS; led++) {
  126. strip->setPixelColor(led, 0, 0, 255);
  127. }
  128. strip->show();
  129. delay(250);
  130. strip->show();
  131. delay(250);
  132. strip->show();
  133. delay(250);
  134. strip->show();
  135. delay(250);
  136. strip->show();
  137. delay(250);
  138. strip->show();
  139. delay(250);
  140. strip->show();
  141. delay(250);
  142. strip->show();
  143. delay(250);
  144. }
  145.  
  146.  
  147.  
  148. void solidColoursRGBW() {
  149. // Clear the existing led values, then set all to green according to potentimeter reading.
  150. strip->clear();
  151. for (int led = 0; led < NUM_LEDS; led++) {
  152. strip->setPixelColor(led, 255, 0, 0, 0);
  153. }
  154. strip->show();
  155. delay(250);
  156. strip->show();
  157. delay(250);
  158. strip->show();
  159. delay(250);
  160. strip->show();
  161. delay(250);
  162. strip->show();
  163. delay(250);
  164. strip->show();
  165. delay(250);
  166. strip->show();
  167. delay(250);
  168. strip->show();
  169. delay(250);
  170.  
  171.  
  172. // Clear the existing led values, then set all to red according to potentimeter reading.
  173. strip->clear();
  174. for (int led = 0; led < NUM_LEDS; led++) {
  175. strip->setPixelColor(led, 0, 255, 0, 0);
  176. }
  177. strip->show();
  178. delay(250);
  179. strip->show();
  180. delay(250);
  181. strip->show();
  182. delay(250);
  183. strip->show();
  184. delay(250);
  185. strip->show();
  186. delay(250);
  187. strip->show();
  188. delay(250);
  189. strip->show();
  190. delay(250);
  191. strip->show();
  192. delay(250);
  193.  
  194.  
  195. // Clear the existing led values, then set all to blue according to potentimeter reading.
  196. strip->clear();
  197. for (int led = 0; led < NUM_LEDS; led++) {
  198. strip->setPixelColor(led, 0, 0, 255, 0);
  199. }
  200. strip->show();
  201. delay(250);
  202. strip->show();
  203. delay(250);
  204. strip->show();
  205. delay(250);
  206. strip->show();
  207. delay(250);
  208. strip->show();
  209. delay(250);
  210. strip->show();
  211. delay(250);
  212. strip->show();
  213. delay(250);
  214. strip->show();
  215. delay(250);
  216.  
  217.  
  218. // Clear the existing led values, then set all to white according to potentimeter reading.
  219. strip->clear();
  220. for (int led = 0; led < NUM_LEDS; led++) {
  221. strip->setPixelColor(led, 0, 0, 0, 255);
  222. }
  223. strip->show();
  224. delay(250);
  225. strip->show();
  226. delay(250);
  227. strip->show();
  228. delay(250);
  229. strip->show();
  230. delay(250);
  231. strip->show();
  232. delay(250);
  233. strip->show();
  234. delay(250);
  235. strip->show();
  236. delay(250);
  237. strip->show();
  238. delay(250);
  239.  
  240. }
  241.  
  242.  
  243. // Slightly different, this makes the rainbow equally distributed throughout
  244. void rainbowCycle(uint8_t wait) {
  245. uint16_t i, j;
  246.  
  247. for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  248. for (i = 0; i < NUM_RAINBOW_LEDS; i++) {
  249. strip->setPixelColor(i, Wheel(((i * 256 / NUM_RAINBOW_LEDS) + j) & 255));
  250. // strip->setPixelColor(1, 255, 255, 255);
  251. }
  252. strip->show();
  253. delay(wait);
  254. }
  255. }
  256.  
  257. // Input a value 0 to 255 to get a color value.
  258. // The colours are a transition r - g - b - back to r.
  259. uint32_t Wheel(byte WheelPos) {
  260. WheelPos = 255 - WheelPos;
  261. if (WheelPos < 85) {
  262. return strip->Color(255 - WheelPos * 3, 0, WheelPos * 3);
  263. }
  264. if (WheelPos < 170) {
  265. WheelPos -= 85;
  266. return strip->Color(0, WheelPos * 3, 255 - WheelPos * 3);
  267. }
  268. WheelPos -= 170;
  269. return strip->Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  270. }
  271.  
  272. void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
  273.  
  274. if (whiteLength >= NUM_RAINBOW_LEDS) whiteLength = NUM_RAINBOW_LEDS - 1;
  275.  
  276. int head = whiteLength - 1;
  277. int tail = 0;
  278.  
  279. int loops = 3;
  280. int loopNum = 0;
  281.  
  282. static unsigned long lastTime = 0;
  283.  
  284.  
  285. while (true) {
  286. for (int j = 0; j < 256; j++) {
  287. for (uint16_t i = 0; i < NUM_RAINBOW_LEDS; i++) {
  288. if ((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ) {
  289. strip->setPixelColor(i, strip->Color(0, 0, 0, 255 ) );
  290. }
  291. else {
  292. strip->setPixelColor(i, Wheel(((i * 256 / NUM_RAINBOW_LEDS) + j) & 255));
  293. }
  294.  
  295. }
  296.  
  297. if (millis() - lastTime > whiteSpeed) {
  298. head++;
  299. tail++;
  300. if (head == NUM_RAINBOW_LEDS) {
  301. loopNum++;
  302. }
  303. lastTime = millis();
  304. }
  305.  
  306. if (loopNum == loops) return;
  307.  
  308. head %= NUM_RAINBOW_LEDS;
  309. tail %= NUM_RAINBOW_LEDS;
  310. strip->show();
  311. delay(wait);
  312. }
  313. }
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement