#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);
}
#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
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.
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.
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.