Write one character then stop

I have a master and slave module set up that are communicating over bluetooth. The Master is working as a push button controller to move my bot. The buttons work fine but I plan on have several modes to control the robot. I have a potentiometer setup(because Im out of buttons) that when it reaches a certain value it sends a character to the slave, letting it know it should be in button mode, or any other mode I will have. The problem is that the master constantly writes the character to slave so cant use the switch properly.

See slave and master code in the attachments.

//MASTER MODULE SKETCH
int left = 10;
int forward = 9;
int backward = 8;
int right = 7;
boolean   cWrite = true;

void setup() 
{
  Serial.begin(9600);// put your setup code here, to run once:
  pinMode(left, INPUT);
  digitalWrite(left, HIGH);
  pinMode(forward, INPUT);
  digitalWrite(forward, HIGH);
  pinMode(backward, INPUT);
  digitalWrite(backward, HIGH);
  pinMode(right, INPUT);
  digitalWrite(right, HIGH);

}

void loop() 
{
  //int BTstate = digitalRead(state); // read state of HC-05

  int sensorValue = analogRead(A0);

  if (sensorValue == 0)
  {
    if (cWrite == true);
    {
      Serial.write("C");
      cWrite = false;
    }
    if (cWrite == false)
    {
      cWrite = false;
      int leftOn = digitalRead(left);
      int forwardOn = digitalRead(forward);
      int backwardOn = digitalRead(backward);
      int rightOn = digitalRead(right);
      if (leftOn == 0)
      {
        Serial.write("L");
        Serial.println(leftOn);
        delay(250);
        Serial.flush();
      }
      if (forwardOn == 0)
      {
        Serial.print("F");
        delay(250);
        Serial.flush();
      }
      if (backwardOn == 0)
      {
        Serial.write("B");
        delay(250);
        Serial.flush();
      }
      if (rightOn == 0)
      {
        Serial.print("R");
        delay(250);
        Serial.flush();
      }
      if(rightOn == 1 && forwardOn == 1 && backwardOn == 1 && leftOn == 1)
      {
        Serial.write("S");
        delay(250);
        Serial.flush();
      }
    }

    if (sensorValue == 1023)
    {
      Serial.write("A"); 
    }
    if (sensorValue == 3);
    {

    }

  }

}
//Since we are using servos and ultrasonic sensors in the robot we will include some libraries written to make their use easier
#include <Servo.h>
#include <NewPing.h>

//Below are the symbolic constants. Instead of having to type in a non-sensical pin number each time we want to do something we can write an easy to understand name which represents the pin, the compiler will then replace the names with the numbers
#define LeftMotorForward 6
#define LeftMotorBackward 5
#define RightMotorForward 8
#define RightMotorBackward 9
#define USTrigger 3
#define USEcho 2
#define MaxDistance 100
#define LED 13

//Here we have created two 'objects', one for the servo and one for the ultrasonic sensor
Servo servo;
NewPing sonar(USTrigger, USEcho, MaxDistance);

//Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values
unsigned int duration;
unsigned int distance;
unsigned int distance2;
unsigned int distance3;
unsigned int distance4;
unsigned int FrontDistance;
unsigned int LeftDistance;
unsigned int RightDistance;
unsigned int Time;
int ledblue = 13; //forward
int ledyellow = 12; //left
int ledgreen = 11; //right
int ledred = 10; //backwards
int ledstop = 19; //stop
int ledhead = A5; //headlight
int incomingByte = 1; 
int cycle = 0;
int wait = 2;
boolean autonomous = false;
void setup()                                            //This block happens once on startup
{
  Serial.begin(9600);                              

  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(ledblue, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  pinMode(ledred, OUTPUT);
  pinMode(ledstop, OUTPUT);
  pinMode(A5, OUTPUT);
  servo.attach(4); //The servo is attached to pin 4

}

void loop()                                           //This block repeats itself while the Arduino is turned on
{
  incomingByte = Serial.read();
  switch (incomingByte)
  {
  case 'C':
    buttonControl();
    break;

  case 'A':
    autonomous = true;
    autono();
    break;
  }
}

void buttonControl()
{
char movement =Serial.read();
  switch (movement)
  {
  case 'L':
    moveLeft();
    Serial.println("left");
    break;

  case 'F':
    moveForward();
    Serial.println("Forward");
    break;

  case 'B':
    moveBackward();
    Serial.println("Backward");
    break;

  case 'R':
    moveRight();
    Serial.println("Right");
    break;

  case 'S':
    moveStop();
    Serial.println("stopped");
    break;
  }
}

void autono()
{

}

void moveForward()                                    //This function tells the robot to go forward 
{
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(ledblue, HIGH);
  delay(250);
  digitalWrite(ledblue, LOW);

}

void moveBackward()                                  //This function tells the robot to move backward
{
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(ledred, HIGH);
  delay(250);
  digitalWrite(ledred, LOW);
}

void moveLeft()                                      //This function tells the robot to turn left
{
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(ledyellow, HIGH);
  delay(500);
  digitalWrite(ledyellow, LOW); 
}

void moveRight()                                    //This function tells the robot to turn right
{
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(ledgreen, HIGH);
  delay(500);
  digitalWrite(ledgreen, LOW);  

}

void moveStop()                                     //This function tells the robot to stop moving
{
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(ledstop, HIGH);
}
void scan()                                         //This function determines the distance things are away from the ultrasonic sensor
{
  delay(50); 
  Time = sonar.ping(); 
  distance = Time / US_ROUNDTRIP_CM;
}
void lights()
{
  int lightsensor = analogRead (A0);

  if (lightsensor < 80)
  {
    digitalWrite(ledhead, HIGH);
  }
  else
  {
    digitalWrite(ledhead, LOW);
  }
  delay(100);
}

Moderator edit: Placed code in code tags, where it should have been.

slave.txt (4.77 KB)

master.txt (1.69 KB)

Anybody got an idea? I'm quickly running out of them

Look carefully at how your code is structured. A great exercise is to go through the code step by step with a pencil and paper pretending your brain is the Arduino.

I think you will find your IF statements are not properly nested. Everything seems to be inside the first IF.

For the future, post your code as a .ino file rather than a .txt file. Also there are a lot of blank lines in your code which makes it difficult to follow.

...R