while loop only working once inside a for loop then ignoring it

OK here is the updated code that doesn’t work properly. The while loop (while(LDR == “light”)) inside the for loop of function calibrate() doesn’t check for the while loop after the first iteration. Any help is appreciated.

#include <Servo.h>
#define SL 6 //left servo
#define SR 5 //right servo
#define SSL 85 //left servo stops value
#define SSR 80 //right servo stops value

//LED colours
#define GREEN 7
#define YELLOW 12
#define RED 13

#define DEBUG 0

#define IRT 3 //transmitter
#define IRR 2 //reciever
#define PBL 4 // Left pushbutton

Servo leftServo, rightServo;

//Barcode options
String leftStr = "1100";

void setup() {
  Serial.begin(9600);
  leftServo.attach(SL); //assigns leftServo to SL
  rightServo.attach(SR); //assigns rightServo to SR
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);

  pinMode(GREEN, OUTPUT); // setup green LED pin as output
  pinMode(YELLOW, OUTPUT); // setup yellow LED pin as output
  pinMode(RED, OUTPUT); // setup red LED pin as output
  calibrate();

  pinMode(IRR, INPUT); // setup IR LED pin as input
  pinMode(IRT, OUTPUT); // setup IR LED pin as output
  tone(IRT, 38000); // Start sending IR pulses at 38kHz on pin 3
  noTone(IRT);
}

void setLEDs(int green_state, int yellow_state, int red_state) {
  /* Code to switch green LED on or off according to green_state */
  /* Code to switch yellow LED on or off according to yellow_state */
  /* Code to switch red LED on or off according to red_state */
  digitalWrite(GREEN, green_state); // turn on LED on pin 7
  digitalWrite(YELLOW, yellow_state); // turn on LED on pin 12
  digitalWrite(RED, red_state); // turn on LED on pin 13
}

String calibrate() { // checks state of middle LDR
  int i = analogRead(A1);
  if (i < 400) {
    return "dark";
  }
  else if (i > 550) {
    return "light";
  }
  else {
    return "between";
  }
}

String barcodeS() { // reads the barcode length and stores as a string
  String curString = "";
  setSpeed(10, 11);
  String LDR = calibrate();
  for (int x = 0; x < 4; x++) {
    Serial.println("IN FOR LOOP");
    while (LDR == "light") {
      LDR =  calibrate();
#ifdef DEBUG
      Serial.println("Still in light");
#endif
    }
    unsigned int start = millis();
    while (LDR == "dark") { // stays in loop until LDR is no longer on barcode
      LDR = calibrate();
#ifdef DEBUG
      Serial.println("Still in dark");
#endif
    }
    unsigned int endo = millis();
    unsigned int width = endo - start;
    if (width >= 450) { // Used for thick barcode
      curString += "1"; // stores value of barcode in a string
#ifdef DEBUG
      Serial.println(curString);
#endif
    }
    else if (width < 450) { // Used for thin barcode
      curString += "0";
#ifdef DEBUG
      Serial.println(curString);
#endif
    }
    delay(10);
    Serial.println("IN HERE");
  }
#ifdef DEBUG
  Serial.println(curString);
  Serial.println("Exiting barcode function");
#endif
  return curString;
}


/*----------------------------------------------------------------------------------------------------------------------------*/
void setSpeed(int left, int right) {//change individual speed of servos
  leftServo.write(SSL + left);
  rightServo.write(SSR - right);
}

void turnAngle(int angle) { //turns robot to left or right (e.g. -90 = turn left)
  if (angle >= 0) {
    setSpeed(20, -20);
    delay(angle * 17L);
    setSpeed(0, 0);
  }
  else {
    setSpeed(-20, 20);
    delay(abs(angle) * 17L);
    setSpeed(0, 0);
  }
}

void left() {
  setLEDs(HIGH, LOW, LOW);
  turnAngle(-90);
  setLEDs(LOW, LOW, LOW);
}

void loop() {
  Serial.println("IN LOOP");
  String curStr = barcodeS();
  Serial.print("length: ");
  Serial.println(curStr);
  if (curStr.length() == 4) {
    Serial.println("Got barcode!");
    if (curStr.equals(leftStr)) {
      left();
#ifdef DEBUG
      Serial.println("Turning left");
#endif
    }
  }
  else {
    Serial.println("Not done yet");
  }
}

Screenshot 2018-12-05 09.17.46.png

Screenshot 2018-12-05 09.17.26.png

Screenshot 2018-12-05 09.17.16.png

You have to post the whole code.
For example, it is not clear what calibrate() returns. Is it just "light" and "dark" or also something else.

You know those variable declarations that must always be there, even though they don't seem to be important? They are important. We can't advise you properly without seeing them.

MorganS:
You know those variable declarations that must always be there, even though they don't seem to be important? They are important. We can't advise you properly without seeing them.

Totally and maybe having it returning a String is not such a good idea.