ESP32 Bluetooth to FastLED

First time posting so if i need to change anything please let me know, and kinda new to C++/coding.

I am using the ESP32 and I would like my FastLED effect to play with a BlueTooth command and keep playing until I send a new command with the new effect. I am able to connect to the bluetooth and switch cases no problem.

My problem is I cant figure out how to "repeat" the void command until a new button is pressed. I feel like I have danced around it for days now, and its time to ask for help from people smarter than myself. This is the original idea for the code, figured simple was better here. And thank you in advance for any help getting this working.

#include "BluetoothSerial.h"
#include <FastLED.h>

#define NUM_LEDs 50   //The number of LED using 
#define LED_PIN 21     // LED pin, 

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;


int received;
char receivedChar;
int g_Brightness = 200;

const char turnON = 'a';
const char turnOFF = 'b';
CRGB leds[NUM_LEDs] = {0};

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32Banana"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  Serial.println("Turn ON send: a");
  Serial.println("Turn OFF send: b");

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDs);
  FastLED.setBrightness(g_Brightness);
}

void loop()
{
  receivedChar = (char)SerialBT.read();

  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  if (SerialBT.available())
  {
    SerialBT.print("Gottcha:");
    SerialBT.println(receivedChar);
    Serial.print("Gottcha:");
    Serial.println(receivedChar);
    switch (receivedChar)
    {
      case turnON:
        NightRider();    
        break;
      case turnOFF:
        Off();
        break;
    }
  }
  delay(20);
}



void Off ()
{
  fadeToBlackBy(leds, NUM_LEDs, 3);
  FastLED.show();
}


void NightRider()
{
  uint8_t sinBeat  = map(beat8(50, 0), 0, 255, 0, NUM_LEDs - 1); 
  leds[sinBeat] = CRGB::Red; 

  fadeToBlackBy(leds, NUM_LEDs, 10);   

  FastLED.show();
}

Good job with using code tags.

My problem is I cant figure out how to "repeat" the void command until a new button is pressed.

Your code is very confused with multiple readings of SerialBT. The switch case should be separate from the input.

See if this doesn't do what you want.

#include "BluetoothSerial.h"
#include <FastLED.h>

#define NUM_LEDs 50   //The number of LED using 
#define LED_PIN 21     // LED pin, 

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;


int received;
char receivedChar;
int g_Brightness = 200;

const char turnON = 'a';
const char turnOFF = 'b';
CRGB leds[NUM_LEDs] = {0};

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32Banana"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  Serial.println("Turn ON send: a");
  Serial.println("Turn OFF send: b");

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDs);
  FastLED.setBrightness(g_Brightness);
}

void loop()
{
  //receivedChar = (char)SerialBT.read();

  //why have this serial input and bt output?
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }

  if (SerialBT.available()) {
    receivedChar = (char)SerialBT.read();
    //Serial.write(SerialBT.read());
    //echo back to monitor and phone
    Serial.print("Gottcha:");
    Serial.print(receivedChar); 
    SerialBT.print("Gottcha:");
    SerialBT.println(receivedChar);
  }
  /*
    if (SerialBT.available())
    {
     SerialBT.print("Gottcha:");
     SerialBT.println(receivedChar);
     Serial.print("Gottcha:");
     Serial.println(receivedChar);
  */

  switch (receivedChar)
  {
    case turnON:
      NightRider();
      break;
    case turnOFF:
      Off();
      break;
  }

delay(20);
}



void Off ()
{
  fadeToBlackBy(leds, NUM_LEDs, 3);
  FastLED.show();
}


void NightRider()
{
  uint8_t sinBeat  = map(beat8(50, 0), 0, 255, 0, NUM_LEDs - 1);
  leds[sinBeat] = CRGB::Red;

  fadeToBlackBy(leds, NUM_LEDs, 10);

  FastLED.show();
}

Thank you, I have been looking around for ideas and I noticed a common first comment was "read the rules" so I did a quick glance at them before posting.

Good idea but sadly still only calls the void command once. I played around with a toggle state but it got stuck and I couldn't get out.

I think you are coming out of the switch case when a newline is received at the end of the 'a' or 'b' command in the SerialBT.

Try set up your phone terminal app to send the command without any line endings. See if that gets you the repeats you want.

There are ways to ignore any line endings and just use the last valid command, but let's verify that that is the issue.

I think this is what you mean, please correct me if I am wrong but this, change the last serial.btprintln to just serial.btprint? If so I gave that a test run and same problem.

If not what command should I send thru the phone other than "a". Or if you could send me a link of different syntax's that would be perfect.

  if (SerialBT.available()) {
    receivedChar = (char)SerialBT.read();
    //Serial.write(SerialBT.read());
    //echo back to monitor and phone
    Serial.print("Gottcha:");
    Serial.print(receivedChar); 
    SerialBT.print("Gottcha:");
    SerialBT.print(receivedChar);
  }

What phone app are you using? I use Kai Morich Serial Bluetooth Terminal and there is a way in Settings to turn of any line endings when sending.

It was as simple as turning off one setting in the phone app.....Thank you so much. I would have never thought of that. tips hat in respect to the smartest person here

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.