Counting axles on a model railroad

I am trying to count the number of axles on a model railroad train. I have a 2 lasers shining on a photo transistors. The speed of the train is variable and also the length of the train or the number of axles. axleFlag == 1or 2 and PTState1or2 depending on direction of travel.

This will count only one time, then it drops out of the loop. I need it to continue counting. If no hits for about 4 seconds then drop out.

Any help will be appreciated.
Thank You
Grandpa Jay

if (axleFlag == 1 && PTState == 0 ) // Right side
{
do
{
digitalWrite(ledPin, HIGH); // beeper or LED on
axleCount = axleCount++;
axleTime = micros(); // reset time after a hit on an axle
lcd.setCursor(12, 0);
lcd.print(axleCount);
do
{
delay(5); // wait for sensors to stabilize
PTState = digitalRead(PhotoTransistor);
}
while (PTState == 0); //stay here till axle clears
digitalWrite(ledPin, LOW); // turn off beeper
}
while (PTState == 0 );
}
if (axleFlag == 2 && PTState2 == 0 ) // Left side
{
do
{
digitalWrite(ledPin, HIGH); // connect to beeper
axleCount = axleCount++;
axleTime = micros(); // reset time after a hit on an axle
lcd.setCursor(12, 0);
lcd.print(axleCount);
do
{
delay(5); // wait for sensors to stabilize
PTState2 = digitalRead(PhotoTransistor2);
}
while (PTState2 == 0); //stay here till axle clears
digitalWrite(ledPin, LOW); // turn off beeper
}
while (PTState2 == 0 );
}

then it drops out of the loop

Which loop ?

You should be using regular while loops, not do-while loops. The exit behaviour need to be specified more exactly than "drops out". You didn't post the entire sketch. Please do so, using code tags as explained in the first sticky threads at the top of the forum.

This program was originally written by d. bondnar. I have part of it working but it does not count the number of axles. I added the do loop to see if that would work but it made no difference. I did not include the program originally because it is so large. The first "if (axleFlag == 1)" in the "Routines to count axles" is when the train comes in from the right and the right photo transistor is used to count axles, the second "if (axleFlag == 2)" is when the train comes in from the left and the left photo transistor used. When the right photo transistor is covered in the count routine the LCD display is "Axle Count = 5", as soon as the photo transistor is uncovered the LCD display is "Axles 5". This indicates to me that the count routine counted once and moved to "DO RAILROAD NAME & DEFECT DETECTOR" where the "lcd.print("Axles ");" is.
What should I use for the condition in the "while"?
Can this counting be done in an "if" statement, or should it be done with an interrupt?
Grandpa Jay
Thank You

Wheel_Defect_Counter.ino (31.7 KB)

I can't undertstand why you are using any sort of loop apart from the loop() function itself.

What I have in mind is to switch the appropriate led on and then regularly check if it is detected by the photo diode. Presumably if it is not, then you add 1 to the axle count. Something like this pseudo code

void loop() {
   switchLedOn
   detectedValue = digitalRead(photoDiodePin)
   if (detectedValue == LOW) {
     axleCount ++;
     lastReadMillis = millis();
   }
   if (millis() - lastReadMills > interval) {
      axleCount = 0;
   }
}

Obviously that code does nothing useful with the axle count - but I'm sure you can figure out how to do that.

And it would be much neater to put the code in a little function that is called from loop().

...R