Command wont leave loop

So i am trying to create a rgb led code set that can be used with the lightblue app from ios

my code is able to get the correct commands i send in however when i send in the command for random colors it gets stuck in the loop when i send in another command.
for example if i send in r it makes the light red, b = blue and so on. those work fine but when i send in a = all color it gets stuck looping through the colors even after i send in another command.

#include <SoftwareSerial.h>
/*int led = 2; */

int redPin = 3; //whichever pin is connected to the red node of the rgb LED

int greenPin = 5; //whichever pin is connected to the green node of the rgb LED

int bluePin = 6; //whichever pin is connected to the blue node of the rgb LED

SoftwareSerial Bluetooth(0, 1);

int x;
void setup() {
  Serial.begin(9600);
  Bluetooth.begin(9600);
  pinMode(redPin, OUTPUT);

}

void loop() {
  char command;
  int randomC = 0;
  if (Bluetooth.available()) {
    command = Bluetooth.read();
    Serial.println("Input received:");

    if (command != 'a') {
      x = 0;
      getCommand(command);
      Serial.println("Not A: " + x);
    }
    if (command == 'a') {
      x = 1;
      randomColors();
      Serial.println(" A: " + x);

    }
    else {
      x = 0;
      getCommand(command);


    }


  }

}

void getCommand(char command) {
  pinMode(4, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  switch (command) {
    case 'r': //RED
      digitalWrite(4, HIGH);
      setColor(255, 0, 0);

      break;
    case 'g': //GREEN
      digitalWrite(4, HIGH);
      setColor(0, 255, 0);

      break;
    case 'b': //BLUE
      digitalWrite(4, HIGH);
      setColor(0, 0, 255);
      break;
    case 'y':
      digitalWrite(4, HIGH);
      setColor(255, 255, 51);
      break;
    case 'o':
      digitalWrite(4, HIGH);
      setColor(255, 165, 0);
      break;
    case 'v':
      digitalWrite(4, HIGH);
      setColor(238, 130, 238);
      break;
    case 'm':
      digitalWrite(4, HIGH);
      setColor(255, 0, 255);
      break;
    case 'c':
      digitalWrite(4, HIGH);
      setColor(0, 255, 255);
      break;
    case 'e':
      digitalWrite(4, HIGH);
      setColor(128, 128, 128);
      break;




    case 'x': //TURNS OFF LIGHT
      digitalWrite(4, LOW);
      pinMode(redPin, LOW);
      pinMode(greenPin, LOW);
      pinMode(bluePin, LOW);
      break;


  }






}

void randomColors() {
  digitalWrite(4, HIGH);
  do {
    int r1 = random(255);
    int r2 = random(255);
    int r3 = random(255);
    setColor(r1, r2, r3);
  }
  while (x == 1);

}


void setColor(int red, int green, int blue) {

  analogWrite(redPin, red);

  analogWrite(greenPin, green);

  analogWrite(bluePin, blue);
  delay(500);

}

Read your randomcolors() function over and over to yourself until you see the problem.

Paul

Paul_KD7HB:
Read your randomcolors() function over and over to yourself until you see the problem.

Paul

problem problems...

aarg:
problem problems...

Sure! But one at a time.
Paul

so in the below code shouldnt the command change set the x value? if not 'a' it should set it to 0 if a set it to 1
and then in the loop i wrote shouldnt that continue until x is set to 0?

if (command != 'a') {
      x = 0;
      getCommand(command);
      Serial.println("Not A: " + x);
    }


if (command == 'a') {
      x = 1;
      randomColors();
      Serial.println(" A: " + x);

    }

void randomColors() {
  digitalWrite(4, HIGH);
  do {  //continue  while x=1
    int r1 = random(255);
    int r2 = random(255);
    int r3 = random(255);
    setColor(r1, r2, r3);
  }
  while (x == 1);

}

darenr:
and then in the loop i wrote shouldnt that continue until x is set to 0?

Yes, except that the code that sets it to zero is outside the loop and will never be executed.

Try:

  while (Bluetooth.available() == 0);

in place of:

  while (x == 1);

That will end your random color loop when a new character arrives from Bluetooth.

thank you, it worked perfectly