What is the issue with my code?

So it is a simple project where arduino judges the distance using ultrasonic sensor. Based on the distance, there should be a movement of servo and the change of light colour, and the message must be displayed on LCD(16*2). However that isn’t happening. I have tested the hardware individually and can see that it is working fine. The only other explanation is my code. Could someone help me out with it and see where i am going wrong?

#include <Servo.h>      //include servo library
#include <LiquidCrystal.h>    //include LCD library

int pos = 0;        //Variable to store the servo position
int servoPin = 9;   //Servo is hooked to pin 9
int servoDelay = 25;    //25 milliseconds delay after each servo write
Servo mainMotor;      //Assigning a name to the servo motor.
float servoAngle;     //Angle we would want the servo to move

LiquidCrystal LCD(10, 9, 5, 4, 3, 2);    //Define RS, E, DB4, DB5, DB6 and DB7

int trigPin = 13;      //Pin that is responsible for sending out the wave (Digital)
int echoPin = 11;      //Pin that is responsible for recieveing the wave (Analog)
float pingTime;       // Distance taken by the sound wave to hit the target and come back
float targetDistance;   //Distance of the target from the sensor
int soundSpeed = 334;   //Speed of sound in m/s

int redPin = 13;    //Assigning the red pin to pin 13 of arduino
int greenPin = 12;   //Assigning the green pin to the pin 12 of arduino 
int bluePin = 7;    //Assigning the blue pin to the pin 7 of arduino
int rBrightness;    //analog value for the brightness of red LED  
int gBrightness;    //analog value for the brightness of green LED
int bBrightness;    //analog value for the brightness of blue LED

void setup() {
  
    mainMotor.attach(servoPin);     //Telling the arduino to fix the servo mainMotor with the pin assigned as sevoPin.
    pinMode(servoPin, OUTPUT);      //Declaring that the servoPin is an output
  
    LCD.begin(16,2);    //Defining the size of the LCD. In this case, 16 columns and 2 rows.
    LCD.setCursor(0,0); //Placing the cursor on the first column of the first row.
    
    pinMode(trigPin, OUTPUT);    // Telling arduino trigPin is an output
    pinMode(echoPin, INPUT);     //Telling arduino that echoPin is an input
    
    pinMode(redPin, OUTPUT);      //Telling arduino redPin is an output
    pinMode(bluePin, OUTPUT);     //Telling arduino bluePin is an output
    pinMode(greenPin, OUTPUT);    //Telling arduino greenPin is an output
    
    
    
    LCD.print("Object is- ");
    delay(1000);
    
    
}

void loop() {
  
  digitalWrite(trigPin, LOW);   //a pulse is set to low
  delayMicroseconds(2000);      //wait
  
  digitalWrite(trigPin, HIGH);  //the pulse is generated from the trigPin
  delayMicroseconds(15);        //wait
  
  digitalWrite(trigPin, LOW);   //the pulse wave is switched off again
  delayMicroseconds(10);        //wait
  
  pingTime = pulseIn(echoPin, HIGH);      //time taken is equal to the time taken by echoPin to recieve the pulse signal sent by trigPin.
  pingTime = pingTime/1000000;            //conversion from microseconds to milliseconds  
  targetDistance = soundSpeed*pingTime;   //calculating distance
  targetDistance = targetDistance/200;
 
  while (targetDistance>35) {            //if the distance is more than 35cm
    LCD.setCursor(12,0);                  //setting the cursor location
    LCD.print("far");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);                          //wait
    
    gBrightness = 8*targetDistance-230;   //by plotting graph where Y axis is LED brightness and X axis is targetDistance.
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, gBrightness);
    analogWrite(bluePin, LOW);
    
    servoAngle = 0;
  }
  
  
   while (35>=targetDistance>15) {
    LCD.setCursor(0,0);
    LCD.print("    ");
    LCD.setCursor(0,0);
    LCD.print("arnd");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);
    
    bBrightness = 10*targetDistance-100;
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    analogWrite(bluePin, bBrightness);
    
    servoAngle = 0;
  }
  
  
   while (targetDistance<=15) {
    LCD.setCursor(12,0);
    LCD.print("    ");
    LCD.setCursor(12,0);
    LCD.print("arnd");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);
    
    rBrightness = (13.3*targetDistance) + 50;
    digitalWrite(redPin, rBrightness);
    digitalWrite(greenPin, LOW);
    analogWrite(bluePin, LOW);
    
    servoAngle = 12*targetDistance - 10;    //Relation between movement of servo and target distance.
  }
  
  
  
  
  
    
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

So it is a simple project where arduino judges the distance using ultrasonic sensor. Based on the distance, there should be a movement of servo and the change of light colour, and the message must be displayed on LCD(16*2). However that isn’t happening. I have tested the hardware individually and can see that it is working fine. The only other explanation is my code. Could someone help me out with it and see where i am going wrong?

#include <Servo.h>      //include servo library
#include <LiquidCrystal.h>    //include LCD library

int pos = 0;        //Variable to store the servo position
int servoPin = 9;   //Servo is hooked to pin 9
int servoDelay = 25;    //25 milliseconds delay after each servo write
Servo mainMotor;      //Assigning a name to the servo motor.
float servoAngle;     //Angle we would want the servo to move

LiquidCrystal LCD(10, 9, 5, 4, 3, 2);    //Define RS, E, DB4, DB5, DB6 and DB7

int trigPin = 13;      //Pin that is responsible for sending out the wave (Digital)
int echoPin = 11;      //Pin that is responsible for recieveing the wave (Analog)
float pingTime;       // Distance taken by the sound wave to hit the target and come back
float targetDistance;   //Distance of the target from the sensor
int soundSpeed = 334;   //Speed of sound in m/s

int redPin = 13;    //Assigning the red pin to pin 13 of arduino
int greenPin = 12;   //Assigning the green pin to the pin 12 of arduino 
int bluePin = 7;    //Assigning the blue pin to the pin 7 of arduino
int rBrightness;    //analog value for the brightness of red LED  
int gBrightness;    //analog value for the brightness of green LED
int bBrightness;    //analog value for the brightness of blue LED

void setup() {
  
    mainMotor.attach(servoPin);     //Telling the arduino to fix the servo mainMotor with the pin assigned as sevoPin.
    pinMode(servoPin, OUTPUT);      //Declaring that the servoPin is an output
  
    LCD.begin(16,2);    //Defining the size of the LCD. In this case, 16 columns and 2 rows.
    LCD.setCursor(0,0); //Placing the cursor on the first column of the first row.
    
    pinMode(trigPin, OUTPUT);    // Telling arduino trigPin is an output
    pinMode(echoPin, INPUT);     //Telling arduino that echoPin is an input
    
    pinMode(redPin, OUTPUT);      //Telling arduino redPin is an output
    pinMode(bluePin, OUTPUT);     //Telling arduino bluePin is an output
    pinMode(greenPin, OUTPUT);    //Telling arduino greenPin is an output
    
    
    
    LCD.print("Object is- ");
    delay(1000);
    
    
}

void loop() {
  
  digitalWrite(trigPin, LOW);   //a pulse is set to low
  delayMicroseconds(2000);      //wait
  
  digitalWrite(trigPin, HIGH);  //the pulse is generated from the trigPin
  delayMicroseconds(15);        //wait
  
  digitalWrite(trigPin, LOW);   //the pulse wave is switched off again
  delayMicroseconds(10);        //wait
  
  pingTime = pulseIn(echoPin, HIGH);      //time taken is equal to the time taken by echoPin to recieve the pulse signal sent by trigPin.
  pingTime = pingTime/1000000;            //conversion from microseconds to milliseconds  
  targetDistance = soundSpeed*pingTime;   //calculating distance
  targetDistance = targetDistance/200;
 
  while (targetDistance>35) {            //if the distance is more than 35cm
    LCD.setCursor(12,0);                  //setting the cursor location
    LCD.print("far");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);                          //wait
    
    gBrightness = 8*targetDistance-230;   //by plotting graph where Y axis is LED brightness and X axis is targetDistance.
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, gBrightness);
    analogWrite(bluePin, LOW);
    
    servoAngle = 0;
  }
  
  
   while (35>=targetDistance>15) {
    LCD.setCursor(0,0);
    LCD.print("    ");
    LCD.setCursor(0,0);
    LCD.print("arnd");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);
    
    bBrightness = 10*targetDistance-100;
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    analogWrite(bluePin, bBrightness);
    
    servoAngle = 0;
  }
  
  
   while (targetDistance<=15) {
    LCD.setCursor(12,0);
    LCD.print("    ");
    LCD.setCursor(12,0);
    LCD.print("arnd");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);
    
    rBrightness = (13.3*targetDistance) + 50;
    digitalWrite(redPin, rBrightness);
    digitalWrite(greenPin, LOW);
    analogWrite(bluePin, LOW);
    
    servoAngle = 12*targetDistance - 10;    //Relation between movement of servo and target distance.
  }
  
  
  
  
  
    
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

while (targetDistance>35) {

Oops.

while (35>=targetDistance>15) {

Double oops.

while (targetDistance>35) {            //if the distance is more than 35cm
    LCD.setCursor(12,0);                  //setting the cursor location
    LCD.print("far");
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(" ");
    LCD.print("cm");
    delay(1000);                          //wait
    
    gBrightness = 8*targetDistance-230;   //by plotting graph where Y axis is LED brightness and X axis is targetDistance.
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, gBrightness);
    analogWrite(bluePin, LOW);
    
    servoAngle = 0;
  }

This will be an infinite loop. Since the value of targetDistance doesn't change inside this while loop, the processor will just sit there constantly repeating this small section over and over forever.

while (35>=targetDistance>15) {

This is also an error. This isn't how you check for a number being between two numbers. You don't get to make up your own syntax in C++. This will check is 35 is greater or equal and then compare that result to 15. Since the result of the first comparison will be false (0) or true (1) either of which is less than 15 this will always be true. You need two separate comparisons and an AND.

while (35>=targetDistance && targetDistance > 15) {

However this too is an infinite loop since targetDistance doesn't change inside the while loop.

So basically this code right now takes one reading and then gets stuck in one of those while loops and never takes another reading. Remember that the loop function is already repeating. There's no need for those while loops. Just let the loop function do the looping and make those into if statements.

@rd0640, please do not cross-post. Threads merged.

@AWOL, glad to know you're still here!

I am not AWOL, I am an unpronounceable symbol.

TheMemberFormerlyKnownAsAWOL:
I am not AWOL, I am an unpronounceable symbol.

Not with that avatar...

Yes off course. i shall not cross post again. My apologies.

And i can see how i set up the infinite while loop. My idea was that "targetDistance" would judge the value of the object in front of the sensor and that value would enable getting out of the loop. Clearly i was wrong.

Now, it is suggested that i could use AND function, but that would be an infinite loop as well.

Could you please be so kind and help me make my code working and effective?
Its a child like error, i understand. I am only beginning my journey in programming.

Try if instead of while.

See how it goes.

Experiment. Have fun

rd0640:
Could you please be so kind and help me make my code working and effective?
Its a child like error, i understand. I am only beginning my journey in programming.

I did. You seem to have not read my post all the way through. Please pay attention.

Delta_G:
So basically this code right now takes one reading and then gets stuck in one of those while loops and never takes another reading. Remember that the loop function is already repeating. There's no need for those while loops. Just let the loop function do the looping and make those into if statements.

Delta_G:
Not with that avatar...

And the "Oops"

meltDown:
And the “Oops”

Yeah, you can tell a lot by how someone uses language, and what language they use.

TheMemberFormerlyKnownAsAWOL:
Yeah, you can tell a lot by how someone uses language, and what language they use.

You should know that from busting shadow accounts. Or you would if you actually were the shadow of AWOL. Of course AWOL would never actually open a shadow account.

So for now we shall call you "the member allegedly formerly known as AWOL"

…happily now with the weight of busting accounts off his shoulders.

int servoPin = 9;   //Servo is hooked to pin 9
.....
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);    //Define RS, E, DB4, DB5, DB6 and DB7

the servo pin is 9 and the LCD E pin is ?

I am not AWOL, I am an unpronounceable symbol.

are you stuck in some record-deal as well and desire creative freedom ?