Photogate Timer

Hello,

I am trying to use two photoresistors to start and stop a timer. I want to print the elapsed time to an LCD screen.

Currently I have 2 separate photoresistors that turn on individual leds when darkened.

I have a basic “hello world” LCD code running in parallel with this circuit.

I am struggling to get the photoresistors to start and stop a timer instead of light up individual leds.

I have looked at various LCD stopwatch codes but am struggling to integrate the start/stop function with the photoresistors.

Any help would be very helpful.

Thankyou

Calum

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
const int pResistor2 = A1; // Photoresistor at Arduino analog pin A1
const int ledPin=8;       // Led pin at Arduino pin 9
const int ledPin2=9;     // Led pin at Arduino pin 10
//Variables
int value;				  // Store value from photoresistor (0-1023)


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");

 pinMode(ledPin, OUTPUT);  // Set lepPin - 9 pin as an output
 pinMode(ledPin2, OUTPUT);  // Set lepPin - 10 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
 pinMode(pResistor2, INPUT);// Set pResistor - A1 pin as an input (optional)


}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1);

 value = analogRead(pResistor);
  
  //You can change value "25"
  if (value > 650){
   startTime = millis();  //Turn LED off
  }
  else{
    digitalWrite(ledPin, HIGH); //Turn led on
  }

  delay(1); //Small delay

  value = analogRead(pResistor2);
  
  //You can change value "25"
  if (value > 650){
    digitalWrite(ledPin2, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin2, HIGH); //Turn led on
  }

  delay(1); //Small delay
  
}

What is the purpose of these?

  delay(1); //Small delay

How is your maths? What is the result of any number divided by one?

  lcd.print(millis() / 1);

Why does this turn an LED off? Where is your end time?

   startTime = millis();  //Turn LED off

I have checked and it makes no difference to the performance of the circuit. I have now removed it.

This is the up to date code, I have removed an error from the previous post.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
const int pResistor2 = A1; // Photoresistor at Arduino analog pin A1
const int ledPin=8;       // Led pin at Arduino pin 9
const int ledPin2=9;     // Led pin at Arduino pin 10
//Variables
int value;				  // Store value from photoresistor (0-1023)


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");

 pinMode(ledPin, OUTPUT);  // Set lepPin - 9 pin as an output
 pinMode(ledPin2, OUTPUT);  // Set lepPin - 10 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
 pinMode(pResistor2, INPUT);// Set pResistor - A1 pin as an input (optional)


}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() ;

 value = analogRead(pResistor);
  
 
  if (value > 650){
    digitalWrite(ledPin, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin, HIGH); //Turn led on
  }



  value = analogRead(pResistor2);
  

  if (value > 650){
    digitalWrite(ledPin2, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin2, HIGH); //Turn led on
  }


  
}

You do understand that as soon as loop returns, it gets called again?

There's no sense in your code that you wait for the "on" condition from the first photoresistor, before waiting for the "on" condition from the second.

calmac12:
I have checked and it makes no difference to the performance of the circuit. I have now removed it.

A lot of goofy code will make no difference to the performance of the circuit. However, it makes a huge difference to other people who have to read it and try to understand its purpose. It also may affect the efficiency of the code.

Your revised code won't compile.

Your comment isn't true:

  // print the number of seconds since reset:
  lcd.print(millis() ;

You haven't explained what the 1 millisecond delays are for.

My apologies. That part should read:

  // print the number of seconds since reset:
  lcd.print(millis() );

No, the comment still doesn't match the code.