Advertisement
JonD1988

RemoteLEDStripControl8StripRev0

Dec 19th, 2023
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 16.75 KB | None | 0 0
  1. //Rev 0 Developed on 12/19/2023 by Jonathan DeWitt.  This is almost a direct copy of "RemoteLEDStripControlRev1.ino" except it controls 8 strips instead of 1 strip.
  2. #include "BluetoothSerial.h" //Needed to communicate via Bluetooth
  3. #include <FastLED.h> //Needed to control LED Strip - In this case WS2812B
  4.  
  5. //Next 3 lines needed for Bluetooth communication
  6. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  7. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
  8. #endif
  9.  
  10. FASTLED_USING_NAMESPACE //From DemoReel100Rev4.ino Used for LED Strip Control
  11. #define LED_TYPE    WS2812B
  12. #define COLOR_ORDER GRB //From DemoReel100Rev4.ino Used for LED Strip Control
  13. #define NUM_LEDS    10 //From DemoReel100Rev4.ino Used for LED Strip Control
  14. //#define NUM_LEDS2 19 //Center strip or strip 2 has 19 LEDs
  15. //CRGB leds[NUM_LEDS]; //From DemoReel100Rev4.ino Used for LED Strip Control
  16. CRGB leds1[NUM_LEDS];
  17. CRGB leds2[NUM_LEDS];
  18. CRGB leds3[NUM_LEDS];
  19. CRGB leds4[NUM_LEDS];
  20. CRGB leds5[NUM_LEDS];
  21. CRGB leds6[NUM_LEDS];
  22. CRGB leds7[NUM_LEDS];
  23. CRGB leds8[NUM_LEDS];
  24. //#define BRIGHTNESS          96 //From DemoReel100Rev4.ino Used for LED Strip Control
  25. int BRIGHTNESS = 96;
  26. #define FRAMES_PER_SECOND  120 //From DemoReel100Rev4.ino Used for LED Strip Control
  27.  
  28. #define DATA_PIN1    13
  29. #define DATA_PIN2    27
  30. #define DATA_PIN3    26
  31. #define DATA_PIN4    25
  32. #define DATA_PIN5    33
  33. #define DATA_PIN6    32
  34. #define DATA_PIN7    23
  35. #define DATA_PIN8    22
  36.  
  37. BluetoothSerial SerialBT; //Creates an object called SerialBT to use in Bluetooth Communication
  38. char incomingSig; //Stores what character is read from the bluetooth module
  39. int PosGI, PosgI, LastDigit,BRIGHTNESStest, BrightResults;
  40.  
  41. void setup() {
  42.   Serial.begin(115200); //Starts the serial communication setting the baud rate
  43.   SerialBT.begin("DennisDesktopLampController"); //Bluetooth device name
  44.   Serial.println("The device started, now you can pair it with bluetooth!");
  45.   //FastLED.addLeds<LED_TYPE,LEDstrip1,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //Tell FastLED about the LED strip configuration - For LED Strip Control
  46.   FastLED.addLeds<LED_TYPE,DATA_PIN1,COLOR_ORDER>(leds1, NUM_LEDS).setCorrection(TypicalLEDStrip);
  47.   FastLED.addLeds<LED_TYPE,DATA_PIN2,COLOR_ORDER>(leds2, NUM_LEDS).setCorrection(TypicalLEDStrip);
  48.   FastLED.addLeds<LED_TYPE,DATA_PIN3,COLOR_ORDER>(leds3, NUM_LEDS).setCorrection(TypicalLEDStrip);
  49.   FastLED.addLeds<LED_TYPE,DATA_PIN4,COLOR_ORDER>(leds4, NUM_LEDS).setCorrection(TypicalLEDStrip);
  50.   FastLED.addLeds<LED_TYPE,DATA_PIN5,COLOR_ORDER>(leds5, NUM_LEDS).setCorrection(TypicalLEDStrip);
  51.   FastLED.addLeds<LED_TYPE,DATA_PIN6,COLOR_ORDER>(leds6, NUM_LEDS).setCorrection(TypicalLEDStrip);
  52.   FastLED.addLeds<LED_TYPE,DATA_PIN7,COLOR_ORDER>(leds7, NUM_LEDS).setCorrection(TypicalLEDStrip);
  53.   FastLED.addLeds<LED_TYPE,DATA_PIN8,COLOR_ORDER>(leds8, NUM_LEDS).setCorrection(TypicalLEDStrip);
  54.  
  55.   FastLED.setBrightness(BRIGHTNESS); //Set master brightness control for LED strip
  56.   BRIGHTNESStest=BRIGHTNESS; //Initialize BRIGHTNESStest to the same value as BRIGHTNESS so that BRIGHTNESStest is never 0
  57.   BrightResults=BRIGHTNESS;
  58. } //End of void setup
  59.  
  60. // List of patterns to cycle through.  Each is defined as a separate function below.
  61. typedef void (*SimplePatternList[])();
  62. SimplePatternList gPatterns = {rainbow, rainbowWithGlitter, confetti, allBlue, juggle, allWhite};
  63. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  64. uint8_t gHue = 0; //Rotating "base color" used by many of the patterns for LED Strip Control
  65.  
  66. void loop() {
  67.   if (SerialBT.available()) { //Reads from the ESP32 bluetooth receiving from the paired device
  68.     incomingSig=SerialBT.read(); //Reads from the ESP32 Bluetooth and stores it to the incoming Sig variable
  69.     Serial.println(incomingSig); //Prints to the Serial Monitor the contents of the incomingSig variable
  70.   }//End of if (SerialBT.available()) statement
  71.  
  72.   //Start the section of code which actually writes to the LED strip
  73.   gPatterns[gCurrentPatternNumber](); //Call the current pattern function once, updating the 'leds' array
  74.  
  75.   FastLED.show();  //Send the 'leds' array out to the actual LED strip - Turns strip on
  76.  
  77.   FastLED.delay(1000/FRAMES_PER_SECOND); //Insert a delay to keep the framerate modest for LED strip
  78.   //Do some periodic updates
  79.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } //Slowly cycle the "base color" through the rainbow
  80.   //End the section of code which actually writes to the LED strip
  81.  
  82.     switch(incomingSig)  //Reads the incoming blutetooth serial connection and decides what to do with it
  83.     {
  84.       case 'A':
  85.         gCurrentPatternNumber = 0; //Accesses the 1st element (index 0) of the array i.e. rainbow
  86.         break;
  87.       case 'C':
  88.         gCurrentPatternNumber = 1; //Accesses the 2nd element (index 1) of the array i.e. rainbowWithGlitter
  89.         break;
  90.       case 'E':
  91.         gCurrentPatternNumber = 2; //Accesses the 3rd element (index 2) of the array i.e. confetti
  92.         break;
  93.       case 'I':
  94.         gCurrentPatternNumber = 3; //Accesses the 4th element (index 3) of the array i.e. allBlue
  95.         break;
  96.       case 'K':
  97.         gCurrentPatternNumber = 4; //Accesses the 5th element (index 4) of the array i.e. juggle
  98.         break;
  99.       case 'M':
  100.         gCurrentPatternNumber = 5; //Accesses the 6th element (index 6) of the array i.e. allWhite
  101.         break;
  102.       case 'G':
  103.         BrightResults = readBrightness(); //calls the readBrightness function which extracts the new brightness level the user desires
  104.         break;
  105.     }//End of switch statement
  106.  
  107.   delay(20); //Delay between readings from Bluetooth
  108.  
  109.   if(BrightResults>0) //If brightvalue is non-zero then it updates the brightness
  110.   {
  111.       FastLED.setBrightness(BrightResults); //Set master brightness control for LED strip
  112.   } //End of BrightResults non-zero test if statement
  113.  
  114. }//End of void loop()
  115.  
  116. //Function definitions
  117. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  118.  
  119. void nextPattern()
  120. {
  121.   // add one to the current pattern number, and wrap around at the end
  122.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  123. }
  124.  
  125. void rainbow()
  126. {
  127.   // FastLED's built-in rainbow generator
  128.   fill_rainbow( leds1, NUM_LEDS, gHue, 7);
  129.   fill_rainbow( leds2, NUM_LEDS, gHue, 7);
  130.   fill_rainbow( leds3, NUM_LEDS, gHue, 7);
  131.   fill_rainbow( leds4, NUM_LEDS, gHue, 7);
  132.   fill_rainbow( leds5, NUM_LEDS, gHue, 7);
  133.   fill_rainbow( leds6, NUM_LEDS, gHue, 7);
  134.   fill_rainbow( leds7, NUM_LEDS, gHue, 7);
  135.   fill_rainbow( leds8, NUM_LEDS, gHue, 7);
  136. }
  137.  
  138. void rainbowWithGlitter()
  139. {
  140.   // built-in FastLED rainbow, plus some random sparkly glitter
  141.   rainbow();
  142.   addGlitter(80);
  143. }
  144.  
  145. void addGlitter( fract8 chanceOfGlitter)
  146. {
  147.   if( random8() < chanceOfGlitter) {
  148.     leds1[ random16(NUM_LEDS) ] += CRGB::White;
  149.     leds2[ random16(NUM_LEDS) ] += CRGB::White;
  150.     leds3[ random16(NUM_LEDS) ] += CRGB::White;
  151.     leds4[ random16(NUM_LEDS) ] += CRGB::White;
  152.     leds5[ random16(NUM_LEDS) ] += CRGB::White;
  153.     leds6[ random16(NUM_LEDS) ] += CRGB::White;
  154.     leds7[ random16(NUM_LEDS) ] += CRGB::White;
  155.     leds8[ random16(NUM_LEDS) ] += CRGB::White;
  156.   }
  157. }
  158.  
  159. void confetti()
  160. {
  161.   // random colored speckles that blink in and fade smoothly
  162.   fadeToBlackBy( leds1, NUM_LEDS, 10);
  163.   fadeToBlackBy( leds2, NUM_LEDS, 10);
  164.   fadeToBlackBy( leds3, NUM_LEDS, 10);
  165.   fadeToBlackBy( leds4, NUM_LEDS, 10);
  166.   fadeToBlackBy( leds5, NUM_LEDS, 10);
  167.   fadeToBlackBy( leds6, NUM_LEDS, 10);
  168.   fadeToBlackBy( leds7, NUM_LEDS, 10);
  169.   fadeToBlackBy( leds8, NUM_LEDS, 10);
  170.   int pos = random16(NUM_LEDS);
  171.   //int pos2 = random16(NUM_LEDS2); //Added to adjust for leds2 having a different number of LEDs
  172.   leds1[pos] += CHSV( gHue + random8(64), 200, 255);
  173.   leds2[pos] += CHSV( gHue + random8(64), 200, 255);
  174.   leds3[pos] += CHSV( gHue + random8(64), 200, 255);
  175.   leds4[pos] += CHSV( gHue + random8(64), 200, 255);
  176.   leds5[pos] += CHSV( gHue + random8(64), 200, 255);
  177.   leds6[pos] += CHSV( gHue + random8(64), 200, 255);
  178.   leds7[pos] += CHSV( gHue + random8(64), 200, 255);
  179.   leds8[pos] += CHSV( gHue + random8(64), 200, 255);
  180. }
  181.  
  182. void sinelon()
  183. {
  184.   // a colored dot sweeping back and forth, with fading trails
  185.   fadeToBlackBy( leds1, NUM_LEDS, 20);
  186.   //fadeToBlackBy( leds2, NUM_LEDS, 20);
  187.   //fadeToBlackBy( leds2, NUM_LEDS2, 20);
  188.   //fadeToBlackBy( leds3, NUM_LEDS, 20);
  189.   int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  190.   //int pos2 = beatsin16( 13, 0, NUM_LEDS2-1 ); //Added to adjust for leds2 having a different number of LEDs
  191.   leds1[pos] += CHSV( gHue, 255, 192);
  192.   //leds2[pos] += CHSV( gHue, 255, 192);
  193.   //leds2[pos2] += CHSV( gHue, 255, 192);
  194.   //leds3[pos] += CHSV( gHue, 255, 192);
  195. }
  196.  
  197. void bpm() //Note: BPM uses a for loop where NUM_LEDS is part of the loop iteration.  So I can't adjust for NUM_LEDS2
  198. {
  199.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  200.   uint8_t BeatsPerMinute = 62;
  201.   CRGBPalette16 palette = PartyColors_p;
  202.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  203.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  204.     leds1[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  205.     //leds2[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  206.     //leds3[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  207.   }
  208. }
  209.  
  210. void juggle() {
  211.   // eight colored dots, weaving in and out of sync with each other
  212.   fadeToBlackBy( leds1, NUM_LEDS, 20);
  213.   fadeToBlackBy( leds2, NUM_LEDS, 20);
  214.   fadeToBlackBy( leds3, NUM_LEDS, 20);
  215.   fadeToBlackBy( leds4, NUM_LEDS, 20);
  216.   fadeToBlackBy( leds5, NUM_LEDS, 20);
  217.   fadeToBlackBy( leds6, NUM_LEDS, 20);
  218.   fadeToBlackBy( leds7, NUM_LEDS, 20);
  219.   fadeToBlackBy( leds8, NUM_LEDS, 20);
  220.   uint8_t dothue = 0;
  221.   for( int i = 0; i < 8; i++) {
  222.     leds1[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  223.     leds2[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  224.     leds3[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  225.     leds4[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  226.     leds5[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  227.     leds6[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  228.     leds7[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  229.     leds8[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  230.     dothue += 32;
  231.   }
  232. }
  233.  
  234. void allWhite() //Sets LED strip to all white - Added by Jonathan DeWitt on 3/24/2022
  235. {
  236. for( int i = 0; i < NUM_LEDS; i++) { //9948
  237.     leds1[i].r = 255;
  238.     leds1[i].g = 255;
  239.     leds1[i].b = 255;
  240.     leds2[i].r = 255;
  241.     leds2[i].g = 255;
  242.     leds2[i].b = 255;
  243.     leds3[i].r = 255;
  244.     leds3[i].g = 255;
  245.     leds3[i].b = 255;
  246.     leds4[i].r = 255;
  247.     leds4[i].g = 255;
  248.     leds4[i].b = 255;
  249.     leds5[i].r = 255;
  250.     leds5[i].g = 255;
  251.     leds5[i].b = 255;
  252.     leds6[i].r = 255;
  253.     leds6[i].g = 255;
  254.     leds6[i].b = 255;
  255.     leds7[i].r = 255;
  256.     leds7[i].g = 255;
  257.     leds7[i].b = 255;
  258.     leds8[i].r = 255;
  259.     leds8[i].g = 255;
  260.     leds8[i].b = 255;
  261.   }
  262. } //End of allWhite function definition
  263.  
  264. int readBrightness()
  265. {
  266.   String incomingSigS;
  267.   char c;
  268.  
  269.   //while(c != 'g') //lowercase g in ascii is 103
  270.   for (int j=0; j<4; j++) //string begins with capital G and ends with lowercase g it has up to 3 digits in between
  271.   {
  272.     c=SerialBT.read(); //Reads one character at a time from the serial connection and stores it to the char type variable c
  273.     incomingSigS=String(incomingSigS + c); //Builds a string out of the incoming characters i.e. converts character type to string type
  274.     //Serial.print(incomingSigS);
  275.     //Serial.println("");
  276.   }
  277.  
  278.   PosGI=incomingSigS.indexOf("G"); //Obtains the index of the start character for the brightness
  279.   PosgI=incomingSigS.indexOf("g"); //Obtains the index of the end character for the brightness
  280.   LastDigit=PosgI-1; //Index of the last digit of the brightness is always one less than the end character
  281.   BRIGHTNESStest=incomingSigS.substring(PosGI+1, PosgI).toInt(); //Parses out the Brigthness Value
  282.   return BRIGHTNESStest;
  283. }//End of readBrightness function definition
  284.  
  285. void allBlue()
  286. { //Sets all LEDs blue - https://github.com/FastLED/FastLED/wiki/Controlling-leds
  287.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  288.     leds1[i].r = 0;
  289.     leds1[i].g = 0;
  290.     leds1[i].b = 255;
  291.     leds2[i].r = 0;
  292.     leds2[i].g = 0;
  293.     leds2[i].b = 255;
  294.     leds3[i].r = 0;
  295.     leds3[i].g = 0;
  296.     leds3[i].b = 255;
  297.     leds4[i].r = 0;
  298.     leds4[i].g = 0;
  299.     leds4[i].b = 255;
  300.     leds5[i].r = 0;
  301.     leds5[i].g = 0;
  302.     leds5[i].b = 255;
  303.     leds6[i].r = 0;
  304.     leds6[i].g = 0;
  305.     leds6[i].b = 255;
  306.     leds7[i].r = 0;
  307.     leds7[i].g = 0;
  308.     leds7[i].b = 255;
  309.     leds8[i].r = 0;
  310.     leds8[i].g = 0;
  311.     leds8[i].b = 255;
  312.   }
  313. } //End of allBlue function definition
  314.  
  315. void allChartreuse()
  316. { //Sets all LEDs Chartreuse - https://github.com/FastLED/FastLED/wiki/Controlling-leds
  317.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  318.     leds1[i].r = 127;
  319.     leds1[i].g = 255;
  320.     leds1[i].b = 0;
  321. //    leds2[i].r = 127;
  322. //    leds2[i].g = 255;
  323. //    leds2[i].b = 0;
  324. //    leds3[i].r = 127;
  325. //    leds3[i].g = 255;
  326. //    leds3[i].b = 0;
  327.   }
  328. } //End of allChartreuse function definition
  329.  
  330. void allBlank()
  331. { //Sets all LEDs blank - https://github.com/FastLED/FastLED/wiki/Controlling-leds
  332.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  333.     leds1[i].r = 0;
  334.     leds1[i].g = 0;
  335.     leds1[i].b = 0;
  336. //    leds2[i].r = 0;
  337. //    leds2[i].g = 0;
  338. //    leds2[i].b = 0;
  339. //    leds3[i].r = 0;
  340. //    leds3[i].g = 0;
  341. //    leds3[i].b = 0;
  342.   }
  343. } //End of allBlank function definition
  344.  
  345. //References
  346. //Reference 1- BluetoothLEDTest.ino Located in D:\Stuff\Projects\ESP32 Control\Bluetooth
  347. //Reference 2- DemoReel100Rev4 Located in D:\Stuff\Projects\Arduino Control\Miscellaneous Projects\Addressable LED Strips\Sketches
  348. //Reference 3- DemoReel100Rev1 Located in D:\Stuff\Projects\ESP32 Control\Miscellaneous Projects\Addressable LED Strips
  349. //Reference 4- https://www.rapidtables.com/web/color/html-color-codes.html This is a great way to find out what mixture of RGB colors to use
  350. //Reference 5- https://www.youtube.com/watch?v=E9EKqMYvLS4 Used for Transmitting Multiple Values to the receiving bluetooth app from ESP32
  351. //Reference 6- https://www.youtube.com/watch?v=JQ3tDhpmSFE&t=363s Same purpose of Reference 6 But Less Useful
  352. //Reference 7- http://ai2.appinventor.mit.edu/reference/blocks/lists.html#additems  Used to understand some MIT App Inventor Blocks
  353.  
  354. //If you are experiencing flickers of light specifically when running animations like the rainbow function but not with static colors like allBlue, it suggests that the flickering is likely related to the animation updates and timing.
  355. //Animations like rainbow require continuous updates to the LED strip at a faster pace, which can lead to timing conflicts with other parts of your code, including the Bluetooth communication. When animations are updating rapidly, the animation frame rate and the frequency of Bluetooth data processing might not be synchronized, leading to flickering issues.
  356. //To address this, you can try the following approaches:
  357. //1. Optimize Animation Updates: Make sure your animation functions are as efficient as possible. If they include any delay or blocking operations, consider using non-blocking techniques like millis() for timing to prevent delays in the main loop. Ensure that the animation update loop doesn't consume too much processing time, allowing for smoother Bluetooth communication.
  358. //2. Separate Animation Updates: Instead of running the animation update and Bluetooth communication within the same loop, consider using separate loops or timers for each. This can help ensure that the two tasks are executed independently and with the appropriate timing.
  359. //3. Use Timer Interrupts: You can use hardware timers or timer interrupts to schedule animation updates at a fixed interval. This can help maintain a consistent frame rate for animations, reducing the chance of flickering.
  360. //4. Adjust Frame Rate: Reduce the frame rate of the animations to a lower value to reduce the rate of animation updates. This may lessen the load on the system and improve synchronization with Bluetooth communication.
  361. //5. Prioritize Bluetooth Communication: If the animations are causing timing conflicts, consider prioritizing the Bluetooth communication and ensuring that it gets sufficient processing time.
  362. //6. Test with Simplified Code: To isolate the issue, try testing with a simplified version of your code that only includes the critical parts related to the animations and Bluetooth communication. This can help you pinpoint the cause of the flickering.
  363. //Remember that managing timing and animation updates can be tricky, especially when you have multiple tasks running concurrently. Thoroughly testing and fine-tuning your code is essential to achieve the desired behavior.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement