Hi guys, bit of a noob with Arduino, not a massive electronics whizz, but here is what I have created... its an automated toy train set
The track is powered with a dedicated 12v, through a bridge driver controlled by the Arduino. All I want to do so far (and have made a bit of headway ) is have the train run from one end of the track to the other and pass over an LDR photocell which slows the train reverses direction and sends it back to the other end of the track, which I am pleased to say more or less works with this code.
Problem is, the LDR gives a high-value of 1023, which drops to below 500 when the train passes over. As the train comes back it looks like it is triggering the LDR sequence again and re-stops the train. This doesn't always happen just once, but sometimes 2 or 3 times when the LDR values fluctuate for some reason back to 1023 even when the sensor is dark (noise maybe?). Using a longer train, so the LDR is covered for the whole time the train is stopped does not do the trick, either the gaps in the coaches causes the value to jump or just the fluctuations.
I am wondering what the best solution might be for this? Have the Arduino ignore the LDR values for a few seconds after the first drop?
Only the ground is connected together between the 5v Arduino and 12v DC adapter, but perhaps there is some PWM interference or other noise created in the circuit.
Is there no way to use the LDR as a 'trigger once' sort of deal, where once the value has dropped below 500 then further values are ignored until the next cycle?
Any help really appreciated here guys!
const int r1Positive = 2;
const int r1Negative = 3;
const int r1Speed = 4;
int LDR5 = A12; // select the input pin for LDR
int LDR7 = A14; // select the input pin for LDR
bool trainRunning = false;
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(r1Positive, OUTPUT);
pinMode(r1Negative, OUTPUT);
pinMode(r1Speed, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
analogWrite(r1Speed, 200);
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(LDR7); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 500) //setting a threshold value
{ for (int i=255; i>=0; i--){
analogWrite(r1Speed, i);
delay(5);
}
trainRunning = false;
// wait for two seconds after stopping the train
delay(2000);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
analogWrite(r1Speed, 200);
}
sensorValue = analogRead(LDR5); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 500) //setting a threshold value
{ for (int i=255; i>=0; i--){
analogWrite(r1Speed, i);
delay(5);
}
trainRunning = false;
// wait for two seconds after stopping the train
delay(2000);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
analogWrite(r1Speed, 200);
}
}