Mega 2560 coding issues

Hello. Before I begin, Yes, I have read and searched the forums for my answer and I'm still baffled.
In the code below, when I start the program I'm trying to get LED1 to illuminate using a photoresistor which is the visual input for me to depress the pushbutton. It doesn't work for whatever reason. I have looked at it and tried several different coding configurations for LED 3 but it still doesn't work. The rest of the program works as designed. What am I overlooking? Thanks for any help

#include<LiquidCrystal.h>
#include<Servo.h>

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
Servo startingGate;

const byte buttonPin = 3; //starting line button for "GO"
const byte servoPin = 2;
const byte finishSensor1Pin = A0;
const byte finishSensor2Pin = A1;

const int darkThreshold = 500;
int LED1 = 7;   //lane 1 win light
int LED2 = 6;   //lane 2 win light
int LED3 = 43;  //prestage light
int finishSensor1;
int finishSensor2;
boolean finishFlag = false;
long startTime;
long stopTime;
float raceTime;

void setup()
{
 
  Serial.begin(9600);  // initializes servo for starting line
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  startingGate.attach(servoPin, 1000, 2000);
  startingGate.write(0);

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Drag Strip Timer");  //initial display Line 1 on lcd
  lcd.setCursor(0, 1);
  lcd.print("Push to start!");  //inital display Line 2 on lcd

  while (digitalRead(buttonPin) == HIGH)
  {
  }

  lcd.clear();
  lcd.print("Go!");  //displayed after race start
  digitalWrite(LED1, HIGH);  //turns on win light upon reset
  digitalWrite(LED2, HIGH);  //turns on win light upon reset
  digitalWrite(LED3, LOW);
  startingGate.write(180);
  startTime = millis();
}

void loop()
{
  //read the finish sensors at the same time
  finishSensor1 = analogRead(finishSensor1Pin);
  finishSensor2 = analogRead(finishSensor2Pin);
  if (finishFlag == false)
  {
    //car #1 crosses first
    if ((finishSensor1 < darkThreshold) && (finishSensor2 > darkThreshold))
    {
      stopTime = millis();
      raceTime = stopTime - startTime;
      finishFlag = true;
      digitalWrite(LED1, LOW);
      lcd.clear();
      lcd.print("Winner Lane #1");  //displayed if lane 1 photeresistor changes state before lane 2
      lcd.setCursor(0, 1);
      lcd.print("Time: ");
      lcd.print(raceTime / 1000, 3);
    }
    //car #2 crosses first
    else if ((finishSensor2 < darkThreshold) && (finishSensor1 > darkThreshold))
    {
      stopTime = millis();
      raceTime = stopTime - startTime;  //formula used to determine elapsed time
      finishFlag = true;
      digitalWrite(LED2, LOW); 
      lcd.clear();
      lcd.print("Winner Lane #2");   //displayed if lane 2 photeresistor changes state before lane 1
      lcd.setCursor(0, 1);
      lcd.print("ET: ");  //displays winning elapsed time
      lcd.print(raceTime / 1000, 3);
    }
  }
}

Please explain the above ?

That is the command to "start" the timer.


  finishSensor1 = analogRead(finishSensor1Pin);
  finishSensor2 = analogRead(finishSensor2Pin);

Print these values to see what they start at and go to when the light changes.



BTW, Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.

I’m not sure how to do this. I assume this is to determine the output status of the pin on the board?

Serial.println(finishSensor1);

Serial.println(finishSensor2);

To confirm what is actually being read.


There is an icon with an up arrow in the posting menu.

Use it to upload the schematic and actual wiring images.


“ I have looked at it and tried several different coding configurations for LED 3 but it still doesn't work”

Don’t see a digitalWrite(LED3, HIGH); anywhere.


BTW
Run the simple Blink sketch to see if the LED3 is wired properly.

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