did some work on it,
/*
Train control
Drive the train between two gates made of a LED and LDR.
*/
byte ledL = 4; //declare variable and value for led L pin
byte sensL = A4; //declare variable and value for ldr L pin
byte ledR = 5; //declare variable and value for led R pin
byte sensR = A5; //declare variable and value for ldr R pin
byte brake = 9; //declare variable and value for train brake pin
byte trainD = 12; //declare variable and value for train direction pin (High/Low)
byte trainS = 3; //declare variable and value for train speed pin (0 - 255)
byte cSens = A0; //declare variable and value for train Current Sensing pin (not used yet)
int Speed = 0; //declare variable and set value for Speed (0 to 255 for speed)
int maxSpeed = 200; //declare variable and set value for maximum speed (255 is ful speed)
int sensLreading = 0; //declare variable for left ldr reading value
int sensRreading = 0; //declare variable for right ldr reading value
int cSensreading = 0; //declare variable for train Current Sensing value (not used yet)
int accspeed = 40; //declare variable for acceleration speed
int decspeed = 2; //declare variable for deceleration speed
String state = String("Stands"); //declare variable and set value for state (keeps track of driving direction)
void setup() {
Serial.begin(9600); //open serial port for communication
pinMode(ledL, OUTPUT); //set pin 32 to output for left led
pinMode(ledR, OUTPUT); //set pin 34 to output for right led
pinMode(trainD, OUTPUT); //set pin 12 to output for train (direction)
pinMode(brake, OUTPUT); //set pin 9 to output for train (brake)
pinMode(trainS, OUTPUT); //set pin 3 to output for train (speed)
digitalWrite(brake, LOW); //Disengage the Brake
}
void loop() {
readSens();
printReport();
if (sensLreading > 200 && sensRreading > 200) { //if both ldr are high value, the program just started and nothing happend yet
digitalWrite(ledL, HIGH); //Turn left LED on
digitalWrite(trainD, LOW); //Establishes left direction of train
accelerate(); //accelerate the train
state = String("goLeft"); //set state (keeps track of driving direction)
}
readSens();
printReport();
if (sensLreading > 200 && state == "goLeft") { //if left ldr is high value and state is "goleft", the train reached left gate
digitalWrite(ledL, LOW); //Turn left LED off
digitalWrite(ledR, HIGH); //Turn right LED on
decelerate(); //decelerate the train
digitalWrite(trainD, HIGH); //reverse direction of the train
accelerate(); //accelerate the train
state = String("goRight"); //set state (keeps track of driving direction)
}
readSens();
printReport();
if (sensRreading > 200 && state == "goRight") { //if right ldr is high value and state is "goright", the train reached right gate
digitalWrite(ledR, LOW); //Turn right LED off
digitalWrite(ledL, HIGH); //Turn left LED on
decelerate(); //decelerate the train
digitalWrite(trainD, LOW); //reverse direction of the train
accelerate(); //accelerate the train
state = String("goLeft"); //set state (keeps track of driving direction)
}
}
//******** functions ********
void readSens() {
cSensreading = analogRead(cSens); //read current sensing of motor
cSensreading = analogRead(cSens); //read current sensing of motor
sensLreading = analogRead(sensL); //read left ldr (a high value means the light is off or blockt)
sensLreading = analogRead(sensL); //read left ldr (a high value means the light is off or blockt)
sensRreading = analogRead(sensR); //read right ldr (a high value means the light is off or blockt)
sensRreading = analogRead(sensR); //read right ldr (a high value means the light is off or blockt)
}
void printReport() {
Serial.println("Start Report;"); //print "Start Report;"
//Serial.print("sensL = "); //print "sensL = "
//Serial.println(sensLreading); //print sensL value (for debugging purposive)
//Serial.print("sensR = "); //print "sensR = "
//Serial.println(sensRreading); //print sensR value (for debugging purposive)
Serial.print("Speed = "); //print "speed = "
Serial.println(Speed); //print speed (for debugging purposive)
Serial.print("Current Sensing value = "); //print "Current Sensing value = "
Serial.println(cSensreading); //print Current Sensing value (for debugging purposive)
}
void accelerate() {
while (Speed < maxSpeed) { //loop to accelerate to maximum speed
readSens(); //read sensors
printReport(); //print report
analogWrite(trainS, Speed); //Sets the speed of the train (255 is ful speed)
Speed = Speed + 1; //add 1 to speed
delay(accspeed); //slow down the proces
}
}
void decelerate() {
while (Speed > 0) { //loop to decelerate to 0 speed
readSens(); //read sensors
printReport(); //print report
if (Speed < 0) {
Speed = 0;
}
analogWrite(trainS, Speed); //Sets the speed of the train (255 is ful speed)
Speed = Speed - 20; //subtract 20 of speed
delay(decspeed); //slow down the proces
}
}
build some functions as recommanded and move some things.
but problems are increasing instead of being solved.
one of them is, program is to busy to keep track of the sensors.
its not the LDR being slow but when the train passing the sensor
program is busy with for example the print report.