train control

ok integrated the "blink without delay" code.
train is running for 30 minutes now and
didn`t missed a port yet :slight_smile:

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