Counter ON/OFF

Counter ON/OFF
This program should turn on the counter by pulses on pin #2 and turn off by pulses on pin #3, the displayed number should i related to phase difference between pulses on mentioned pins.
When I change the phase shift there is not changes on LCD counter is counting something.
When signals are aplyied to inputs the numpers are changing continuosly, when signals are turned OFF the counter is slowly count backward existind random number.
So there is no action
1.start counter
2. stop counter
Any idea what is wrong ?

#include <LiquidCrystal.h> 
//LiquidCrystal lcd(4,5,6,7,8,9); 

LiquidCrystal lcd(13, 12, 6, 7, 10, 11);  // RS, E, D4, D5, D6, D7.


unsigned long a = 0; 
unsigned long b = 0; 
unsigned long c = 0; 
unsigned long d = 0; 
unsigned long e = 0; 
volatile int state = HIGH; 
void setup() 
{ 
lcd.begin(5,2); 
pinMode(2, INPUT); 
pinMode(3, INPUT); 
} 

void loop() 
{ 

while(b==0 && d==0) { 
    if(digitalRead(2)==1 && a==0) { a=micros(); } 
     if(digitalRead(2)==0 && a!=0) { b=micros(); } 
     if(digitalRead(3)>0  && c==0) { c=micros(); } 
    if(digitalRead(3)>0  && c!=0) { d=micros(); } 
 } //end while

if(a>b) 
{ 
e=(a-c)*360/(a-b); 
} 
else 
{ 
e=(a-c)*360/(b-a); 
} 
lcd.setCursor(0,0); 
lcd.print( e); 

a = 0; 
b = 0; 
c = 0; 
d = 0; 
}

For starters loop() gets called continuously forever.

I.E. You already have a loop structure in place courtesy of Arduino IDE.

So you don't really need this secondary loop structure: while(b==0 && d==0)

Secondly

turn on the counter

means to me that there is one single counter that is controlled by digital pins 2 and 3.

So why do you need all these counters?

unsigned long a = 0;
unsigned long b = 0;
unsigned long c = 0;
unsigned long d = 0;
unsigned long e = 0;

Based on your original post I would have thought something like the following would be appropriate:

uint32_t nTheCounter = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(115200);
}

void loop()
{
    if (digitalRead(2) == HIGH)
    {
        // Start the counter
        nTheCounter = micros();
    }
    if (digitalRead(3) == HIGH)
    {
        // Stop the counter
        nTheCounter = 0;
    }  
    // If the counter is turned on
    if (nTheCounter > 0)
    {
        // Update and display the counter
         nTheCounter = micros();
         Serial.println(nTheCounter);
    }
}
void loop( ) {
    while( digitalRead( pin2 ) == HIGH )   // you don't want to start in the middle
        ;                                  // of pin2 being HIGH
    
    for( ; ; ) {
        if( digitalRead( pin2 ) == HIGH ) {  // now catch the rising edge
            time = micros( );

            while( digitalRead( pin3 ) == LOW )  // stuck here if pin3 never goes HIGH!
                ;

            timeout = micros( ) - time;
            break;
        }
    }
    // do whatever with timeout value
}

I used code, it seems to work on serial monitor, when I added LCD I have this error.

error
C:\Users\OWNER\Documents\Arduino\f1\f1.ino: In function ‘void setup()’:

f1:6: error: ‘lcd’ was not declared in this scope

lcd.begin(16, 2);

^

C:\Users\OWNER\Documents\Arduino\f1\f1.ino: In function ‘void loop()’:

f1:31: error: ‘lcd’ was not declared in this scope

lcd.setCursor(0, 0);

^

Multiple libraries were found for “LiquidCrystal.h”
Used: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
Not used: C:\Users\OWNER\Documents\Arduino\libraries\Newliquidcrystal_1.3.5
Using library LiquidCrystal at version 1.0.5 in folder: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
exit status 1
‘lcd’ was not declared in this scope

uint32_t nTheCounter = 0;
#include <LiquidCrystal.h>
void setup()
{
  lcd.begin(16, 2);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(115200);
}

void loop()
{

  if (digitalRead(2) == HIGH)
  {
    // Start the counter
    nTheCounter = micros();
  }
  if (digitalRead(3) == HIGH)
  {
    // Stop the counter
    nTheCounter = 0;
  }
  // If the counter is turned on
  if (nTheCounter > 0)
  {
    // Update and display the counter
    nTheCounter = micros();
    Serial.println(nTheCounter);
    lcd.setCursor(0, 0);
    lcd.println(nTheCounter);

  }
}

boolrules code error

C:\Users\OWNER\Documents\Arduino\f3\f3.ino: In function 'void loop()':

f3:23: error: 'pin2' was not declared in this scope

while( digitalRead( pin2 ) == HIGH ) // you don't want to start in the middle

^

f3:27: error: 'pin2' was not declared in this scope

if( digitalRead( pin2 ) == HIGH ) { // now catch the rising edge

^

f3:28: error: 'time' was not declared in this scope

time = micros( );

^

f3:30: error: 'pin3' was not declared in this scope

while( digitalRead( pin3 ) == LOW ) // stuck here if pin3 never goes HIGH!

^

f3:33: error: 'timeout' was not declared in this scope

timeout = micros( ) - time;

^

Multiple libraries were found for "LiquidCrystal.h"
Used: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
Not used: C:\Users\OWNER\Documents\Arduino\libraries\Newliquidcrystal_1.3.5
Using library LiquidCrystal at version 1.0.5 in folder: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
exit status 1
'pin2' was not declared in this scope

Pin 3 is it going HIGH that why I have two inputs, one to start and second to stop timer

boolrules:

            while( digitalRead( pin3 ) == LOW )  // stuck here if pin3 never goes HIGH!

[/quote]

#include <LiquidCrystal.h> 
//LiquidCrystal lcd(4,5,6,7,8,9); 

LiquidCrystal lcd(13, 12, 6, 7, 10, 11);  // RS, E, D4, D5, D6, D7.
void setup() 
{ 

  lcd.begin(16, 2);
//lcd.begin(5,2); 
pinMode(2, INPUT); 
pinMode(3, INPUT); 
} 

void loop( ) {
    while( digitalRead( pin2 ) == HIGH )   // you don't want to start in the middle
        ;                                  // of pin2 being HIGH
    
    for( ; ; ) {
        if( digitalRead( pin2 ) == HIGH ) {  // now catch the rising edge
            time = micros( );

            while( digitalRead( pin3 ) == LOW )  // stuck here if pin3 never goes HIGH!
                ;

            timeout = micros( ) - time;
            break;
        }
    }
    // do whatever with timeout value
}

I figured you can write setup( ) and any constants yourself. Change the names if you like.

I am beginner with that but I will try, I fixed code LCD is counting continuously
293431488
293436172
293440848
293446200
293450872
293455556
293460572
293465588

The second pulse is not stoping the counter, counting is stop when I turn off yhe source of pulsess - generator.

I just wrote the part that does the measurement. ( I knew I should have left off loop( ) too )
Post the code you are using now.

Actually program is not responding to pin 2, when I remove signal from it there is no reaction, the counter is running the same way.

uint32_t nTheCounter = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 6, 7, 10, 11);  // RS, E, D4, D5, D6, D7.
void setup()
{
  lcd.begin(16, 2);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(115200);
}

void loop()
{

  if (digitalRead(2) == HIGH)
  {
    // Start the counter
    nTheCounter = micros();
  }
  if (digitalRead(3) == HIGH)
  {
    // Stop the counter
    nTheCounter = 0;
  }
  // If the counter is turned on
  if (nTheCounter > 0)
  {
    // Update and display the counter
    nTheCounter = micros();
    Serial.println(nTheCounter);
    lcd.setCursor(0, 0);
    lcd.println(nTheCounter);

  }
}

ted:
Actually program is not responding to pin 2, when I remove signal from it there is no reaction, the counter is running the same way.

I didn't saw it, there is a response, counter is stop counting 2 seconds after removing signal from pin 2

You really hove no idea what’s happening, do you? You are not using my code at all so I’ll just put comments in yours.

uint32_t nTheCounter = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 6, 7, 10, 11);  // RS, E, D4, D5, D6, D7.
void setup()
{
  lcd.begin(16, 2);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(115200);
}

void loop()
{

  if (digitalRead(2) == HIGH)   // this may or may not be the rising edge
  {                             // so your start time is suspect
    // Start the counter       // This comment is bogus. The "counter" is always running
                               // micros( ) returns the count at the time you read it but keeps counting.
    nTheCounter = micros();     // OK
  }
  if (digitalRead(3) == HIGH)   // you take one look at 3 and keep going - pointless
  {
    // Stop the counter     // this comment is bogus - the counter always runs
    nTheCounter = 0;     // you just erased your start time that you saved
  }
  // If the counter is turned on  // bogus
  if (nTheCounter > 0)      // this is equal to 0 if 3 was high, but it wasn't
  {
    // Update and display the counter
    nTheCounter = micros();         // now you set your counter to current count
    Serial.println(nTheCounter);     // that's why it always counts up
    lcd.setCursor(0, 0);
    lcd.println(nTheCounter);

  }
}

your code and the error
error
C:\Users\OWNER\Documents\Arduino\f3\f3.ino: In function ‘void loop()’:

f3:23: error: ‘pin2’ was not declared in this scope

while( digitalRead( pin2 ) == HIGH ) // you don’t want to start in the middle

^

f3:27: error: ‘pin2’ was not declared in this scope

if( digitalRead( pin2 ) == HIGH ) { // now catch the rising edge

^

f3:28: error: ‘time’ was not declared in this scope

time = micros( );

^

f3:30: error: ‘pin3’ was not declared in this scope

while( digitalRead( pin3 ) == LOW ) // stuck here if pin3 never goes HIGH!

^

f3:33: error: ‘timeout’ was not declared in this scope

timeout = micros( ) - time;

^

Multiple libraries were found for “LiquidCrystal.h”
Used: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
Not used: C:\Users\OWNER\Documents\Arduino\libraries\Newliquidcrystal_1.3.5
Using library LiquidCrystal at version 1.0.5 in folder: C:\Users\OWNER\Desktop\183\arduino-1.8.3\libraries\LiquidCrystal
exit status 1
‘pin2’ was not declared in this scope

#include <LiquidCrystal.h> 
//LiquidCrystal lcd(4,5,6,7,8,9); 

LiquidCrystal lcd(13, 12, 6, 7, 10, 11);  // RS, E, D4, D5, D6, D7.
void setup() 
{ 

  lcd.begin(16, 2);
//lcd.begin(5,2); 
pinMode(2, INPUT); 
pinMode(3, INPUT); 
} 

void loop( ) {
    while( digitalRead( pin2 ) == HIGH )   // you don't want to start in the middle
        ;                                  // of pin2 being HIGH
    
    for( ; ; ) {
        if( digitalRead( pin2 ) == HIGH ) {  // now catch the rising edge
            time = micros( );

            while( digitalRead( pin3 ) == LOW )  // stuck here if pin3 never goes HIGH!
                ;

            timeout = micros( ) - time;
            break;
        }
    }
    // do whatever with timeout value
}

I read your comments, thanks for explanations.
What is the solution ?

The solution, I'm afraid, is that you need to learn some C, how to basically write global constants, what belongs in
setup( ), and how arduino utility functions work.
Have you done any tutorials at all or is this your first piece of code?
I see that you have posted, on average, every day for a year. I don't mean to sound derogatory, but surely you can
figure out how to declare my pin names and set them up in setup( )?
In a year, you haven't used millis( )? micros( ) works the same way.
If you can, follow my comments in your code, try to fix it, re-post, and I'll take another look.

I am learning by using existing codes and when they don't work I asking the questions why if I can't find the answer. I will follow your commands and see what I can do.
Thanks for all your explanations.

This comment is bogus. The "counter" is always running - I thought that clock is always running and I can start and stop the counter which is counting clock pulses ?