Comparing Strings, Wacky Serial Data, I dont know

Hello!

I am working with FastLED right now, communicating through Serial.

I have a bunch of commands going through, like “m” for masterswitch, “b” for brightness, etc.

Currently, I got the commands to work, but something happened and it fell apart. The same lines of code don’t compare strings (the string “command” to string “m”).

I have 0 issues with the led patterns themselves. It is only with the reading commands.

And also, if I remove this line from the main loop, it wont read serial all together !?
gMode = 1;

I am obviously not experienced in this XD. Help would be awesome! Maybe a different system for reading commands?

#include <FastLED.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1); // RX, TX


#define LED_PIN     6
#define COLOR_ORDER GRB
#define CHIPSET     WS2811
#define NUM_LEDS    436
#define FRAMES_PER_SECOND 120
int BRIGHTNESS = 40;
int BRIGHTNESScur = 0;

CRGB leds[NUM_LEDS] = CRGB (0, 0, 0);

void setup() /////////////////////////////////////////////////////////////////////////////////////////////
{
  delay(200);
  Serial.begin(9600);
  BTSerial.begin(9600);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESScur );
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CRGB (0, 0, 0);
  }
  Serial.println("");
  Serial.println("Setup Done!");
} /////////////////////////////////////////////////////////////////////////////////////////////

const int LED_BREAK_01  =  143;
const int LED_BREAK_02  =  297;
String command;
int sectionNum = 0;
int MasterSwitch = 1;
int colors[3][3] = {{255, 150, 0}, {255, 0, 17}, {183, 0, 255}};
int hue = 0;
int subCommand = 0;
int subCommandNum = 0;
int subCommandTemp01;
int gMode = 1;
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { patternSolid, patternSin3 };

void loop() /////////////////////////////////////////////////////////////////////////////////////////////
{
  while (Serial.available()) {     // Reading serial and putting it on command
    delay(20);
    char c = Serial.read();
    if ( (c == " ") || (c == ","))
    {
      break;
    }
    command += c;
  }
  if (command.length() > 0)
  {
    command.toLowerCase();                    // Calling action to the command
    CommandMasterSwitch();
    CommandMode();
    CommandMode2();
    CommandRGB();
    CommandRGB2();
    CommandBright();
    CommandBright2();
    if ( command != "")
    {
      Serial.println(command);                // Print the command if nothing happened
    }
    command = "";
  }
  if (MasterSwitch == 0)                      // Masterswitch brightness
  {
    BRIGHTNESScur = min(max((BRIGHTNESScur - 3), 0), BRIGHTNESS) ;
    FastLED.setBrightness(BRIGHTNESScur);
  }
  else
  {
    BRIGHTNESScur = min(max((BRIGHTNESScur + 3), 0), BRIGHTNESS) ;
    FastLED.setBrightness(BRIGHTNESScur);
  }
  gMode = 1;                                  // I DONT KNOW HOW THIS LINE CHANGES ANYTHING?!?!?!
  gPatterns[gMode]();                         // The Pattern


  FastLED.show();
  FastLED.delay(1000 / FRAMES_PER_SECOND);
} /////////////////////////////////////////////////////////////////////////////////////////////




void CommandMasterSwitch() {
  if (command == "m")                              // even if it sends through M, it doesnt go through O.O
  {
    MasterSwitch = not(MasterSwitch);
    if ( MasterSwitch == 1) {
      Serial.println(F("All On"));
    }
    if ( MasterSwitch == 0) {
      Serial.println(F("All Off"));
    }
    command = "";
  }
}
void CommandBright() {
  if (command == "b")
  {
    subCommand = 2;
    Serial.println(F("Brightness"));
    command = "";
  }
}
void CommandRGB() {
  if (command == "c")
  {
    subCommand = 1;
    subCommandNum = 0;
    subCommandTemp01 = 0;
    Serial.println("");
    command = "";
  }
}
void CommandMode() {
  if (command == "o")
  {
    subCommand = 3;
    Serial.println("");
    command = "";
  }
}
void CommandMode2() {
  if ( subCommand == 3)
  {
    int commandint = atoi(command.c_str());
    commandint = min(max(commandint, 0), 1);
    gMode = commandint;
    subCommand = 0;
    //Serial.print("Mode Updated to ");
    Serial.println(gMode);
    command = "";
  }
}
void CommandBright2() {
  if ( subCommand == 2)
  {
    int commandint = atoi(command.c_str());
    commandint = min(max(commandint, 0), 255);
    BRIGHTNESS = commandint;
    subCommand = 0;
    Serial.print("Brightness Updated to ");
    Serial.println(BRIGHTNESS);
    command = "";
  }
}
void CommandRGB2() {
  if ( subCommand == 1)
  {
    if ( subCommandNum == 0)
    {
      int commandint = atoi(command.c_str());
      subCommandTemp01 = commandint;
      subCommandNum++;
      //Serial.print("Updating Section ");
      //Serial.println(subCommandTemp01);
      Serial.println("Type Three RGB Values (0-255)");
      Serial.print(colors[subCommandTemp01][0]);
      Serial.print(", ");
      Serial.print(colors[subCommandTemp01][1]);
      Serial.print(", ");
      Serial.println(colors[subCommandTemp01][2]);
    }
    else
    {
      int commandint = atoi(command.c_str());
      colors[subCommandTemp01][subCommandNum - 1] = ((commandint - 1) % 255) + 1;
      Serial.print(colors[subCommandTemp01][0]);
      Serial.print(", ");
      Serial.print(colors[subCommandTemp01][1]);
      Serial.print(", ");
      Serial.println(colors[subCommandTemp01][2]);
      subCommandNum++;
      if ( subCommandNum > 3)
      {
        subCommand = 0;
        subCommandNum = 0;
        subCommandTemp01 = 0;
        Serial.println("finish");
      }
    }
    command = "";
  }
}

void patternSin3()
{
  fadeToBlackBy( leds, NUM_LEDS, 15);
  int pos;
  pos = beatsin16( 5, 0, NUM_LEDS - 1 );
  for ( int i = 0; i < 6; i++)
  {
    leds[min(NUM_LEDS, (pos + i))] += CRGB(colors[1][0], colors[1][1], colors[1][2]);
  }
  pos = (NUM_LEDS - beatsin16( 5, 0, NUM_LEDS - 1 ));
  for ( int i = 0; i < 6; i++)
  {
    leds[min(NUM_LEDS, (pos + i))] += CRGB(colors[2][0], colors[2][1], colors[2][2]);
  }
  pos = beatsin16( 2, 0, NUM_LEDS - 1 );
  for ( int i = 0; i < 2; i++)
  {
    leds[min(NUM_LEDS, (pos + i))] = CRGB(colors[0][0], colors[0][1], colors[0][2]);
  }
}

void patternSolid ()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CRGB(colors[sectionNum][0], colors[sectionNum][1], colors[sectionNum][2]);
  }
}

your command strings are single characters. but your code in loop repeatedly reads characters from the serial port as long as they are available and regardless of whether you receive anything you test whether the command string is > 0 before processing.

if there is more than one character, you String comparisons won't work

why not simply check if something available and if so read a single character and process it before reading any more. if there's nothing available, there's no need to process anything

  if (Serial.available()) {
        char c = Serial.read();
        if ( (c == " ") || (c == ","))
        {
            break;
        }

        command = c;
        command.toLowerCase();    // Calling action to the command

        Serial.println(command);  // Print the command if nothing happened

        CommandMasterSwitch();
        CommandMode();
        CommandMode2();
        CommandRGB();
        CommandRGB2();
        CommandBright();
        CommandBright2();
    }

What Arduino are you using? UNO, MEGA, Nano, Mini use pins 0 and 1 for Serial and you are using them for SoftwareSeral so on most Arduinos you can't use those two pins for BOTH.

You have some compile warnings that you might need to look at. You should compare a character with a character (' ' or ','), not a string (" " or ",").

/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino:13:23: warning: array must be initialized with a brace-enclosed initializer [-fpermissive]
 CRGB leds[NUM_LEDS] = CRGB (0, 0, 0);
                       ^~~~~~~~~~~~~~
/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino:50:16: warning: comparison with string literal results in unspecified behavior [-Waddress]
     if ( (c == " ") || (c == ","))
                ^~~
/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino:50:16: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino:50:30: warning: comparison with string literal results in unspecified behavior [-Waddress]
     if ( (c == " ") || (c == ","))
                              ^~~
/Users/john/Documents/Arduino/sketch_aug12a/sketch_aug12a.ino:50:30: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Sketch uses 10044 bytes (31%) of program storage space. Maximum is 32256 bytes.
Global variables use 1863 bytes (90%) of dynamic memory, leaving 185 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

Just…

CRGB leds[NUM_LEDS];

You already have…

  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CRGB (0, 0, 0);
  }