I'am facing a problem in reading time between two INPUTS.
The project is as follows: A sensor gives a input to the Arduino. Then after sometime the same sensor gives another input to the Arduino.
I WANT TO MEASURE THE TIME BETWEEN THESE TWO INPUTS GIVEN BY THE SENSOR.
Which function should I use : pulseIn()
millis()
or any another
I have read all the posts on the Forum but I couldn't get a suitable answer .
PLEASE HELP !!!!!!!!!!!!!
Maybe any of these, maybe interrupts, maybe hardware timers. You failed to provide the necessary information. Either provide links to the manual/datasheet of that sensor or provide the technical data we would find there yourself (time range of that signal, accuracy needed, signal voltage, measuring error acceptable, etc.).
I'am facing a problem in reading time between two INPUTS.
The project is as follows: A sensor gives a input to the Arduino. Then after sometime the same sensor gives another input to the Arduino.
I WANT TO MEASURE THE TIME BETWEEN THESE TWO INPUTS GIVEN BY THE SENSOR.
Which function should I use : pulseIn()
millis()
or any another
I have read all the posts on the Forum but I couldn't get a suitable answer .
PLEASE HELP !!!!!!!!!!!!!
"A sensor gives a input to the Arduino" What kind of sensor input? Analog? Digital? Serial? "Then after sometime" How long? microseconds, milliseconds, seconds, minutes?
ToddL1962: "A sensor gives a input to the Arduino" What kind of sensor input? Analog? Digital? Serial? "Then after sometime" How long? microseconds, milliseconds, seconds, minutes?
a digital sensor gives the input after some milliseconds
Code below has a button wired from pin14 to ground. Run with monitor open.
// https://forum.arduino.cc/index.php?topic=671988
// time between two digital inputs
/*
BASED ON State change detection (edge detection) changed for INPUT PULLUP
(button wired from pin to ground)
https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/
// this constant won't change:
const int button = 14;
// Variables will change:
bool buttonState; // current state of the button
bool lastButtonState; // previous state of the button
unsigned long firstEventAt;
unsigned long secondEventAt;
unsigned long timeBetweenEvents;
bool weAreTiming;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println("time between two digital inputs");
pinMode(button, INPUT_PULLUP);
//initialize button states
buttonState = digitalRead(button);
lastButtonState = buttonState;
//turn bulitin led off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println(" ");
Serial.println("setup() done... press the button");
Serial.println(" ");
}
void loop()
{
// read the button:
buttonState = digitalRead(button);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
{
if (buttonState == LOW) //... and if it's now low, that's a press
{
Serial.print("New event");
if (!weAreTiming)
{
weAreTiming = true;
Serial.print(" start ");
firstEventAt = millis();
Serial.println(firstEventAt);
}
else
{
weAreTiming = false;
Serial.print(" end ");
secondEventAt = millis();
Serial.println(secondEventAt);
timeBetweenEvents = secondEventAt - firstEventAt;
Serial.print(" Time between events ");
Serial.println(timeBetweenEvents);
}
}// change to low
// Delay a little bit to avoid bouncing
delay(50);
}//change
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
} //loop
Adivijaya:
a digital sensor gives the input after some milliseconds
If you are getting data from the sensor with digitalRead() then immediately afterwards you should save the value of millis() to a variable and do the same for the second reading. The difference between the two time values is the interval between readings. Use micros() for greater accuracy if the interval is short.
If that does not solve the problem then post your program.
It is very inappropriate to waste time discussing how to deal with this when the nature of the "sensor" and the purpose of the measurement remain a secret.
Robin2:
If you are getting data from the sensor with digitalRead() then immediately afterwards you should save the value of millis() to a variable and do the same for the second reading. The difference between the two time values is the interval between readings. Use micros() for greater accuracy if the interval is short.
If that does not solve the problem then post your program.
...R
I got your point, but I am not able to figure out how to save value of millis() after the second reading.
Can you please give me a hint?
// https://forum.arduino.cc/index.php?topic=671988
// time between two digital inputs
/*
BASED ON State change detection (edge detection) changed for INPUT PULLUP
(button wired from pin to ground)
https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/
// this constant won't change:
const int button = 14;
// Variables will change:
bool buttonState; // current state of the button
bool lastButtonState; // previous state of the button
unsigned long firstEventAt;
unsigned long secondEventAt;
unsigned long timeBetweenEvents;
bool weAreTiming;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println("time between two digital inputs");
pinMode(button, INPUT_PULLUP);
//initialize button states
buttonState = digitalRead(button);
lastButtonState = buttonState;
//turn bulitin led off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println(" ");
Serial.println("setup() done... press the button");
Serial.println(" ");
}
void loop()
{
// read the button:
buttonState = digitalRead(button);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
{
if (buttonState == LOW) //... and if it's now low, that's a press
{
Serial.print("New event");
if (!weAreTiming)
{
weAreTiming = true;
Serial.print(" start ");
firstEventAt = millis();
Serial.println(firstEventAt);
}
else
{
weAreTiming = false;
Serial.print(" end ");
secondEventAt = millis();
Serial.println(secondEventAt);
timeBetweenEvents = secondEventAt - firstEventAt;
Serial.print(" Time between events ");
Serial.println(timeBetweenEvents);
}
}// change to low
// Delay a little bit to avoid bouncing
delay(50);
}//change
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
} //loop
But it didn't work on my Arduino.
And the one that I wrote:
unsigned long duration;
int sound_pin=3;
void setup()
{
Serial.begin(9600);
pinMode(sound_pin,INPUT);
}
void loop()
{
duration=pulseIn(sound_pin,HIGH,2500000);
Serial.println(duration);
}
Also didn't work on my Arduino UNO.
I think that there must be some or the other hardware problem with my Arduino UNO.
I can say that because I have tried some example sketches in the IDE but they didn't work on the Arduino as well.
Arduino is giving OUTPUTS correctly but it is not processing the INPUTS in a correct way. Also the Serial communication is not proper. What should I do???
I can't say why it's not working for you. I copied back my code that you posted in #11 just to make sure I had the exact code you did, and with the button pin 14 to ground, I got this output:
time between two digital inputs
setup() done... press the button
New event start 1667
New event end 5711
Time between events 4044
New event start 11188
New event end 20764
Time between events 9576
New event start 26746
New event end 39655
Time between events 12909
New event start 40790
New event end 41119
Time between events 329