Q:

Recieved String to useable variable in Arduino

I'm trying to use data sent via Bluetooth from a mobile app to the Arduino. There will be 2 strings received dependent on the function required.
First function: When the string "ledon" or "ledoff" received turn on or turn off an led.
Second Function: from the app I get the data a user has typed into 2 boxes, one a text box the other a password box, this is received by the Arduino as one string with comma's as a delimiter so the string can be separated into it's component parts, which in this case are an "ssid name" and a "network key". These details I will be intending to write to an onboard sd card so that the wifi module will be able to logon to the network that the ssid and network key are relevant too.
So far I can get the led light to do as it should, I can also get the string read, separated by the delimiter and printed out to the serial monitor. However when I try to combine the two codes the led function fails to turn on or off the led light although the correct command is printed into the serial monitor. I have researched how to solve this trying each requirement as a standalone function and calling them in the loop section, which individually they work but when called together again the led fails to come on.
This is the code i'm presently using:
const int ledPin = 11;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode (ledPin, OUTPUT);
}

void loop() {   
  while (Serial.available()) {
    delay(3);
    char c = Serial.read();
    readString  += c;
  }

//turns led on or off

   if (readString.length() >0) {

      Serial.println(readString); 

   if (readString == "ledon")
    {
      digitalWrite (ledPin, HIGH);
    }
    if (readString == "ledoff")
    {
      digitalWrite (ledPin, LOW);
    }
    readString=""; 

//seperates the string into 2 lines using the delimiter ","

    String first  = Serial.readStringUntil(',');
    Serial.read(); //next character is comma, so skip it using this
    String second = Serial.readStringUntil(',');
    Serial.read();

    Serial.println(first);
    Serial.println(second); 
    readString="";
   }

    }

If I comment out the string part of the code the led turns on and off fine, if I try and run the code as is the code prints everything you would expect but the led doesn't come on.
I'm pretty new to Arduino,c and c++ and I cannot seem to figure this out so any help would be great.
The app I'm using to control this is being built in Mit's App Inventor 2

A:

I have finally resolved this.
I decided to look at other logical steps to find a solution.
I considered the issue could be having different strings that did different things which may be conflicting in some way.
In my research I found reference to the fact that the Bluetooth module HC-05 has 2 pins that in my case were not being utilised, the key pin and the state pin, in this stack overflow discussion Tell when Bluetooth module connects on Arduino and decided this logic of using the "State pin" to signify Bluetooth connection would negate the possibility of any conflicts within the String logic.
Once I had figured out the best way to place and order the "where" statements I managed to get the functionality at this point in my project which then allows me to move on to the next step in the development.
The code I have now arrived at:
const int ledPin1 = 11;
const int ledPin2 = 4;

String readString;

boolean BTconnected = false;

void setup() {
  Serial.begin(9600);
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, INPUT);
}

void loop() {  

      while (!BTconnected)
    {
      if ( digitalRead(ledPin2)==HIGH)  { BTconnected = true;};
    }
    digitalWrite(ledPin1, HIGH);
    Serial.println("HC-05 is now connected");
    Serial.println("");

       while (BTconnected)
              {
        if ( digitalRead(ledPin2)==LOW)  { BTconnected = false;
        digitalWrite(ledPin1, LOW);
        Serial.println("HC-05 is now disconnected");
        Serial.println("");
                };

        while (Serial.available()) 
              {
            delay(3);
            char c = Serial.read();
            readString  += c;
              }

   if (readString.length() >0) {

      //Serial.println(readString);
      //readString="";

    String first  = Serial.readStringUntil(',');
    Serial.read(); //next character is comma, so skip it using this
    Serial.println(first);
    //readString="";

    String second = Serial.readStringUntil(',');
    Serial.read();
    Serial.println(second);
    Serial.println(""); 
    //readString="";
    }
  }
 }

