Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- #include "BluetoothSerial.h" //Needed to communicate via Bluetooth
- #include <FastLED.h> //Needed to control LED Strip - In this case WS2812B
- //Next 3 lines needed for Bluetooth communication
- #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
- #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
- #endif
- FASTLED_USING_NAMESPACE //From DemoReel100Rev4.ino Used for LED Strip Control
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB //From DemoReel100Rev4.ino Used for LED Strip Control
- #define NUM_LEDS 10 //From DemoReel100Rev4.ino Used for LED Strip Control
- //#define NUM_LEDS2 19 //Center strip or strip 2 has 19 LEDs
- //CRGB leds[NUM_LEDS]; //From DemoReel100Rev4.ino Used for LED Strip Control
- CRGB leds1[NUM_LEDS];
- CRGB leds2[NUM_LEDS];
- CRGB leds3[NUM_LEDS];
- CRGB leds4[NUM_LEDS];
- CRGB leds5[NUM_LEDS];
- CRGB leds6[NUM_LEDS];
- CRGB leds7[NUM_LEDS];
- CRGB leds8[NUM_LEDS];
- //#define BRIGHTNESS 96 //From DemoReel100Rev4.ino Used for LED Strip Control
- int BRIGHTNESS = 96;
- #define FRAMES_PER_SECOND 120 //From DemoReel100Rev4.ino Used for LED Strip Control
- #define DATA_PIN1 13
- #define DATA_PIN2 27
- #define DATA_PIN3 26
- #define DATA_PIN4 25
- #define DATA_PIN5 33
- #define DATA_PIN6 32
- #define DATA_PIN7 23
- #define DATA_PIN8 22
- BluetoothSerial SerialBT; //Creates an object called SerialBT to use in Bluetooth Communication
- char incomingSig; //Stores what character is read from the bluetooth module
- int PosGI, PosgI, LastDigit,BRIGHTNESStest, BrightResults;
- void setup() {
- Serial.begin(115200); //Starts the serial communication setting the baud rate
- SerialBT.begin("DennisDesktopLampController"); //Bluetooth device name
- Serial.println("The device started, now you can pair it with bluetooth!");
- //FastLED.addLeds<LED_TYPE,LEDstrip1,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //Tell FastLED about the LED strip configuration - For LED Strip Control
- FastLED.addLeds<LED_TYPE,DATA_PIN1,COLOR_ORDER>(leds1, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN2,COLOR_ORDER>(leds2, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN3,COLOR_ORDER>(leds3, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN4,COLOR_ORDER>(leds4, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN5,COLOR_ORDER>(leds5, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN6,COLOR_ORDER>(leds6, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN7,COLOR_ORDER>(leds7, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE,DATA_PIN8,COLOR_ORDER>(leds8, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS); //Set master brightness control for LED strip
- BRIGHTNESStest=BRIGHTNESS; //Initialize BRIGHTNESStest to the same value as BRIGHTNESS so that BRIGHTNESStest is never 0
- BrightResults=BRIGHTNESS;
- } //End of void setup
- // List of patterns to cycle through. Each is defined as a separate function below.
- typedef void (*SimplePatternList[])();
- SimplePatternList gPatterns = {rainbow, rainbowWithGlitter, confetti, allBlue, juggle, allWhite};
- uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
- uint8_t gHue = 0; //Rotating "base color" used by many of the patterns for LED Strip Control
- void loop() {
- if (SerialBT.available()) { //Reads from the ESP32 bluetooth receiving from the paired device
- incomingSig=SerialBT.read(); //Reads from the ESP32 Bluetooth and stores it to the incoming Sig variable
- Serial.println(incomingSig); //Prints to the Serial Monitor the contents of the incomingSig variable
- }//End of if (SerialBT.available()) statement
- //Start the section of code which actually writes to the LED strip
- gPatterns[gCurrentPatternNumber](); //Call the current pattern function once, updating the 'leds' array
- FastLED.show(); //Send the 'leds' array out to the actual LED strip - Turns strip on
- FastLED.delay(1000/FRAMES_PER_SECOND); //Insert a delay to keep the framerate modest for LED strip
- //Do some periodic updates
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } //Slowly cycle the "base color" through the rainbow
- //End the section of code which actually writes to the LED strip
- switch(incomingSig) //Reads the incoming blutetooth serial connection and decides what to do with it
- {
- case 'A':
- gCurrentPatternNumber = 0; //Accesses the 1st element (index 0) of the array i.e. rainbow
- break;
- case 'C':
- gCurrentPatternNumber = 1; //Accesses the 2nd element (index 1) of the array i.e. rainbowWithGlitter
- break;
- case 'E':
- gCurrentPatternNumber = 2; //Accesses the 3rd element (index 2) of the array i.e. confetti
- break;
- case 'I':
- gCurrentPatternNumber = 3; //Accesses the 4th element (index 3) of the array i.e. allBlue
- break;
- case 'K':
- gCurrentPatternNumber = 4; //Accesses the 5th element (index 4) of the array i.e. juggle
- break;
- case 'M':
- gCurrentPatternNumber = 5; //Accesses the 6th element (index 6) of the array i.e. allWhite
- break;
- case 'G':
- BrightResults = readBrightness(); //calls the readBrightness function which extracts the new brightness level the user desires
- break;
- }//End of switch statement
- delay(20); //Delay between readings from Bluetooth
- if(BrightResults>0) //If brightvalue is non-zero then it updates the brightness
- {
- FastLED.setBrightness(BrightResults); //Set master brightness control for LED strip
- } //End of BrightResults non-zero test if statement
- }//End of void loop()
- //Function definitions
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
- void nextPattern()
- {
- // add one to the current pattern number, and wrap around at the end
- gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
- }
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds1, NUM_LEDS, gHue, 7);
- fill_rainbow( leds2, NUM_LEDS, gHue, 7);
- fill_rainbow( leds3, NUM_LEDS, gHue, 7);
- fill_rainbow( leds4, NUM_LEDS, gHue, 7);
- fill_rainbow( leds5, NUM_LEDS, gHue, 7);
- fill_rainbow( leds6, NUM_LEDS, gHue, 7);
- fill_rainbow( leds7, NUM_LEDS, gHue, 7);
- fill_rainbow( leds8, NUM_LEDS, gHue, 7);
- }
- void rainbowWithGlitter()
- {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- void addGlitter( fract8 chanceOfGlitter)
- {
- if( random8() < chanceOfGlitter) {
- leds1[ random16(NUM_LEDS) ] += CRGB::White;
- leds2[ random16(NUM_LEDS) ] += CRGB::White;
- leds3[ random16(NUM_LEDS) ] += CRGB::White;
- leds4[ random16(NUM_LEDS) ] += CRGB::White;
- leds5[ random16(NUM_LEDS) ] += CRGB::White;
- leds6[ random16(NUM_LEDS) ] += CRGB::White;
- leds7[ random16(NUM_LEDS) ] += CRGB::White;
- leds8[ random16(NUM_LEDS) ] += CRGB::White;
- }
- }
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds1, NUM_LEDS, 10);
- fadeToBlackBy( leds2, NUM_LEDS, 10);
- fadeToBlackBy( leds3, NUM_LEDS, 10);
- fadeToBlackBy( leds4, NUM_LEDS, 10);
- fadeToBlackBy( leds5, NUM_LEDS, 10);
- fadeToBlackBy( leds6, NUM_LEDS, 10);
- fadeToBlackBy( leds7, NUM_LEDS, 10);
- fadeToBlackBy( leds8, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- //int pos2 = random16(NUM_LEDS2); //Added to adjust for leds2 having a different number of LEDs
- leds1[pos] += CHSV( gHue + random8(64), 200, 255);
- leds2[pos] += CHSV( gHue + random8(64), 200, 255);
- leds3[pos] += CHSV( gHue + random8(64), 200, 255);
- leds4[pos] += CHSV( gHue + random8(64), 200, 255);
- leds5[pos] += CHSV( gHue + random8(64), 200, 255);
- leds6[pos] += CHSV( gHue + random8(64), 200, 255);
- leds7[pos] += CHSV( gHue + random8(64), 200, 255);
- leds8[pos] += CHSV( gHue + random8(64), 200, 255);
- }
- void sinelon()
- {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds1, NUM_LEDS, 20);
- //fadeToBlackBy( leds2, NUM_LEDS, 20);
- //fadeToBlackBy( leds2, NUM_LEDS2, 20);
- //fadeToBlackBy( leds3, NUM_LEDS, 20);
- int pos = beatsin16( 13, 0, NUM_LEDS-1 );
- //int pos2 = beatsin16( 13, 0, NUM_LEDS2-1 ); //Added to adjust for leds2 having a different number of LEDs
- leds1[pos] += CHSV( gHue, 255, 192);
- //leds2[pos] += CHSV( gHue, 255, 192);
- //leds2[pos2] += CHSV( gHue, 255, 192);
- //leds3[pos] += CHSV( gHue, 255, 192);
- }
- 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
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds1[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- //leds2[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- //leds3[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- }
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds1, NUM_LEDS, 20);
- fadeToBlackBy( leds2, NUM_LEDS, 20);
- fadeToBlackBy( leds3, NUM_LEDS, 20);
- fadeToBlackBy( leds4, NUM_LEDS, 20);
- fadeToBlackBy( leds5, NUM_LEDS, 20);
- fadeToBlackBy( leds6, NUM_LEDS, 20);
- fadeToBlackBy( leds7, NUM_LEDS, 20);
- fadeToBlackBy( leds8, NUM_LEDS, 20);
- uint8_t dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds1[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds2[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds3[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds4[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds5[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds6[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds7[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- leds8[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
- void allWhite() //Sets LED strip to all white - Added by Jonathan DeWitt on 3/24/2022
- {
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds1[i].r = 255;
- leds1[i].g = 255;
- leds1[i].b = 255;
- leds2[i].r = 255;
- leds2[i].g = 255;
- leds2[i].b = 255;
- leds3[i].r = 255;
- leds3[i].g = 255;
- leds3[i].b = 255;
- leds4[i].r = 255;
- leds4[i].g = 255;
- leds4[i].b = 255;
- leds5[i].r = 255;
- leds5[i].g = 255;
- leds5[i].b = 255;
- leds6[i].r = 255;
- leds6[i].g = 255;
- leds6[i].b = 255;
- leds7[i].r = 255;
- leds7[i].g = 255;
- leds7[i].b = 255;
- leds8[i].r = 255;
- leds8[i].g = 255;
- leds8[i].b = 255;
- }
- } //End of allWhite function definition
- int readBrightness()
- {
- String incomingSigS;
- char c;
- //while(c != 'g') //lowercase g in ascii is 103
- 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
- {
- c=SerialBT.read(); //Reads one character at a time from the serial connection and stores it to the char type variable c
- incomingSigS=String(incomingSigS + c); //Builds a string out of the incoming characters i.e. converts character type to string type
- //Serial.print(incomingSigS);
- //Serial.println("");
- }
- PosGI=incomingSigS.indexOf("G"); //Obtains the index of the start character for the brightness
- PosgI=incomingSigS.indexOf("g"); //Obtains the index of the end character for the brightness
- LastDigit=PosgI-1; //Index of the last digit of the brightness is always one less than the end character
- BRIGHTNESStest=incomingSigS.substring(PosGI+1, PosgI).toInt(); //Parses out the Brigthness Value
- return BRIGHTNESStest;
- }//End of readBrightness function definition
- void allBlue()
- { //Sets all LEDs blue - https://github.com/FastLED/FastLED/wiki/Controlling-leds
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds1[i].r = 0;
- leds1[i].g = 0;
- leds1[i].b = 255;
- leds2[i].r = 0;
- leds2[i].g = 0;
- leds2[i].b = 255;
- leds3[i].r = 0;
- leds3[i].g = 0;
- leds3[i].b = 255;
- leds4[i].r = 0;
- leds4[i].g = 0;
- leds4[i].b = 255;
- leds5[i].r = 0;
- leds5[i].g = 0;
- leds5[i].b = 255;
- leds6[i].r = 0;
- leds6[i].g = 0;
- leds6[i].b = 255;
- leds7[i].r = 0;
- leds7[i].g = 0;
- leds7[i].b = 255;
- leds8[i].r = 0;
- leds8[i].g = 0;
- leds8[i].b = 255;
- }
- } //End of allBlue function definition
- void allChartreuse()
- { //Sets all LEDs Chartreuse - https://github.com/FastLED/FastLED/wiki/Controlling-leds
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds1[i].r = 127;
- leds1[i].g = 255;
- leds1[i].b = 0;
- // leds2[i].r = 127;
- // leds2[i].g = 255;
- // leds2[i].b = 0;
- // leds3[i].r = 127;
- // leds3[i].g = 255;
- // leds3[i].b = 0;
- }
- } //End of allChartreuse function definition
- void allBlank()
- { //Sets all LEDs blank - https://github.com/FastLED/FastLED/wiki/Controlling-leds
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds1[i].r = 0;
- leds1[i].g = 0;
- leds1[i].b = 0;
- // leds2[i].r = 0;
- // leds2[i].g = 0;
- // leds2[i].b = 0;
- // leds3[i].r = 0;
- // leds3[i].g = 0;
- // leds3[i].b = 0;
- }
- } //End of allBlank function definition
- //References
- //Reference 1- BluetoothLEDTest.ino Located in D:\Stuff\Projects\ESP32 Control\Bluetooth
- //Reference 2- DemoReel100Rev4 Located in D:\Stuff\Projects\Arduino Control\Miscellaneous Projects\Addressable LED Strips\Sketches
- //Reference 3- DemoReel100Rev1 Located in D:\Stuff\Projects\ESP32 Control\Miscellaneous Projects\Addressable LED Strips
- //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
- //Reference 5- https://www.youtube.com/watch?v=E9EKqMYvLS4 Used for Transmitting Multiple Values to the receiving bluetooth app from ESP32
- //Reference 6- https://www.youtube.com/watch?v=JQ3tDhpmSFE&t=363s Same purpose of Reference 6 But Less Useful
- //Reference 7- http://ai2.appinventor.mit.edu/reference/blocks/lists.html#additems Used to understand some MIT App Inventor Blocks
- //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.
- //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.
- //To address this, you can try the following approaches:
- //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.
- //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.
- //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.
- //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.
- //5. Prioritize Bluetooth Communication: If the animations are causing timing conflicts, consider prioritizing the Bluetooth communication and ensuring that it gets sufficient processing time.
- //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.
- //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