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.