drag tree 2.0

I finally got all my code to work my issue now is the sensor only read a car going by to maybe a fraction of a second and i have to cover the laser for at least 5 seconds for it to read it and it doesnt post the correct time on the serial monitor

// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte ARDUINOoutput = A0;
const byte ARDUINOInput = A1;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
const byte WinnerOut = 5;
const byte WinnerIn = 9;

// Pin values for Lasers
const byte StagingLinePin = A5;
const byte StartingLinePin = A4;
const byte EightMileLinePin = A2;
const byte FinishLinePin = A3;

//handicap timer for finish
unsigned long Handicapvalue = 0;

//  Delay between the countdoown lights
const unsigned InitialDelay = 1000; // between Stage and first Yellow
const unsigned YellowDelay = 500;  // between each Yellow and Green
const unsigned HandicapDelay = Handicapvalue;  // Handicap

// sensitivity value to determine if sensor is blocked
const int LaserInterruptSensitivity = 0;


// State Flags
bool RaceHasStarted = false;  // Set 'true' when Green light is turned on
bool RaceHasFinished = false; // Set 'true' when Finish Line is crossed

// Timers for Race
unsigned long RaceStartTime = 0;  // Set to micros() when Green light is turned on
unsigned long RaceFinishTime = 0;  // Set to micros() when Finish Line is crossed
unsigned long RaceEightMile = 0;   // Set to micros() when 1/8 Finish Line is crossed
unsigned long ReactionTimer = 0; // This will contain the reaction time
unsigned long HandicapTimer = 0;

// Timer for Light Countdown
unsigned long CountdownStart = 0;  // Note: Doubles as a state flag: if != 0, countdown is in progress; Set to millis() when Start Line is crossed

// Time since start of program
unsigned long now;

// Checks if sensor on analog input 'pin' is blocked
bool isSensorBlocked(byte pin)
{

  int val = 0;
  val = digitalReadFast(pin);  // read input value
  //Serial.print("Sensor Value: ");
  //Serial.print(val);
  //Serial.print("\n");

  return digitalReadFast(pin) == LaserInterruptSensitivity;
}

// Turns on Light on pin 'lightPin'
void turnOnLight(byte lightPin) {
  digitalWrite (lightPin, HIGH);
}

// Turns off Light on pin 'lightPin'
void turnOffLight(byte lightPin) {
  digitalWrite (lightPin, LOW);
}

// turn on Red LED, turn off all others
void falseStart() {

  // turn on Red LED
  turnOnLight(LEDRed);

  // turn off all other LEDs
  turnOffLight(LEDWhite1);
  turnOffLight(LEDWhite2);
  turnOffLight(LEDYellow1);
  turnOffLight(LEDYellow2);
  turnOffLight(LEDYellow3);
  turnOffLight(LEDGreen);
}

void checkStagingPin() {

  if (isSensorBlocked(StagingLinePin)) {
    //Serial.print("Staging Pin is blocked\n");
    turnOnLight(LEDWhite1);
    // delay(DebugDelay);
  }
  else {
    //Serial.print("Staging Pin NOT blocked\n");
    turnOffLight(LEDWhite1);
    // delay(DebugDelay);
  }

}

// if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
void checkStartingPin() {

  if (isSensorBlocked(StartingLinePin)) {
    //Serial.print("Start Pin is blocked  on THIS Arduino \n");
    // Turn off the red light again. We are back at the start.
    turnOffLight(LEDRed);
    turnOnLight(LEDWhite2);
    digitalWrite (ARDUINOoutput, HIGH);
    // delay(DebugDelay);
  } else { //
    digitalWrite(ARDUINOoutput, LOW);
    turnOffLight(LEDWhite2);
  }

  if ((digitalRead(ARDUINOInput) == HIGH) && isSensorBlocked(StartingLinePin)) {
    //Serial.print("Start Pin is blocked on OTHER Arduino \n");
    CountdownStart = millis();  // Start the countdown to GREEN
  }
}

void RaceInPreStage() {

  checkStagingPin();
  checkStartingPin();
}

void RaceInCountdown() {



  unsigned long elapsedCountdown =  now - CountdownStart;

  checkStagingPin();

  // The car has left the Starting Line before the countdown ended!
  if (!isSensorBlocked(StartingLinePin)) {
    falseStart();
    CountdownStart = 0; // DO NOT CONTINUE COUNTDOWN!
    return;
  }

  if (elapsedCountdown >= (InitialDelay + HandicapDelay)) {

    turnOnLight(LEDYellow1);
  }

  if (elapsedCountdown >= (InitialDelay + YellowDelay + HandicapDelay)) {
    turnOnLight(LEDYellow2);
  }

  if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + HandicapDelay)) {
    turnOnLight(LEDYellow3);
  }

  if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay + HandicapDelay)) {
    // START!
    turnOnLight(LEDGreen);
    turnOffLight(LEDWhite1);
    turnOffLight(LEDWhite2);
    turnOffLight(LEDYellow1);
    turnOffLight(LEDYellow2);
    turnOffLight(LEDYellow3);
    RaceHasStarted = true;
    RaceStartTime = micros(); // Measure race in microseconds
    CountdownStart = 0; // Countdown is done
  }
}

void RaceInProgress() {

  HandicapTimer = (Handicapvalue * 1000);

  // When the car leaves the starting line after we turned green, capture the reaction time
  if ( ReactionTimer == 0 && !isSensorBlocked(StartingLinePin)) {
    ReactionTimer = micros() - RaceStartTime;

  }

  if (isSensorBlocked(FinishLinePin)) {
    RaceFinishTime = micros() + HandicapTimer;  // Time of Finish
    RaceHasFinished = true;    
    Serial.print("Reaction Time (sec): ");
    Serial.println(ReactionTimer / 1000000.0, 6);
    Serial.print("Finishing time (sec): ");
    Serial.println((RaceFinishTime - RaceStartTime ) / 1000000.0, 6);
    Serial.print("Finishing time + Reaction time (sec): ");
    Serial.println(((RaceFinishTime - RaceStartTime) + ReactionTimer) / 1000000.0, 6);
   
  }

}

void RaceDone() {
   digitalWrite(WinnerOut, HIGH);

     
  if (digitalRead(WinnerIn) == HIGH) {
     Serial.print("Done Second ");
    //digitalWrite(WinnerOut, HIGH);
    
  }
      else if (digitalRead(WinnerIn) == LOW) {
         Serial.print("Done First"); 
         
      }
//digitalWrite(WinnerOut, HIGH);
while (1);
}

void setup() {
  Serial.begin(9600);

  // Initialize Lights On Drag Tree
  pinMode(LEDWhite1, OUTPUT); // Pre-stage: Car is on Staging Line
  pinMode(LEDWhite2, OUTPUT); // Staged: Car has reached Start Line
  pinMode(LEDYellow1, OUTPUT); // Turns on 'InitialDelay' milliseconds after White2
  pinMode(LEDYellow2, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow1
  pinMode(LEDYellow3, OUTPUT); // Turns on  'YellowDelay' milliseconds after Yellow2
  pinMode(LEDGreen, OUTPUT); // Start: Turns on  'YellowDelay' milliseconds after Yellow3
  pinMode(LEDRed, OUTPUT); // Fault: Leaving Staging Line between White2 and Green
  pinMode(WinnerOut, OUTPUT);
  pinMode(WinnerIn, OUTPUT);


  // Initialize Input Lasers
  pinMode(StagingLinePin, INPUT); //checks stage light laser
  pinMode(StartingLinePin, INPUT); //checks stage light laser
  pinMode(FinishLinePin, INPUT); //checks finish line light laser
  pinMode(EightMileLinePin, INPUT); //checks 1/8 mile line pin

  // Initialize communications between the two arduinos
  pinMode(ARDUINOoutput, OUTPUT);  // Arduino sends out signal to other arduino saying car is staged
  pinMode(ARDUINOInput, INPUT);   // Arduino recieves signal from other arduino saying car is staged
}


void loop()
{

  now = millis(); // Get current time

  if (RaceHasFinished) {
    RaceDone();
  }
  else if (RaceHasStarted) {
    RaceInProgress();
  }
  else if (CountdownStart != 0) {
    RaceInCountdown();
  }
  else {
    RaceInPreStage();
  }


}

Why 5 seconds? What is speed of loop, turns per second?

i have to cover the laser for at least 5 seconds

Go on, give us a clue where we should look.

If I remember correct You monitor racing cars. There should be no other work to do then checking the start/finish line. That could be done 1 miljon times per second.

what do you mean by no other work to do ? should I run one arduino just monitoring time?

the other problem is so the car has to be on the sensor for at least 3 seconds for anything to happen basically even at the start of the program when all i need is to turn on an led

Okey. I am close to say You are using the wrong kind of sensor that is so terribly slow, 3 seconds.

I'm trying to remember, if we've seen any design description that explains what is supposed to happen, what is the sequence of events, and how that sequence changes with any inputs. I can't imagine solving any of your problems without that.

Give us a link to that laser.

i really dont think its the laser because i tried running 5v to where its supposed to read high signal and it still took like 3 seconds but the laser is Amazon.com : Ximimark 5Pcs Laser Sensor Module Non-Modulator Tube Laser Receiver Detection Module Relay Switch 5V for Arduino : Camera & Photo

i really don't think its the laser because i tried running 5v to where its supposed to read high signal

It makes no sense to me.

running 5 volt to where?

It sounds like a jungle test and I don't understand what You have done.

Do You have a wide angle laser transmitter triggering those sensors?

Why not use usual light sensitive diodes? Light sensitive resistors are known to be slow.

I apologize. That kit looks like having both transmitter and receiver.
Anyway, why not use much faster light sensitive diodes not needing seconds to detect?

I tried running the other diodes what i did was i used switches and it was still slow even getting a direct signal

Sorry but I don't manage to get it. What was the wiring for those diodes, what was the diode triggering light, what was the code?
Your descriptions is like "it doesn't work", not telling anything.

Can You use Serial.print and print out millis telling the execution time of one turn of loop?
Something is confusing us.

Here the sun is about to raise and more than high time for me to use the pillow. Tomorrow a new day, new game, new bets,,,,

So everything on the code is doing what its supposed to but with a 3-6 second lag time from when something triggers to when the action happens

I used an arduino Due and it started working properly

Your main code is:

void loop()
{

  now = millis(); // Get current time

  if (RaceHasFinished) {
    RaceDone();
  }
  else if (RaceHasStarted) {
    RaceInProgress();
  }
  else if (CountdownStart != 0) {
    RaceInCountdown();
  }
  else {
    RaceInPreStage();
  }
}

In which one of those 4 "modes" do You experience this?

Can You clearify "something triggers" Is that the laser stuff?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.