While Loop Usage

Hi all I'm trying to get an LED flashing while my 2WD robot is in forward motion and does not detect any obstacles closer than 8cm.

I'm definitely using the WHILE loop incorrectly, but can't seem to fix it. help appreciated. thanks

/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8
 */

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 1000; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int led = 4; // LED while fwd direction maintained

void setup() {
  // put your setup code here, to run once:

Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT); /* LED pin 4*/
  pinMode(12, OUTPUT); /* Motor Channel A RIGHT SIDE*/
  pinMode(9, OUTPUT); /* Motor Channel A */
  pinMode(13, OUTPUT); /* Motor Channel B LEFT SIDE */
  pinMode(8, OUTPUT);

  }

void loop() {


  /* Distance check begins */

  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 /* End distance check */



 if (distance >= 8){
 /* Distance great enough to provide room to move */
  Serial.println("Move!");

  //digitalWrite(LEDPin, HIGH); /* LED show movement */

  digitalWrite(12, LOW); /*move channel A motor half-speed */
  digitalWrite(9, LOW);
  analogWrite(3, 123);

  digitalWrite(13, HIGH); /*move channel B motor half-speed */
  digitalWrite(8, LOW);
  analogWrite(11, 123); 

  delay(1000);

  digitalWrite(9, HIGH); 
  digitalWrite(8, HIGH);

  while( distance >= 8 )
    digitalWrite(4, HIGH);
    delay(500);
    digitalWrite(4, LOW);
    delay(500);
 }

 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 //digitalWrite(LEDPin, LOW);

/* Turn Channel A wheel fwd and Channel B wheel backward */

  digitalWrite(12, HIGH); /*move channel A motor half-speed */
  digitalWrite(9, LOW);
  analogWrite(3, 123);

  digitalWrite(13, LOW); /*move channel B motor half-speed */
  digitalWrite(8, LOW);
  analogWrite(11, 123);

  delay(30);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
 }

 //Delay 50ms before next reading.
 delay(500); 

}
while( distance >= 8 )
    digitalWrite(4, HIGH);

Without braces only this first line is part of the while loop. A while loop says, keep repeating this section of code as long as this condition is true. It won't run any other code, only what is inside the while loop. The problem here is that if distance is >= 8 in order to get into this loop and nothing inside the while loop ever changes distance, then it will always and forever be >= 8 and this becomes an infinite loop.

For what you are trying to do, remember that the loop function is repeating over and over. You can use that to your advantage to check the condition every few milliseconds and do whatever should be happening if the distance is greater than 8. While loops are only useful when you want to stop all other code and only run a small section until some condition changes.

thanks - so within this 'IF' statement, would I just be easier to skip the 'WHILE' part as the 'IF' statement will handle the distance check part initially and run the 'flash the LED' code as needed?

TheHansens: thanks - so within this 'IF' statement, would I just be easier to skip the 'WHILE' part as the 'IF' statement will handle the distance check part initially and run the 'flash the LED' code as needed?

That's part of it. You'll also need to get rid of the delay calls in your code and learn to write non-blocking code (see Blink Without Delay) if you want to be able to have two or more things (like driving a motor and blinking a light) happen at the same time.