Combining 2 programs to run in sequence

Just when I think I have earned something, I get kicked below the belt again. LOL. I constructed the following code to use 2 photoresistors to turn on 2 LEDs. It works as it should. Here is the code.
They are wired as described in the Arduino examples.
This program uses 2 photoresistors at the end of the coding to stop the timer and determine the winner also. I'm wanting to run the 1st program to turn on 2 LEDS and once this has happened, allow that to trigger the sequential countdown in the second program. that starts on line 43-44. Any help would be appreciated..

PRE-STAGE CODING   car 1 uses pins A5 and pin 50.  The photoresistor value is 120.  Car 2 uses A4 and pin 48 and the value changes to 120
 
int ldr=A4;//Set A4(Analog Input) for LDR2.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(48,OUTPUT);
}
 
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
 
if(value<=200)
  {
    digitalWrite(48,HIGH);//Makes the LED glow in Dark.
  }
  else
  {
    digitalWrite(48,LOW);//Turns the LED OFF in Light.
  }
}

_________________________________________________________________________________________________
Second code starts here.....


`#include<LiquidCrystal.h>


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


const byte buttonPin = 3; //starting line button for "GO"

const byte finishSensor1Pin = A0;
const byte finishSensor2Pin = A1;

const int darkThreshold = 250;

int LED6 = 6;   //lane 1 win light used to determine winner
int LED7 = 7;   //lane 2 win light used to determine winner
int LED1 = 41;
int LED2 = 43;
int LED3 = 45;

int finishSensor1;
int finishSensor2;
boolean finishFlag = false;
long startTime;
long stopTime;
float raceTime;

void setup()
{
 
  Serial.begin(9600);  
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(LED1, OUTPUT);     //Configures the specified pin to behave as an output. 
  pinMode(LED2, OUTPUT);    
  pinMode(LED3, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
   
  digitalWrite(LED1, HIGH);   
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED6, HIGH);  
  digitalWrite(LED7, HIGH); 
  
  delay(4000);
  digitalWrite(LED1, LOW);    
  delay(1000); 
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, LOW);     
  delay(1000);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, LOW);    
  delay(4000);
  digitalWrite(LED3, HIGH);
  
  

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Drag Strip Timer");  // display Line 1 on lcd
  lcd.setCursor(0, 1);
  lcd.print("Push to start!");  // display Line 2 on lcd
  
  while (digitalRead(buttonPin) == HIGH)
  {
  }

  lcd.clear();
  lcd.print("Go!");
  lcd.setCursor(0, 1);
  lcd.print("Who will win?"); 
  
  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(LED6, 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(LED7, 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);
    }
  }
}


 `

The line numbers are part of the IDE screen, not part of your code, so we can't see line numbers.

You can make EACH program a function, and run the functions individually from loop(). Any conflicting names will have to be resolved by yourself.
Paul

With some more basic knowledge about how writing programs for arduino works you will understand why it does not yet work

any program has only one function

void setup {
}

and one function

void loop  {
}

I recommend that you read at least part 1, part 2 and part 15 of this tutorial

Of course it will be good if you work through all parts.

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

This is my tutorial giving step by step instructions and some warnings about Merging Code

Edit:- Just had a look at it and it seems two screen dumps are being displayed a bit odd. As it is a few years since I wrote this it seems like the HTML generator is a bit out of date. I will have to fix this sometime but it was written in the now obsolete and unsupported app iWeb.

Edit 2:- When you come to the two "wrong" pictures, then left click on them and select "open image in New Tab" to see them displayed correctly. Sorry but it is a quick work round while I get things sorted out.

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