Help with Bluetooth (HC-05) Problem

Hello, my project consists of a 60 LED Strip, an RTC, and the HC-05 bluetooth module. My issue is that when I send the HC-05 simple commands such as single characters, I receive noisy output. I am using an Arduino UNO.

Example:

Input Output Percentage*
a No Output 20%
a 97 50%
a 216 10%
a 251 5%
a 246 5%
a 255 10%

*Approximate percentage of times I get the output (of 20 readings)

I believe the “No Output” readings can be attributed to the delay caused by the LED functions. That is fine. The issue is the inconsistent readings where ‘a’ produces a number other than the expected 97. Furthermore when I comment out the LED effects I get 100% accuracy with sending ‘a’ and receiving 97. Please help me pinpoint what is causing the issue between the LED’s and the Bluetooth module’s output.

Below is my code:

#include <Wire.h>
#include <stdio.h>
#include <DS3231.h>
#include "FastLED.h"
#include <SoftwareSerial.h>

//LED Strip Data WS2812
#define LED_PIN 9
#define NUM_LEDS 60
#define CHIPSET WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS], l, seconds;
#define BRIGHTNESS 128
byte lastsecond;

// Clock Settings
boolean mode = 0, lastsec = 1;
int count = 0, effectMode = 0;
int redSecond = 10;
#define MAX_EFFECTS 4

// Create a DS3231 object. (Real Time Clock)
DS3231  rtc(SDA, SCL);
Time  t;

// Bluetooth //
boolean debug = true;
SoftwareSerial BTserial(0, 1); // RX | TX
// max length of command is 20 chrs
const byte numChars = 20;
char receivedChars[numChars];
boolean newData = false;
char myChar ;
//fix IR from phone

/////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SETUP //////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

void setup() {
  delay(2000);
  Wire.begin();
  rtc.begin();
  Serial.begin(9600);
  BTserial.begin(9600);
  IRSensor.enableIRIn(); //Start IR Receivers
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );

}

/////////////////////////////////////////////////////////////////////////////
//////////////////////////////// MAIN LOOP //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

void loop() {

  int rGreen = random(256), rRed = random(256), rBlue = random(256);

  //Dims the clock after 9PM and before 6AM
  t = rtc.getTime();

  if (t.hour >= 21 || t.hour <= 6) LEDS.setBrightness(constrain(5, 0, 100));
  else LEDS.setBrightness(constrain(128, 10, 255));

  //*******************************
  //********** Effects ************
  //*******************************
  if ( mode == 0) {
    IRSensor.resume();
    for ( int i = 0; i < 240; i++) {
      BTInput();
      //*******DIFFERENT IMPLEMENTED EFFECTS*****
      switch (effectMode) {
        case 0:
          //printColors( rGreen, rRed, rBlue);
          Glow( rGreen, rRed, rBlue);
          break;
        case 2:
          Crazy(i);
          break;
        case 3:
          Temperature();
          break;
        default: //turns clock off
          mode = 1;
          break;
      }
      //************    END EFFECTS    **********

      // Changes to Clock mode
      if ( digitalRead(SENSOR_PIN) == 0 ) mode = 1;
      if ( mode == 1) {
        modeTransition();
        break;
      }
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
////////////////////////////////// EFFECTS //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

void Glow( int rGreen, int rRed, int rBlue) {

  for ( int j = 0; j < 60; j++) leds[j] = CRGB(rGreen, rRed, rBlue);
  if (count < 121) LEDS.setBrightness(constrain(count, 0, 128));
  else {
    int x = map(count, 121, 239, 121, 0);
    LEDS.setBrightness(constrain(x, 0, 128));
  }
  count++;
  if (count > 239) count = 0;
  ledShow(20);
}

/////////////////////////////////////////////////////////////////////////////
//////////////////////////////// HELPER FUNCTIONS ///////////////////////////
/////////////////////////////////////////////////////////////////////////////

void printColors(int g, int r, int b) {
  Serial.println("Current Color Settings: (G,R,B)");
  Serial.println(g);
  Serial.println(r);
  Serial.println(b);
}

void ledShow(int refresh) {
  FastLED.show();
  FastLED.delay(refresh);
}

/////////////////////////////////////////////////////////////////////////////
////////////////////////////// BLUETOOTH FUNCTIONS //////////////////////////
/////////////////////////////////////////////////////////////////////////////

void BTInput() {
  if (BTserial.available() > 0)     {
    Serial.println(BTserial.read());
  }
}

void BTInput2() {
  while (BTserial.available() ) {
    myChar = BTserial.read();
    Serial.print(myChar);
  }

  while (Serial.available()) {
    myChar = Serial.read();
    BTserial.print(myChar);
  }
}

void recvWithStartEndMarkers()
{

  // function recvWithStartEndMarkers by Robin2 of the Arduino forums
  // See  http://forum.arduino.cc/index.php?topic=288234.0

  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  if (BTserial.available() > 0)
  {
    rc = BTserial.read();
    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }

}

void parseData()
{
  newData = false;

  if (debug) {
    Serial.println( receivedChars );
  }

  if (receivedChars[0] == 'O'  && receivedChars[1] == 'N' )  {
    //Serial.println("on");
  }
  if (receivedChars[0] == 'O'  && receivedChars[1] == 'F' )  {
    //Serial.println("off");
  }
}

Edit: I am using my Samsung Galaxy S6 and a Bluetooth Terminal App to communicate with the Bluetooth module.

CRGB leds[NUM_LEDS], l, seconds;

Why are you creating an instance of the CRGB class called seconds? Why are you creating an instance of the CRGB class called l? I had to copy and paste that character, since I can't tell if it is a one, a lower case l, or a pipe symbol.

SoftwareSerial BTserial(0, 1); // RX | TX

It makes absolutely no sense to do SoftwareSerial on the hardware serial pins.

  Serial.begin(9600);

And you sure as hell can't do it while doing hardware serial on those same pins.

Hello and thank you for your response.

PaulS:

CRGB leds[NUM_LEDS], l, seconds;

Why are you creating an instance of the CRGB class called seconds? Why are you creating an instance of the CRGB class called l? I had to copy and paste that character, since I can't tell if it is a one, a lower case l, or a pipe symbol.

Those are used as placeholders elsewhere in the program to accurately depict the seconds LED moving around the clock. I condensed the code to focus on the problem and save users of the forum the pain from going through my entire project. However, I see that I overlooked the setup/initialization.

PaulS:

SoftwareSerial BTserial(0, 1); // RX | TX

It makes absolutely no sense to do SoftwareSerial on the hardware serial pins.

  Serial.begin(9600);

And you sure as hell can't do it while doing hardware serial on those same pins.

After reading about the SoftwareSerial library I understand your point. Being unfamiliar with communication devices I was following a tutorial which led me astray. I will follow-up when I've revised my code.

After researching the aforementioned issues I revised and condensed my code in order to isolate the main issue.

#include <Wire.h>
#include "FastLED.h"
#include <SoftwareSerial.h>

//LED Strip Data WS2812
#define LED_PIN 9
#define NUM_LEDS 60
#define CHIPSET WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 128
// Sensors
#define SENSOR_PIN 2 //Touch Sensor
// Clock Settings
boolean mode = 0;
int count = 0;
SoftwareSerial BTserial(10, 11); // RX | TX


void setup() {
  
  delay(1000);
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {}
  BTserial.begin(9600);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
}


void loop() {
  
  int rGreen = random(256), rRed = random(256), rBlue = random(256);
  for (int i = 0; i < 240; i++) {
    if (BTserial.available()) Serial.write(BTserial.read());
    if (mode == 0) Glow(rGreen, rRed, rBlue);
  }
  mode = 1;
}


void Glow( int rGreen, int rRed, int rBlue) {
  
  for ( int j = 0; j < NUM_LEDS; j++) leds[j] = CRGB(rGreen, rRed, rBlue);
  if (count < 121) LEDS.setBrightness(constrain(count, 0, 128));
  else {
    int x = map(count, 121, 239, 121, 0);
    LEDS.setBrightness(constrain(x, 0, 128));
  }
  count++;
  if (count > 239) count = 0;
  FastLED.show();
  FastLED.delay(20);
}

After inputting the numbers 1-20 I had the following results:

While glow was running: 1 2 4 ñ þ ó 8 9 10 11 Ä 13 þ š 16 17 þ 19 ‚þ
Without glow running: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Demothenes:
After researching the aforementioned issues I revised and condensed my code in order to isolate the main issue.

#include <Wire.h>

#include “FastLED.h”
#include <SoftwareSerial.h>

//LED Strip Data WS2812
#define LED_PIN 9
#define NUM_LEDS 60
#define CHIPSET WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 128
// Sensors
#define SENSOR_PIN 2 //Touch Sensor
// Clock Settings
boolean mode = 0;
int count = 0;
SoftwareSerial BTserial(10, 11); // RX | TX

void setup() {
 
  delay(1000);
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {}
  BTserial.begin(9600);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
}

void loop() {
 
  int rGreen = random(256), rRed = random(256), rBlue = random(256);
  for (int i = 0; i < 240; i++) {
    if (BTserial.available()) Serial.write(BTserial.read());
    if (mode == 0) Glow(rGreen, rRed, rBlue);
  }
  mode = 1;
}

void Glow( int rGreen, int rRed, int rBlue) {
 
  for ( int j = 0; j < NUM_LEDS; j++) leds[j] = CRGB(rGreen, rRed, rBlue);
  if (count < 121) LEDS.setBrightness(constrain(count, 0, 128));
  else {
    int x = map(count, 121, 239, 121, 0);
    LEDS.setBrightness(constrain(x, 0, 128));
  }
  count++;
  if (count > 239) count = 0;
  FastLED.show();
  FastLED.delay(20);
}




After inputting the numbers 1-20 I had the following results:

While glow was running: 1 2 4 ñ þ ó 8 9 10 11 Ä 13 þ š 16 17 þ 19 ‚þ
Without glow running: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

bump

Hello Demothenes

I've exactly the same problem. Did you find a solution for this?