HC-05 LED Problem - Software Serial Monitor gives always the same String

Hello.
Apologize for the bad grammer ahead but I'll try my best to make myself clear.

First, I'd like to share two pieces of codes. Ones the master and one's the slave. Each part has a HC-05 module.

One is for sending informations, and one is for receiving it. In my case, I send the values of each color from RGB (0 too 255).

The following code is the function which sends the combined colors together, sends the String and is therefore the master.

void SendData()
{
  StringList = String(RED) + "," + String(GREEN) + "," + String(BLUE);
  HC05Serial.write('StringList');
}

The following code is the function for receiving the data from the master, so it's the slave.

void ReceiveData(void)
{
  if (HC05Serial.available() > 0)
  {
    StringList = HC05Serial.read();
  }

  StringList.toCharArray(stringBuffer, 100);

  for (str = strtok_r(stringBuffer, ",", &p); str; str = strtok_r(NULL, ",", &p))
  {
    colorRGBW++;
    switch (colorRGBW)
    {
      case 1:
        Upload = 1;
        break;

      case 2:
        Upload = 2;
        break;

      case 3:
        Upload = 3;
        colorRGBW = 0;
        break;
    }

    switch (Upload)
    {
      case 1:
        LAMPRED = atoi(str);
        break;
      case 2:
        LAMPGREEN = atoi(str);
        break;
      case 3:
        LAMPBLUE = atoi(str);
        break;
    }
  }
}

The code is all good and it has no syntax mistakes, but still I have a problem...

As soon as I open the Serial.Monitor on the slave part and try to read the variables, for example LAMPBLUE, I always get the number 116, no matter how the string from the master looks like.

For example, when I force the master over the software to send a string, which looks like this "255,0,0", that it would have to send only one color, I receive always the same at the slave .. namely "116,116,116".

The connection with the two modules does work btw. I checked the connection more than once, so the hardware should be correct. The NeoPixel, which gets the string later in the code also makes a reaction, but it does get always white. Of course that confirms, that it has a "116,116,116".

Does somebody know, what the problem could be or how I could get rid of this 116? I kinda can't relate, why exactly 116, so I decided to ask the forum. In the internet, I found sadly nothing to that.

If you need something, like more lines of the code, just respons shortly. I can send more if it's necassary.

Thanks in advance.
Stay healthy y'all ..

  HC05Serial.write('StringList');

Is this your actual code ?

Single quotes are used to enclose a single character
Double quotes are used to enclose a constant string
You have made a double mistake by enclosing the multi character name of a variable in single quotes

Try

HC05Serial.write(StringList);

UKHeliBob:

  HC05Serial.write('StringList');

Is this your actual code ?

Try

HC05Serial.write(StringList);

First, yes. It is. But not the whole one, as I said. The rest of the code has no affection tho. That's why I only sent these from above.

I've tried it, but all what comes back is the following message: "error: no matching function for call to 'SoftwareSerial::write(String&)"

Please, post your complete sketches for both Transmitter and Receiver along with Arduino types.

  if (HC05Serial.available() > 0)
  {
    StringList = HC05Serial.read();
  }

  StringList.toCharArray(stringBuffer, 100);

Seeing things like this always makes me think that the programmer could not make their mind up whether to use Strings or strings so used both

In the above snippet the program reads a single character from HC05Serial then puts it into a character array of 100 bytes. Why ?

Is that something different and where is the difference seen.

I guess I have to look up for that. Seems like I mixed something up?

GolamMostafa:
Please, post your complete sketches for both Transmitter and Receiver along with Arduino types.

Sure.

Here's the master.

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

#define Switch1 2
#define Switch2 3
#define Button 8
#define MOSFET 12
#define NeoPixel 13

SoftwareSerial HC05Serial (10, 11);
Adafruit_NeoPixel NPAdafruit = Adafruit_NeoPixel(1, NeoPixel, NEO_RGB + NEO_KHZ400);

int SwitchUp;
int SwitchDown;
int RED;
int GREEN;
int BLUE;
int FictionMode = 1;
int FictionWert;
int OfficialLamp = 0;
int OfficialMode = 0;
int OfficialWert = 0;
int OfficialModeOld = 0;
int OfficialWertOld = 0;
int OneShot;
int ButtonState;
int LampOld;

String StringList;




// This function sets the value of the potentiometer
int SetWert()
{
  FictionWert = analogRead(Poti);
  FictionWert = FictionWert / 4;

  return FictionWert;
}




// This function sets the current mode for the lamp by switching through RGBW and brightness
int SetMode()
{
  ButtonState = digitalRead(Button);

  if (ButtonState == 1)
  {
    if (OneShot == 0)
    {
      OneShot = 1;
      FictionMode++;
    }
  }

  if (ButtonState == 0)
  {
    OneShot = 0;
  }

  if (FictionMode >= 4)
  {
    FictionMode = 1;
  }

  return FictionMode;
}




// This function sets the current mode for the lamp by the THT NP
void ShowNeoPixel(int FictionWert, int FictionMode)
{
  switch (FictionMode)
  {
    case 1:
      // Gives the value to the correct mode
      RED =  FictionWert;
      NPAdafruit.setPixelColor(0, 10, 0, 0);
      NPAdafruit.show();
      break;

    case 2:
      // Gives the value to the correct mode
      GREEN =  FictionWert;
      NPAdafruit.setPixelColor(0, 0, 10, 0);
      NPAdafruit.show();
      break;

    case 3:
      // Gives the value to the correct mode
      BLUE =  FictionWert;
      NPAdafruit.setPixelColor(0, 0, 0, 10);
      NPAdafruit.show();
      break;
  }
}




// This function combines the colors/brightness to one string and sends it
void SendData()
{
  StringList = String(RED) + "," + String(GREEN) + "," + String(BLUE);
  //StringNewList = atoi(StringList);
  HC05Serial.write('StringList');
}




void setup()
{
  Serial.begin(38400);
  HC05Serial.begin(38400);
  NPAdafruit.begin();
  NPAdafruit.show();

  pinMode(Switch1, INPUT);
  pinMode(Switch2, INPUT);
  pinMode(Poti, INPUT);
  pinMode(Button, INPUT);
}




void loop()
{
  OfficialWertOld = OfficialWert;
  OfficialModeOld = OfficialMode;
  OfficialWert = SetWert();
  OfficialMode = SetMode();

  ShowNeoPixel(OfficialWert, OfficialMode);
    
  if ((OfficialWertOld != OfficialWert) || (OfficialModeOld != OfficialMode))
  {
    SendData();
  }
  delay(100);
}

And the following is the slave.

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

#define NPLampe 2
#define NeoPixel 3
#define Switch1 4
#define Switch2 5
#define Button 12
#define Poti A3

SoftwareSerial HC05Serial (10, 11);
Adafruit_NeoPixel NPAdafruit = Adafruit_NeoPixel(1, NeoPixel, NEO_RGB + NEO_KHZ400);
Adafruit_NeoPixel NPLamp = Adafruit_NeoPixel(1, NPLampe, NEO_RGB + NEO_KHZ400);

int Switch1;
int Switch2;
int colorRGBW = 0;
const int MAX_STRING_LEN = 500;
char stringBuffer[MAX_STRING_LEN];
char *str;
char *p;
String StringList = "0,0,0";
int FictionWert;
int FictionMode = 1;
int OfficialModeOld = 0;
int OfficialMode = 0;
int OfficialWertOld = 0;
int OfficialWert = 0;
int OfficialLampMode = 0;
int RED;
int GREEN;
int BLUE;
int WHITE;
int BRIGHTNESS;
int LAMPRED = 0;
int LAMPGREEN = 0;
int LAMPBLUE = 0;
int LAMPWHITE;
int LAMPBRIGHTNESS;
int ButtonState = 1;
int OneShot;
int Upload;
int once = 1;




void SetLampMode(void)
{
  SwitchUp= digitalRead(Switch1);
  SwitchDown= digitalRead(Switch2);
  if (LampMode1)
  {
    OfficialLampMode = 1;
  }
  else if (LampMode2)
  {
    OfficialLampMode = 2;
  }
  else
  {
    OfficialLampMode = 0;
  }
}




// This function sets the value of the potentiometer
int SetWert(void)
{
  FictionWert = analogRead(Poti);
  FictionWert = FictionWert / 4;

  return FictionWert;
}




// This function sets the current mode for the lamp by switching through RGBW and brightness
int SetMode(void)
{
  ButtonState = digitalRead(Button);
  if (ButtonState == 1)
  {
    if (OneShot == 1)
    {
      FictionMode++;
      OneShot = 0;
    }
  }
  if (ButtonState == 0)
  {
    OneShot = 1;
  }
  if (FictionMode >= 4)
  {
    FictionMode = 1;
  }

  return FictionMode;
}




// This function sets the current mode for the lamp by the THT NP
void ShowNeoPixel(int FictionMode, int FictionWert)
{
  switch (FictionMode)
  {
    case 1:
      // Gives the value to the correct mode
      RED =  FictionWert;
      NPAdafruit.setPixelColor(0, 100, 0, 0);
      NPAdafruit.show();
      break;

    case 2:
      // Gives the value to the correct mode
      GREEN =  FictionWert;
      NPAdafruit.setPixelColor(0, 0, 100, 0);
      NPAdafruit.show();
      break;

    case 3:
      // Gives the value to the correct mode
      BLUE =  FictionWert;
      NPAdafruit.setPixelColor(0, 0, 0, 100);
      NPAdafruit.show();
      break;
  }
}




void ClearNeoPixel(void)
{
  NPAdafruit.setPixelColor(0, 0, 0, 0);
  NPAdafruit.show();
}




// This function receives the string and split it up
void ReceiveData(void)
{
  if (HC05Serial.available() > 0)
  {
    StringList = HC05Serial.read();
  }

  StringList.toCharArray(stringBuffer, 100);
  for (str = strtok_r(stringBuffer, ",", &p); str; str = strtok_r(NULL, ",", &p))
  {
    colorRGBW++;
    switch (colorRGBW)
    {
      case 1:
        Upload = 1;
        break;

      case 2:
        Upload = 2;
        break;

      case 3:
        Upload = 3;
        colorRGBW = 0;
        break;
    }

    switch (Upload)
    {
      case 1:
        LAMPRED = atoi(str);
        break;
      case 2:
        LAMPGREEN = atoi(str);
        break;
      case 3:
        LAMPBLUE = atoi(str);
        break;
    }
  }
}




// Set the colors, including the brightness, via the module the lamp itself
void WriteLampDataWithModule(void)
{
  NPLamp.setPixelColor(0, LAMPRED, LAMPGREEN, LAMPBLUE);
  NPLamp.show();
}




// Set the colors, including the brightness, via the lamp the lamp itself
void WriteLampDataWithLamp(void)
{
  NPLamp.setPixelColor(0, RED, GREEN, BLUE);
  NPLamp.show();
}



void setup()
{
  Serial.begin(38400);
  HC05Serial.begin(38400);
  NPLamp.begin();
  NPLamp.show();
  NPAdafruit.begin();
  NPAdafruit.show();

  pinMode(Switch1, INPUT);
  pinMode(Switch2, INPUT);
  pinMode(Poti, INPUT);
  pinMode(Button, INPUT);
}

////////////////////////////////////////////////////////////////////

void loop()
{
  // Get into the function for the modes
  SetLampMode();
  OfficialWert = SetWert();
  OfficialMode = SetMode();

  if ((OfficialLampMode == 0) || (OfficialLampMode == 1))
  {
    // ... set the THT NP, including the lamp itself
    ShowNeoPixel(OfficialMode, OfficialWert);
    WriteLampDataWithLamp();
  }
  // ... otherwise ...
  else
  {
    // ... the lamp makes the colors/brightness from the module on
    ClearNeoPixel();
    ReceiveData();
    WriteLampDataWithModule();
  }
  
  delay(100);
}

I know that the codes aren't well written. I will edit it anyways. But tomorrow. Thanks so far. The help is appreciated!

softheartedboy:
Sure.

Your codes (both sketches) are not compiled. There are many undeclared variables. Get them fixed and then just check that the two Arduinos communicate over BTs and then add your other functionalities.

Is that something different and where is the difference seen.

As far as the Arduino environment is concerned a String is an object of the String library. Using them can fragment the little memory that a micro-controller has because there is no garbage collection. C style strings on the other hand are zero terminated arrays of chars which use memory more effectively and are to be preferred when memory is limited

Thank you both. That helped already much :slight_smile: