train control

i have rebuild the code to implement slow break and start of the train.
as you can see in this video glitch - YouTube
when the train reverse there is a glitch. for a split second it gets a high voltage.
and i see no reason why is does this.
this is the point of code where the train reverse.

      while (Speed >= 0){
        analogWrite(3, Speed);   //Sets the speed of the train to 0 (255 is ful speed)
        Speed = Speed - 2;
        delay(20);
        } 

      while (Speed <= maxSpeed){
      digitalWrite(train, LOW); //Establishes forward direction of train
      digitalWrite(9, LOW);      //Disengage the Brake
      analogWrite(3, Speed);       //Sets the speed of the train (255 is ful speed)

so to ad it al up i have to read sensor input value`s twice to get right value, stil 1 of 10 times skips
shutting off the right LED (loop is going to fast or it just ignores the command, and now this.

is this general arduino or just my board ?.

further codewise am i an the right track of building this?.

full code;

  /*
  Train control
  Drive the train between two gates made of a LED and LDR.
  */

int ledL = 32;
int ledR = 34;
int sensL = A10;
int sensR = A11;
int train = 12;
int brake = 9;
int Speed = 0;
int maxSpeed = 150;
int sensLreading = analogRead;
int sensRreading = analogRead;
String state = String("Stands");

void setup() {
  Serial.begin(9600);
  pinMode(ledL, OUTPUT);
  pinMode(ledR, OUTPUT);
  pinMode(train, OUTPUT);
  pinMode(brake, OUTPUT);  
}

void loop() {
  sensLreading = analogRead(sensL);
  sensRreading = analogRead(sensL);
  delay(20);
  sensLreading = analogRead(sensR);
  sensRreading = analogRead(sensR);
  
  if (sensLreading > 200 && sensRreading > 200) {
      digitalWrite(ledL, HIGH);  //Turn left LED on  
      digitalWrite(train, LOW);  //Establishes left direction of train
      digitalWrite(brake, LOW);  //Disengage the Brake
      while (Speed <= maxSpeed){
        analogWrite(3, Speed);   //Sets the speed of the train (255 is ful speed)
        Speed = Speed + 1;
        delay(40);
        }
      state = String("goLeft");  //Sets "state"
      }
  
  sensLreading = analogRead(sensR);
  sensRreading = analogRead(sensR);
  delay(20);
  sensLreading = analogRead(sensL);
  sensRreading = analogRead(sensL);

  if (sensLreading > 200 && state == "goLeft") {
      digitalWrite(ledL, LOW);   //Turn left LED off
      digitalWrite(ledR, HIGH);  //Turn right LED on
      while (Speed >= 0){
        analogWrite(3, Speed);   //Sets the speed of the train to 0 (255 is ful speed)
        Speed = Speed - 2;
        delay(20);
        }
      
      while (Speed <= maxSpeed){
        digitalWrite(train, HIGH);  //Establishes backward direction of train
        digitalWrite(9, LOW);      //Disengage the Brake
        analogWrite(3, Speed);   //Sets the speed of the train (255 is ful speed)
        Speed = Speed + 1;
        delay(60);
        }
      state = String("goRight"); //Sets "state"
  }

  sensLreading = analogRead(sensL);
  sensRreading = analogRead(sensR);
  delay(20);
  sensLreading = analogRead(sensL);
  sensRreading = analogRead(sensR);

  if (sensRreading > 200 && state == "goRight") {
      digitalWrite(ledR, LOW);   //Turn right LED off
      digitalWrite(ledL, HIGH);  //Turn left LED on
      while (Speed >= 0){
        analogWrite(3, Speed);   //Sets the speed of the train to 0 (255 is ful speed)
        Speed = Speed - 2;
        delay(20);
        } 

      while (Speed <= maxSpeed){
      digitalWrite(train, LOW); //Establishes forward direction of train
      digitalWrite(9, LOW);      //Disengage the Brake
      analogWrite(3, Speed);       //Sets the speed of the train (255 is ful speed)
      Speed = Speed + 1;
      delay(60);
      }
      state = String("goLeft");  //Sets "state"
  }
}