ok integrated the "blink without delay" code.
train is running for 30 minutes now and
didn`t missed a port yet ![]()
need some tweaking on speed values....
/*
 Train control
 Drive the train between two gates made of a LED and LDR.
*/
// --------CONSTANTS (won't change)---------------
const byte sensL = A4;Â Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for ldr L
const byte sensR = A5;Â Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for ldr R
const byte led_L_Pin = 4;Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for the L LED
const byte led_R_Pin = 5;Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for the R LED
const byte motor_Pin = 3;Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for the motor speed
const byte brake_Pin = 9;Â Â Â Â Â Â Â Â Â Â Â Â // the pin number for the motor brake
const byte direction_Pin = 12;Â Â Â Â Â Â Â Â Â // the pin number for the motor direction
const int read_sens_Interval = 200;Â Â Â Â Â Â Â // millisecs between reading sensors
const int motor_Acc_Interval = 100;Â Â Â Â Â Â Â // millisecs between acceleration steps
const int motor_Dec_Interval = 100;Â Â Â Â Â Â Â // millisecs between deceleration steps
//------------ VARIABLES (will change)---------------------
unsigned long currentMillis = 0;Â Â Â Â Â Â Â Â // stores the value of millis() in each iteration of loop()
unsigned long previous_sens_Millis = 0;Â Â Â Â Â // will store the last time sensors are read
unsigned long previous_state_Millis = 0;Â Â Â Â // will store the last time state is printed
unsigned long previous_Acc_Millis = 0;Â Â Â Â Â // will store time of last acceleration step
unsigned long previous_Dec_Millis = 0;Â Â Â Â Â // will store time of last deceleration step
int sensLreading = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â Â //variable voor de waarde van sensL
int sensRreading = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â Â //variable voor de waarde van sensR
byte max_Speed = 200;Â Â Â Â Â Â Â Â Â Â Â Â Â Â //declare variable and set value for maximum Speed (0 to 255)
byte Speed = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â //declare variable and set value for current Speed (0 to 255)
boolean direct = true;Â Â Â Â Â Â Â Â Â Â Â Â Â //declare variable and set value for driving direction
String state = String("start");Â Â Â Â Â Â Â Â Â //declare variable and set value for state
//========================================
void setup() {
 Serial.begin(9600);
Â
 pinMode(led_L_Pin, OUTPUT);          // set led_L_Pin as output
 pinMode(led_R_Pin, OUTPUT);          // set led_R_Pin as output
 pinMode(motor_Pin, OUTPUT);          // set motor_Pin as output
 pinMode(brake_Pin, OUTPUT);          // set brake_Pin as output   Â
 pinMode(direction_Pin, OUTPUT);        // set direction_Pin as output
Â
//Â pinMode(sensL, INPUT);Â Â Â Â Â Â Â Â Â Â Â // not needed cousse analogread is always input?
//Â pinMode(sensR, INPUT);Â Â Â Â Â Â Â Â Â Â Â // not needed cousse analogread is always input?
Â
}
//========================================
void loop() {
 currentMillis = millis();           // capture the latest value of millis()
 read_sens();                 // read the sensors
 if (state == "start") {
  Start();
 }
Â
 if (state == "accelerate") {
  Accelerate();
 }
Â
 if (state == "decelerate") {
  Decelerate();
 }
}
//========================================
void read_sens() {
  if (currentMillis - previous_sens_Millis >= read_sens_Interval) { // time is up, so read sensors
   sensLreading = analogRead(sensL);                // read left ldr (high value means the light is off or blockt)
   //Serial.print("Left = ");                   // print for debug
   //Serial.println(sensLreading);                 // print for debug
   sensRreading = analogRead(sensR);                // read right ldr (high value means the light is off or blockt)
   //Serial.print("Right = ");                   // print for debug
   //Serial.println(sensRreading);                 // print for debug
   previous_sens_Millis += read_sens_Interval;           // and save the time we last read sensors
   if (sensLreading > 200 && direct == true) {           //if conditions are throu, the train reached left gate***
     digitalWrite(led_L_Pin, LOW);                //Turn left LED off
     digitalWrite(led_R_Pin, HIGH);                //Turn right LED on
     state = String("decelerate");                // set state to "decelerate"
     }
   if (sensRreading > 200 && direct == false) {          //if conditions are throu, the train reached right gate***
     digitalWrite(led_R_Pin, LOW);                //Turn right LED off
     digitalWrite(led_L_Pin, HIGH);                //Turn left LED on
     state = String("decelerate");                // set state to "decelerate"
     } Â
  }
}
//========================================
void Start() {
  digitalWrite(led_L_Pin, HIGH);                  // turn left led on
  digitalWrite(brake_Pin, LOW);                   // Disengage the Brake
  digitalWrite(direction_Pin, LOW);                 // Establishes left direction of train
  state = String("accelerate");                   // set state to "accelerate"
  direct = true;                          // set direction to true (true = LOW = left)
}
//========================================
void Accelerate() {
  if (currentMillis - previous_Acc_Millis >= motor_Acc_Interval) { // time is up, so accelerate
   Speed = Speed + 1;                       // add 1 to speed
   analogWrite(motor_Pin, Speed);                 // send Speed to motor_Pin
   Serial.print("Speed = ");                   // print for debug
   Serial.println(Speed);                     // print for debug
   previous_Acc_Millis += motor_Acc_Interval;           // last time of acceleration step
   if (Speed == max_Speed){                    // if speed reach max speed
    state = String("run");                    // set state to "run"
   }
  }
}
//========================================
void Decelerate() {
  if (currentMillis - previous_Dec_Millis >= motor_Dec_Interval) {  // time is up, so decelerate
   Speed = Speed - 1;                       // subtract 1 of speed
   analogWrite(motor_Pin, Speed);                 // send Speed to motor_Pin
   Serial.print("Speed = ");                    // print for debug
   Serial.println(Speed);                     // print for debug
   previous_Dec_Millis += motor_Dec_Interval;           // last time of deceleration step
  if (Speed == 0){
     if (direct == true){                     // flip direction
      direct = false;
      digitalWrite(direction_Pin, HIGH);             // Establishes right direction of train
      }
     else {
      direct = true;
      digitalWrite(direction_Pin, LOW);             // Establishes left direction of train
      }  Â
    state = String("accelerate");                 // dit gaat er uit
   }
  }
}
//========================================END