FastLED and Bluetooth

Hello everyone,

This is my first topic here and I hope this is in the right topic.

Now, I would like to control my WS2812B leds with my HC-05 bluetooth module.
This is kind of a problem for me since I do not know much about bluetooth, so I am a total noob. I do kind of understand FastLED, but I do not know how to implement bluetooth control in it. When I know how to do that I might know how to make an app to control the leds with, using App Inventor 2.

Can someone take a look at this working spectrum cycle I made for my leds and maybe explain how to implement bluetooth control in it? Can’t seem to find much information about using bluetooth + FastLED, so all the help I can get is very much appreciated!

Thanks in advance!
Staijn

#include "FastLED.h"

#define NUM_LEDS 30
#define PIN 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
int l;
int r;
int g;
int b;
CRGB leds[NUM_LEDS];


void setup() {
 FastLED.addLeds<LED_TYPE,PIN,COLOR_ORDER>(leds, NUM_LEDS);
 
}
void loop(){
 
    //ADD RED.. ONLY RED
     for(r=0;r<256;r++)
     {
        for(l=0;l<NUM_LEDS;l++)
        {
            leds[l].red = r;
            leds[l].green = g;
            leds[l].blue = b;
        }
        delay(30);
    }
    FastLED.show();
    
    //ADD GREEN, RED AND GREEN
     r = 255;
     for(g=0;g<256;g++)
     {
        for(l=0;l<NUM_LEDS;l++)
        {
            leds[l].red = r;
            leds[l].green = g;
            leds[l].blue = b;
        }
    FastLED.show();
    delay(30);  
  
    }
    //SUBTRACT RED, ONLY GREEN
    
    for(r=255;r>0;r--)
    {
        for(l=0;l<NUM_LEDS;l++)
        {
            leds[l].red = r;
            leds[l].green = 255;
            leds[l].blue = 0;
        }
    FastLED.show();
    delay(30);
    }
     
     
     //ADD BLUE, GREEN AND BLUE
     g = 255;
     for(b=0;b<256;b++)
     {
        for(l=0;l<NUM_LEDS;l++)
        {
            leds[l].red = r;
            leds[l].green = g;
            leds[l].blue = b;
        }
    FastLED.show();
    delay(30);  
    }
    //SUBTRACT GREEN, ONLY BLUE
    
    for(g=255;g>0;g--)
    {
        for(l=0;l<NUM_LEDS;l++)
        {
            leds[l].red = r;
            leds[l].green = g;
            leds[l].blue = 255;
        }
    FastLED.show();
    delay(30);
    }
   //ADD RED, RED AND BLUE
   for(r=0;r<256;r++)
   {
      for(l=0;l<NUM_LEDS;l++)
      {
          leds[l].red = r;
          leds[l].green = g;
          leds[l].blue = 255;
      }
  
     FastLED.show();
      delay(30);
  }
  //SUBTRACT BLUE, BACK TO RED TO LOOP AGAIN
      r = 255;
      for(b=255;b>0;b--)
      {
        for(l=0;l<NUM_LEDS;l++)

        {
            leds[l].red = r;
            leds[l].green = 0;
            leds[l].blue = b;
        }
      FastLED.show();
      delay(30);
      }  
}

why don't you put this code aside for the time being and explore tutorial on bluetooth? there are tons online. Once you get how things work, you can merge both

Hello,

Thank you for your reply.

I’ve looked at multiple sketches and apps, and none of them work sadly.
I did get one to work, a sketch where the creator used the leds to decorate his hat. The sketch works, the app works, but not entirely to my liking yet, and that is why I want to learn what is going on so I can change the code so it is to my liking. I really am not sure what is all going on here, and when I click on serial monitor it shows weird gibberish like ???!?A and stuff. This is the arduino code

// Pro Mini 3.3V
// Blutooth pin 3 - RX, pin 4 - TX
// LED Strip 16 leds - pin 6 - DATA
// Lipo 3.7V

#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>

#define  SERIAL_PORT_SPEED  115200
#define  BT_PORT_SPEED  9600

#define PINLED 6
#define NumLED 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NumLED, PINLED, NEO_GRB + NEO_KHZ800);

int Mode = 0;
int ParameterR = 0;
int ParameterG = 0;
int ParameterB = 170;
int ParameterSpeed = 60;
int ParameterBrightness = 130;
String MsgBT = "";
String strTemp;
char c;
SoftwareSerial BT(0, 1);// RX | TX


void setup() {
  strip.begin();
  strip.setBrightness(ParameterBrightness);
  strip.show(); // Initialize all pixels to 'off'
  BT.begin(BT_PORT_SPEED);
  Serial.begin(SERIAL_PORT_SPEED);
  MsgBT.reserve(150);
}

void loop() {

  if (BT.available())
  {
    MsgBT = BT.readString();

    Serial.print("bytes read: ");
    Serial.println(MsgBT.length());
    Serial.println(MsgBT);

    if (MsgBT == "999") { // Turn Off
      TurnOff();
      Mode = 1;
    } else
    {
      strTemp = (MsgBT.substring(0, MsgBT.indexOf(",")));
      ParameterR = strTemp.toInt();
      MsgBT = MsgBT.substring(MsgBT.indexOf(",") + 1);

      strTemp = (MsgBT.substring(0, MsgBT.indexOf(",")));
      ParameterG = strTemp.toInt();
      MsgBT = MsgBT.substring(MsgBT.indexOf(",") + 1);

      strTemp = (MsgBT.substring(0, MsgBT.indexOf(",")));
      ParameterB = strTemp.toInt();
      MsgBT = MsgBT.substring(MsgBT.indexOf(",") + 1);

      strTemp = (MsgBT.substring(0, MsgBT.indexOf(",")));
      ParameterSpeed = strTemp.toInt();
      MsgBT = MsgBT.substring(MsgBT.indexOf(",") + 1);

      strTemp = (MsgBT.substring(0, MsgBT.indexOf("#")));
      ParameterBrightness = strTemp.toInt();

      Serial.print(ParameterR);
      Serial.print(",");
      Serial.print(ParameterG);
      Serial.print(",");
      Serial.print(ParameterB);
      Serial.print(",");
      Serial.print(ParameterSpeed);
      Serial.print(",");
      Serial.print(ParameterBrightness);
      Serial.println();

      Mode = 0;
      MsgBT = "";
    }
  }

  switch (Mode) {
    case 0:
      strip.setBrightness(ParameterBrightness);
      strip.show();
      if (ParameterSpeed == 400) {
        for (uint16_t i = 0; i < strip.numPixels(); i++)
          strip.setPixelColor(i, strip.Color(ParameterR, ParameterG, ParameterB));
        strip.show();
        Mode = 1;
      }
      else {
        colorWipe(strip.Color(ParameterR, ParameterG, ParameterB), ParameterSpeed);
        if (BT.available()) break;
        colorWipe(strip.Color(0, 0, 0), ParameterSpeed);

      }
      break;
    case 1:
      Serial.println("Mode 1");
      break;
  }
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    if (BT.available()) return;
    delay(wait);
  }
}

void TurnOff() {
  for (uint16_t i = 0; i < strip.numPixels(); i++)
    strip.setPixelColor(i, strip.Color(0, 0, 0));
  strip.show();
}

when I click on serial monitor it shows weird gibberish

have you set the terminal to 115200 bauds?

Not yet, will do soon. Thanks!

Yes it works! I can read the serial monitor now! Thanks a lot!

:slight_smile: