"Start Stop LCD display time with Arduino"

Dear Friends,
Im a NewBee to Arduino setup, :o
Kindly help me through the Program.

I am doing a project in which i have three phototransistors , when a ball crosses the light to 1st phototransis. is obstructed , then the lcd connected to Arduino should start counting the time in milliseconds and when a ball cross, light to 3rd Phototransis. is obstructed the lcd should stop and display the time in ms.

//A program to display the time in milliseconds on LCD when a medium travels between two phototransistors//
//Please help me correct this program// :fearful:

//these constants wont change:

const int analogPin0=A0;      //pin that attached to first sensor
const int analogPin1=A1;      //pin that attached to second sensor
const int analogPin2=A2;      //pin that attached to third sensor 
                             


#include <LiquidCrystal.h>
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
   //set up the LCD's number of column and rows:
 lcd.begin(16,2);
 //print message
 lcd.print("zero, gravity!");
 
 
 //initialize the serial communications:
 Serial.begin(9600);
}

void loop() 
{
 int analogPin0=1;       //A0 bcm1,when light to phototransis1. is blockd-LCD start counting time in millisec
 int analogPin1=1;     //would use later
 int analogPin2=1;    //A2 bcm1,when light to phototransis2. is blockd-LCD pausetime.
 char time;              // direct if needed or not
 char count;            //direct if needed or not
 lcd.setCursor(0,1);
 lcd.print(millis()/1000);
 
 // read the value of first sensor fromPinA0:
 // if A0 or analogpin0 becomes 1, the LCD should start counting time in Milliseconds;

 if (analogPin0==1); 
 {
   lcd.print(millis()/1000);//i want to start lcd count in milliseconds what is the correct code?
 }

    //read the value of last sensor from pinA2:
    //if A2 or analogpin2 becomes 1, the LCD should pause or stop the reading in milliseconds

    if (analogPin2==1); 
    {
     lcd.print(millis()/1000);
    }
delay(500);// stop the timer if A2=1
}

//display the time in milliseconds start lcd count when A0==1 & pause lcd counting when A2==1
// no idea how this syntax can make LCD count in milliseconds

Please help me edit this program so that it does the desired. :grin:

**Many Thanks in Advance :slight_smile: **
Greatly Appreciated :slight_smile:

What is it You want to take Place and what does actually happened?
Give us a fault description.
The LCD is not counting. The Arduino is doing that and sending the Count to the LCD when counting is finished I guess.

  int analogPin0=1;       //A0 bcm1,when light to phototransis1. is blockd-LCD start counting time in millisec
  int analogPin1=1;     //would use later
  int analogPin2=1;    //A2 bcm1,when light to phototransis2. is blockd-LCD pausetime.

You already have global variables with those names. These local variables are completely separate.

I don't see where you ever read the state or value of the pins.

if (analogPin0==1);

if statement shouldn't have semicolon.

And you defined analogPin0 to have the value of 1

int analogPin0=1;

and you never change that value anywhere in the code. So this test seems useless. It will always be true.

Please post a draw of the circuit that you are using.
We'll have to know how the photo transistors are connected.
Do not define the variables AnalogPin0, AnalogPin1 and AnalogPin2 twice, you have already defined them on the top of the program (as global variables), do not do it again inside the loop function.

The idea is that inside the loop function you'll read the value of pins A0, A1 and A2 with function analogRead.
When A0 goes down you set a variable to 1 and store the time in another variable, when A2 goes down and IF the other variable is 1 then you get the current time, compute the difference and send it to the lcd.

Something like this (not tested)
This is not a finished program, it is just to give you an idea.

// global vars, so are defined outside loop()
long tA0=0L; // store A0 time
long tA2=0L; // store A2 time
int fA0=0; // flag for A0
loop()
{
    int vA0=0;
    int vA2=0;
    long tdiff=0L;

// it wont be this easy, unless the circuit is very well done, analogRead() will not return just a zero
// probably the test will have to be for vA0 to be below a certain level.

    vA0=analogRead(analogPin0);
    if(vA0==0 && fA0==0)
    {
        fA0=1;
        tA0=mills();
    }
    vA2=analogRead(analogPin2);
    if(vA2==0 && fA0==1)
    {
        tA2=mills();
        tdiff=tA0-tA2;
        print_this_value_on_lcd(tdiff);
        fA0=0;
    }
}

ocsav:
Please post a draw of the circuit that you are using.
We'll have to know how the photo transistors are connected.
Do not define the variables AnalogPin0, AnalogPin1 and AnalogPin2 twice, you have already defined them on the top of the program (as global variables), do not do it again inside the loop function.

The idea is that inside the loop function you'll read the value of pins A0, A1 and A2 with function analogRead.
When A0 goes down you set a variable to 1 and store the time in another variable, when A2 goes down and IF the other variable is 1 then you get the current time, compute the difference and send it to the lcd.

Something like this (not tested)
This is not a finished program, it is just to give you an idea.

// global vars, so are defined outside loop()

long tA0=0L; // store A0 time
long tA2=0L; // store A2 time
int fA0=0; // flag for A0
loop()
{
    int vA0=0;
    int vA2=0;
    long tdiff=0L;

// it wont be this easy, unless the circuit is very well done, analogRead() will not return just a zero
// probably the test will have to be for vA0 to be below a certain level.

vA0=analogRead(analogPin0);
    if(vA0==0 && fA0==0)
    {
        fA0=1;
        tA0=mills();
    }
    vA2=analogRead(analogPin2);
    if(vA2==0 && fA0==1)
    {
        tA2=mills();
        tdiff=tA0-tA2;
        print_this_value_on_lcd(tdiff);
        fA0=0;
    }
}

ocsav:
Please post a draw of the circuit that you are using.
We'll have to know how the photo transistors are connected.
Do not define the variables AnalogPin0, AnalogPin1 and AnalogPin2 twice, you have already defined them on the top of the program (as global variables), do not do it again inside the loop function.

The idea is that inside the loop function you'll read the value of pins A0, A1 and A2 with function analogRead.
When A0 goes down you set a variable to 1 and store the time in another variable, when A2 goes down and IF the other variable is 1 then you get the current time, compute the difference and send it to the lcd.

Something like this (not tested)
This is not a finished program, it is just to give you an idea.

// global vars, so are defined outside loop()

long tA0=0L; // store A0 time
long tA2=0L; // store A2 time
int fA0=0; // flag for A0
loop()
{
    int vA0=0;
    int vA2=0;
    long tdiff=0L;

// it wont be this easy, unless the circuit is very well done, analogRead() will not return just a zero
// probably the test will have to be for vA0 to be below a certain level.

vA0=analogRead(analogPin0);
    if(vA0==0 && fA0==0)
    {
        fA0=1;
        tA0=mills();
    }
    vA2=analogRead(analogPin2);
    if(vA2==0 && fA0==1)
    {
        tA2=mills();
        tdiff=tA0-tA2;
        print_this_value_on_lcd(tdiff);
        fA0=0;
    }
}

unfortunately i donot have a diagram, as this is only a first stage.
still i can give you more details, please see if u can help me structure the program.

three IRlight is incident on three phototransistor(1,2,3),when is light to phototrans1 is obructed by a moving Ball the output of phototransis(1,) becomes high i want while phototransis 2 and 3 are still low.

similarly when the ball reaches phototransis3, the output is high.while other two becomes low.

now this output change states of phototrans(1,2,3) from low to high are connected to ardino input (A0,A1,A2).

the moment out put of phototrans1 becomes high the timer should start counting and display LCD in millisec.
once the output of phototransis 2 becomes high the total time should be displayed on LCD in ms.

calculate the time taken for the moving ball to travel from bottom to top should be displayed.

the practical setup isnt completely ready.

Railroader:
What is it You want to take Place and what does actually happened?
Give us a fault description.
The LCD is not counting. The Arduino is doing that and sending the Count to the LCD when counting is finished I guess.

Three IRlight is incident on three phototransistor(1,2,3),when is light to phototrans1 is obructed by a moving Ball the output of phototransis(1,) becomes high while phototransis 2 and 3 are still low.

similarly when the ball reaches phototransis3, the output is high.while other two becomes low.

now this output change states of phototrans(1,2,3) are connected to ardino input (A0,A1,A2).

the moment out put of phototrans1 becomes high the ARDINO should start counting time and display LCD in millisec.
once the output of phototransis 2 becomes high the total time should be displayed on LCD in ms.

i havent still loaded the program to the arduino board.
still in the initial stage, so now im not able to give a description.

i accept the arduino does the counting and LCD should display total time.

Once you get through the weeds, an LCD is highly unlikely to update at any speed close to 1mS

As well as using the micros() timer to monitor your start/stop events - setup a millis() timer to refresh the LCD maybe every 100ms -[u]millis/u- so your code doesn't spend too much time waiting inside the LCD routines.

Better still - if you don't need fast updates, rethink your strategy and refresh much slower - or even only at the start and top events.

lastchancename:
As well as using the micros() timer to monitor your start/stop events - setup a millis() timer to refresh the LCD maybe every 100ms -[u]millis/u- so your code doesn't spend too much time waiting inside the LCD routines.

Why use millis()?
micros() is more than capable of counting to 100000.

itheen:
the moment out put of phototrans1 becomes high the timer should start counting and display LCD in millisec.
once the output of phototransis 2 becomes high the total time should be displayed on LCD in ms.

In this situation you don't need to use an Arduino timer, so you don't need to "start" anything, the Arduino already have a timer updating a value that can be read by the functions mills() and micros().
You just have to grab a timestamp when the 1st photo transistor go down and another when the 3th photo transistor go down. The time will be the difference between the two timestamps.
Unless you are measuring balls moving very fast a mills() timestamp will do, otherwise use micros().
I suggest that you build a test circuit before trying to write the software.

Why use millis()?
micros() is more than capable of counting to 100000.

Read my reply...
micros() is used for the event timing, while millis() is used for the LCD refresh timer.
You could use the underlyning micros ‘event timer’ but why waste more time with deriving the refresh interval when millis() is free.

ocsav:
In this situation you don't need to use an Arduino timer, so you don't need to "start" anything, the Arduino already have a timer updating a value that can be read by the functions mills() and micros().
You just have to grab a timestamp when the 1st photo transistor go down and another when the 3th photo transistor go down. The time will be the difference between the two timestamps.
Unless you are measuring balls moving very fast a mills() timestamp will do, otherwise use micros().
I suggest that you build a test circuit before trying to write the software.

Friend, timestamp its new term, let me read through for some knowledge, as well i have purchased the electronics, once i set it up and try to run my program over it, i will come back with my result to further debug it with your suggestions.

thank you. :slight_smile: