Logic Control Loop

I have the following program in My Arduino Mega 2560 ADK

#include<Servo.h>

String inputString = "";
int recievedVal;
boolean toggleComplete = false;  // whether the string is complete
boolean toggleComplete1 = false;
boolean toggleComplete2 = false;
boolean toggleComplete3 = false;
boolean pwmComplete = false;  // for servo
boolean toggleComplete4 = false;

// Debug pins LED
const int ledPin = 13;
const int ledPin2 = 3;

// Servo Definition & Pin
// Servo definition
Servo webcam;

// Temperature Sensor

int analogPin = 0; // Temp sensor at AnalogPin 0
int readValue = 0;
float TemperatureC = 0;
float TemperatureF = 0;

// Ping vars
const int pingPin = 7;
int sensorValue = 0;        // value read from the sensor
int sensorValue1 = 0;        // value read from the sensor
int prevValue = 0;          // previous value from the sensor
long duration;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
  webcam.attach(5); // the servo is attached to pin 5 of the arduino
  webcam.write(90);
}

void loop() {
  
  // control LED via commands read from serial  
  when (Serial.available() && toggleComplete4 == false && toggleComplete == false && toggleComplete1 == false && toggleComplete2 == false && toggleComplete3 == false && pwmComplete == false) {
       char inChar = (char)Serial.read();
    if(inChar == 'F'){ // end character for led FORWARD
     toggleComplete = true;
     //Serial.print(inChar);
    }
    if(inChar == 'S'){   // STOP
      toggleComplete1 = true;
     // Serial.print(inChar);
    }
    if(inChar == 'L'){ //LEFT
      toggleComplete2 = true;
   //Serial.print(inChar);
    }
    if(inChar == 'R'){ //RIGHT
      toggleComplete3 = true;
   //Serial.print(inChar);
    }
      if(inChar == 'B'){ //BACK
      toggleComplete4 = true;
   //Serial.print(inChar);
    }
    if(inChar == 'P'){ // SERVO
      pwmComplete = true;
      int recievedVal = stringToInt();
  
    int ser = map(recievedVal,10,180,0,180);
    webcam.write(ser);
    //Serial.print(recievedVal);
    delay(15);
    pwmComplete = false;
    }
    else{
      inputString += inChar;
   }
}
// FORWARD
 if(!Serial.available() && toggleComplete == true)
{
  int recievedVal = stringToInt();
  if(recievedVal == 1)
  {
    digitalWrite(ledPin,HIGH);
   //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin,recievedVal);
    //  Serial.print(recievedVal);
    }
   toggleComplete = false;
}

// STOP
 if(!Serial.available() && toggleComplete1 == true)
{
  int recievedVal = stringToInt();
  if(recievedVal == 0)
  {
    digitalWrite(ledPin,LOW);
    digitalWrite(ledPin2,LOW);
    //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin,LOW);
      //Serial.print(recievedVal);
    }
   toggleComplete1 = false;
}
// LEFT
 if(!Serial.available() && toggleComplete2 == true)
{
  int recievedVal = stringToInt();
  if(recievedVal == 2)
  {
    digitalWrite(ledPin2,HIGH);
    //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin2,LOW);
      //Serial.print(recievedVal);
    }
   toggleComplete2 = false;
}
// RIGHT
if(!Serial.available() && toggleComplete3 == true)
{
  int recievedVal = stringToInt();
  if(recievedVal == 3)
  {
    digitalWrite(ledPin,HIGH);
    //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin,LOW);
     // Serial.print(recievedVal);
    }
   toggleComplete3 = false;
}
//BACK
if(!Serial.available() && toggleComplete4 == true)
{
  int recievedVal = stringToInt();
  if(recievedVal == 4)
  {
    digitalWrite(ledPin,HIGH);
   //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin,LOW);
     // Serial.print(recievedVal);
    }
   toggleComplete4 = false;
}
  //Ping distance
   pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
   sensorValue1 = microsecondsToCentimeters(duration);
     sensorValue = microsecondsToInches(duration);
    int x = sensorValue;
    int y = sensorValue1;
    int z = (x/y);
    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');
     Serial.print(z);
     Serial.print('\t');
     Serial.println(); 
  
}
int stringToInt()
{
    char charHolder[inputString.length()+1];
    inputString.toCharArray(charHolder,inputString.length()+1);
    inputString = "";
    int _recievedVal = atoi(charHolder);
    return _recievedVal;
}

// Distance Calculation
 long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

This program is giving the digital output readings in Serial.print for values of x , y and z in the serial monitor. But the led’s donot blink if I enter any combinations from the serial monitor. The servo even do not work.
But surprisingly when I remove the ping sensor code from ( ie // convert the time into a distance to the end) , i can enter from the serial monitor and the LED’s function. Why is this so?

One more surprising fact is happening. I have to enter all the variables twice to register , ie I have to enter F1F1 or F1 (ENTER) F1 (ENTER) to turn on led13. Similarly with other combinations. I think , a single F1 combination should have worked , but it is not working. This is obviously without any ping codes.

Please help me . I have tried for 2 complete days but couldnot debug the fault.

You should start by tidying up the indenting of your code. Personally I would line up the braces vertically.

The problem might become obvious when the program structure is clearer.

I agree with radman, and as a start, I'd change the "when" loop to:

  while (Serial.available() && toggleComplete4 == false && toggleComplete == false && toggleComplete1 == false && toggleComplete2 == false && toggleComplete3 == false && pwmComplete == false) {

    char inChar = toupper((char)Serial.read());   // Why force them to enter uppercase letters??

     switch (inChar) 
     {
       case 'F':
         toggleComplete = true;
         break;
       case 'S':
         toggleComplete1 = true;
         break;
       case 'L':
         toggleComplete2 = true;
         break;
       case 'R':
         toggleComplete3 = true;
         break;
       case 'B':
         toggleComplete4 = true;
         break;
       case 'P':
          pwmComplete = true;
          int recievedVal = stringToInt();
          int ser = map(recievedVal,10,180,0,180);
          webcam.write(ser);
          //Serial.print(recievedVal);
          delay(15);
          pwmComplete = false;
          break
        default:
          inputString += inChar;
          break;        
     }
     Serial.print("inChar = ");
     Serial.println(inChar);
  }

The call to toupper() prevents the user from having to use uppercase letters. The switch code prevents you from making up to six unnecessary if tests. Next, since the switch tells you the state of your boolean variable, why not move the code into the switch, or make the code a function call? For example, your code:

   // FORWARD
  if(!Serial.available() && toggleComplete == true)
  {
    int recievedVal = stringToInt();
    if(recievedVal == 1)
    {
      digitalWrite(ledPin,HIGH);
     //Serial.print(recievedVal);
    } else if(recievedVal == 0)
      {
        digitalWrite(ledPin,recievedVal);
        //  Serial.print(recievedVal);
      }
     toggleComplete = false;
  }

could be move into the case 'F' case. This also prevents up to six unnecessary if test.

If don't understand this code:

 if(recievedVal == 0)
  {
    digitalWrite(ledPin,LOW);
    digitalWrite(ledPin2,LOW);
    //Serial.print(recievedVal);
  }
  else if(recievedVal == 0)
    {
      digitalWrite(ledPin,LOW);
      //Serial.print(recievedVal);
    }
   toggleComplete1 = false;

The first if test tells you if recievedVal is 0, so what's the purpose of the second if after the else clause?

In your code:

  return microseconds / 74 / 2;

why not add a little more info to the comment and just write:

  return microseconds / 37;

I stopped on seeing a "when" command. My compiler doesn't know what that is either.

Read up on if-else as opposed to if if if if if when they are all mutually exclusive

How is it that so many people who don't know so many basics write so much code before checking so much of it?

Thanks everybody for all the suggestions.
My modified code according to your suggestions are

#include<Servo.h>

String inputString = "";
int recievedVal;
boolean toggleComplete = false;  // whether the string is complete
boolean toggleComplete1 = false;
boolean toggleComplete2 = false;
boolean toggleComplete3 = false;
boolean pwmComplete = false;  // for servo
boolean toggleComplete4 = false;

// Debug pins LED
const int ledPin = 13;
const int ledPin2 = 3;

// Servo Definition & Pin
// Servo definition
Servo webcam;

// Temperature Sensor

int analogPin = 0; // Temp sensor at AnalogPin 0
int readValue = 0;
float TemperatureC = 0;
float TemperatureF = 0;

// Ping vars
const int pingPin = 7;
int sensorValue = 0;        // value read from the sensor
int sensorValue1 = 0;        // value read from the sensor
int prevValue = 0;          // previous value from the sensor
long duration;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
  webcam.attach(5); // the servo is attached to pin 5 of the arduino
  webcam.write(90);
}

void loop() {
  
  // control LED via commands read from serial  
   while(Serial.available() && toggleComplete4 == false && toggleComplete == false && toggleComplete1 == false && toggleComplete2 == false && toggleComplete3 == false && pwmComplete == false) {
       char inChar = toupper((char)Serial.read());
    switch (inChar)
    {
    case 'F':
        {toggleComplete = true;
        //Serial.print(inChar);
        recievedVal = stringToInt();
        if(recievedVal == 1)
            {
                digitalWrite(ledPin,HIGH);
            }
         else if(recievedVal == 0)
            {
              digitalWrite(ledPin,LOW);
              
            }    
        toggleComplete = false;
        break;
        }
     case 'S':
        {toggleComplete1 = true;
         recievedVal = stringToInt();
          if(recievedVal == 0)
            {
                digitalWrite(ledPin,LOW);
                digitalWrite(ledPin2,LOW);
              }
               
        toggleComplete1 = false;
        break;
        }
           case 'L':
        {toggleComplete2 = true;
         recievedVal = stringToInt();
            if(recievedVal == 2)
                {
                digitalWrite(ledPin2,HIGH);
                
                }
             else if(recievedVal == 0)
                {
                  digitalWrite(ledPin2,LOW);
              stop1();
              }    
          toggleComplete2 = false;
          break;
        }
      case 'R':
         {toggleComplete3 = true;
         recievedVal = stringToInt();
          if(recievedVal == 3)
            {
                digitalWrite(ledPin,HIGH);
                
            }
         else if(recievedVal == 0)
            {
              digitalWrite(ledPin,LOW);
              stop1();
            }    
        toggleComplete3 = false;
        break;
        }
     case 'B':
         {toggleComplete4 = true;
           recievedVal = stringToInt();
          if(recievedVal == 4)
            {
                digitalWrite(ledPin2,HIGH);
             }
           else if(recievedVal == 0)
            {
              digitalWrite(ledPin2,LOW);
              stop1();
            }    
        toggleComplete4 = false;
        break;
        }
      case 'P':
         { pwmComplete = true;
          int recievedVal = stringToInt();
          int ser = map(recievedVal,10,180,0,180);
          webcam.write(ser);
          //Serial.print(recievedVal);
           delay(15);
           pwmComplete = false;
           break;
         }
        default :
          inputString += inChar;
          break;
    }
}
  
  //Ping distance
   pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
   sensorValue1 = microsecondsToCentimeters(duration);
     sensorValue = microsecondsToInches(duration);
    int x = sensorValue;
    int y = sensorValue1;
    int z = (x/y);
    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');
    Serial.print(z);
    Serial.print('\t');
    Serial.println();
     
}

int stringToInt()
{
    char charHolder[inputString.length()+1];
    inputString.toCharArray(charHolder,inputString.length()+1);
    inputString = "";
    int _recievedVal = atoi(charHolder);
    return _recievedVal;
}

// Distance Calculation
 long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Now I can write to the serial monitor even when the serial monitor is printing read data from the sensors.
But one problem remains
I have to input F1F1 to turn on ledpin (lED 13) HIGH . or S0S0 to close all led. Why only F1 or S0 not working?

Secondly there is a slight noticeable delay on led on /off time . Why isn’t F1F1 not executed immediately? Is it because that there is Serial.print still in the buffer?
Thanks
again

Serial.read() is designed to read only one byte at a time, If you type in F1, the '1' character is still in the buffer, but you don't read it. Replace Serial.read() with the following and read the input into buffer:

void loop() {
  char buffer[10];

  // control LED via commands read from serial  
  while(Serial.available() && toggleComplete4 == false && toggleComplete == false && toggleComplete1 == false && toggleComplete2 == false && toggleComplete3 == false && pwmComplete == false) {
    Serial.readBytesUntil('\r', buffer, 9);
    char inChar = toupper(buffer[0]);
    switch (inChar)
    {
    case 'F':
      {
        toggleComplete = true;
        //Serial.print(inChar);
        //recievedVal = stringToInt();
        // if(recievedVal == 1)
        if (buffer[1] == '1')
        {
          digitalWrite(ledPin,HIGH);
        }
        else if(recievedVal == 0)
        {
          digitalWrite(ledPin,LOW);

        }    
        toggleComplete = false;
        break;
      }
    case 'S':

Similar changes need to be made for the 'S' case, too. Also, you should not post code that won't compile unless that the problem itself. Your code had a bunch of "stops" in it and readers shouldn't have to take the time to remove these.

Thanks @econjack for the valuable suggestion. Your code has done the trick. But what did you mean't by "Your code had a bunch of "stops" in it " ??? Every body helped me with the code. Thanks

But still the problem of delay is there. The led's are taking 1 to 2 secs( i think it is a delay) to display status after the command is sent. Can anyone please throw some help in this matter?

I don't see why in the code fragment you show....

When I copied your code to my IDE, several stop() method calls were present. I reloaded the code and the "when" error was still there, but the stop()'s are gone. Don't know what happened?

subhendusc: Thanks @econjack for the valuable suggestion. Your code has done the trick. But what did you mean't by "Your code had a bunch of "stops" in it " ??? Every body helped me with the code. Thanks

But still the problem of delay is there. The led's are taking 1 to 2 secs( i think it is a delay) to display status after the command is sent. Can anyone please throw some help in this matter?

I was looking at the piece that Jack had posted. I just copied your updated code into IDE 1.03 and there are several calls to stop1() that doesn't exist except in calls.

It won't compile so how can you run it?

Where is the code that you are using to get the problems you describe?