Timing between 2 events

Hello,

I create 2 signals pluses PL1 & PL2 on the Arduino Nano.
I want to measure the time between 2 first pulses of 2 signals.

Here is the schematic and coding.
I don't know why I always got on the LCD display 105ms ±1

Do you have others method ?
Thanks,
V5D


#include <Wire.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);

int PL1_out = 2;
int PL2_out = 3;

int PL1_in = 9;
int PL2_in = 10;

void setup() {
  lcd.init();

  pinMode(PL1_out, OUTPUT);
  pinMode(PL2_out, OUTPUT);

  pinMode(PL1_in, INPUT);
  pinMode(PL2_in, INPUT);

}

void loop() {
  init();
  lcd.backlight();

  // set frequency PL1_out
  digitalWrite(PL1_out, HIGH);
  unsigned long Start = millis();
  delay(5);
  digitalWrite(PL1_out, LOW);
  delay(100); 
  
  // set frequency PL2_out, time off is longer than PL1_out.
  digitalWrite(PL1_out, HIGH);
  unsigned long End = millis();
  delay(5);
  digitalWrite(PL1_out, LOW);
  delay(400);  
  
  // calcul and display the delay between PL1 and PL2
  int process = End-Start;
  lcd.setCursor(0,0);
  lcd.print(process);
  lcd.print(" ms   ");
  delay(1);
}

Use 2 pin interrupt. (pin2 and pin3).

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

Mode use RISING.

  • Move the above into setup( )
  • Try

unsigned long process = End-Start;




  • This will always give delay(5); + delay(100); = 105ms
2 Likes

Is End-Start declared or defined somewhere hidden to me?

that's why ! Thanks !

Where is your code to read inputs?

1 Like

Do not use delay. Learn how to Blink Without Delay

1 Like

“ Is End-Start declared or defined somewhere hidden to me?“

unsigned long Start = millis();

. . .

unsigned long End = millis();

It's declared inside the "loop". Thanks

I use an oscilloscope, it makes things much easier. Measure from pulse to pulse: trigger on the starting edge and measure to the ending edge.

1 Like

Thanks for reply !

Here is the code updated:


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

int PL1_out = 2;
int PL2_out = 3;

int PL1_in = 9;
int PL2_in = 10;

void setup() {
  lcd.init();

  pinMode(PL1_out, OUTPUT);
  pinMode(PL2_out, OUTPUT);

  pinMode(PL1_in, INPUT);
  pinMode(PL2_in, INPUT);

  unsigned long Start;
  unsigned long End;
  int process;

}

void loop() {
  init();
  lcd.backlight();

  // set frequency PL1_out (1st event)
  digitalWrite(PL1_out, HIGH);
  delay(5);
  digitalWrite(PL1_out, LOW);
  delay(50);
  // Reading PL1_in
  if (digitalRead(PL1_in) == HIGH) {
    Start = millis();
    }

  // set frequency PL2_out, time off is longer than PL1_out (start 2nd event after <5 + 50> ms)
  digitalWrite(PL1_out, HIGH);
  delay(5);
  digitalWrite(PL1_out, LOW);
  delay(100);
   // Reading PL2_in
  if (digitalRead(PL2_in) == HIGH) {
    End = millis();
    }
  
  // calcul and display the delay between PL1 and PL2
  process = End-Start;
  lcd.setCursor(0,0);
  lcd.print(process);
  lcd.print(" ms   ");
  delay(1000);
}

But I got the error code that I don't know why for now....

C:\Users\multi\OneDrive\Documents\Arduino\vpdoARDUI\time_milli\time_milli.ino:53:13: note: suggested alternative: 'En'
process = End-Start;
^~~
En
C:\Users\multi\OneDrive\Documents\Arduino\vpdoARDUI\time_milli\time_milli.ino:53:17: error: 'Start' was not declared in this scope
process = End-Start;
^~~~~

exit status 1

Compilation error: 'Start' was not declared in this scope

Your declarations are hiding in the setup() function.

Move these lines out and up to the top of you sketch like you may have had it before, or certainly seen elsewhere:

unsigned long Start;
unsigned long End;
int process;

void setup() {
// blah blah blah

HTH

a7

You're right ! It's compiled after moving these lines.
Thanks bro.
V5D

It may compile, but the cide will not work.
When will the above be true?

Here are my notes.... You really need to move away from delay() to make this work.

digitalWrite(PL1_out, HIGH);
delay(5);
digitalWrite(PL1_out, LOW);
delay(50);
if (digitalRead(PL1_in) == HIGH) {// if PL1_in is tied to PL1_out, this line will ALWAYS be false
  Start = millis();
}
digitalWrite(PL1_out, HIGH);//do you mean PL2_out?
delay(5);
digitalWrite(PL1_out, LOW);//do you mean PL2_out?
delay(100);
// Reading PL2_in
if (digitalRead(PL2_in) == HIGH) {// if PL2_in is tied to PL2_out, which starts out LOW, this line will ALWAYS be false
  End = millis();
}
process = End - Start;// 0 - 0 will ALWAY be 0

This is not executing all lines simultaneously, so by the time you get to the digital read, you have gone from LOW to HIGH to LOW, so you are NEVER reading any other state.

1 Like

Hello,

I have 2 square signals that I want to detect the delay (dt) between 2 rise-time (or 2 fall-time) edges. That doesn't care the status/level logic (HIGH/LOW) other signal.

Do you know how to code with arduino language ?

I use Arduino NANO.

Thanks for all sugessions,
V5D

I do, yes. How about you make an attempt, and show us what you've come up with. Read about micros(), and maybe think about

if input A goes high,
store the time when that happened, then
loop until input B goes high,
compute how much time has elapsed
print that value
go back to watching input A

There are nuances, and you need to tell us if the inputs could, for example, either change first, etc. etc.

Depending on which processor you're using, using one or more hardware Timer / Counters in Input Capture mode would provide the lowest measurement jitter and highest accuracy.

have a look at attachinterrupt - rising edge

1 Like