IR sensor timer using one Obstacle sensor

I'm relatively new to arduino, I have an idea on how to do it but struggling in coding my idea.
I have one osbtacle sensor. I need to measure the time between the sensor in LOW state (when an object passes) and HIGH state. I'm aware that I need to use interrupt on the arduino board and use millis() to get the value of time. I also have this formula:

TimeDifference = CurrentTime - PreviousTime

Sorry for being a noob on this. I can't find anything on google about this or I just didn't put the right query combination in order to get the answer I am looking for.

Your question's not really clear. Do you mean the sensor is normally high, and goes low when obstructed, and you need to measure the time between when it goes low (object comes in view) and goes high again (object has gone)?

Looking at your other thread, seems the answer's there?

In johnwasser's #3 there, lastRecordedTime is the last time the pin was high, so millis()-lastRecordedTime is the length of time it's been low.

grIMAG3:
I need to use interrupt on the arduino board

Very unlikely.

oh okay. thank you.

grIMAG3:
thank you.

You're welcome, but you could still answer my question :wink:

fishboneDiagram:
Do you mean the sensor is normally high, and goes low when obstructed, and you need to measure the time between when it goes low (object comes in view) and goes high again (object has gone)?

Yes. It is normally high and low when something passes on the obstacle sensor. I'm going to use this sensor to measure time between drops of intravenous fluid on an IV drip chamber. I'm really having a hard time figuring out the proper code. Thank you for the future replies.

grIMAG3:
between drops of intravenous fluid on an IV drip chamber.

In the pic, do you mean the end to start gap (a) or the start to start time (b) ?

drops.PNG

I think you probably mean (b) start of drop1 to start of drop2?

drops.PNG

Yes :slight_smile: . I need to measure the time between each drops of fluid that passes through the Obstacle sensor. I'm planning to desolder and extend the IR receiver and transmitter so that I can position it around the drip chamber. I'm more inclined on hardware rather than software that's why I'm really struggling.

My intrrnet just died grrr so now on my mobile which is a pain.

Still not clear... a or b in the diagram?

Look at the state change detect example in the ide to see how to detect a hi/lo or low/hi change.

I wrote you some code for both but cant upload until internet comes back...

Internet's back, posting this code quickly while it's in a good mood...

Only tested with buttons, wired pin to ground and internal pullups.

This measures start1 to start2:

// https://forum.arduino.cc/index.php?topic=663280
//  7 feb 2020
// how long between lows? (start of drip1 to start of drip2)


/*
  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 = 2;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
unsigned long wentLowAt;
unsigned long previousLowAt;
int lowToLowTime;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("how long between lows?");
  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 low at ");
      wentLowAt = millis();
      Serial.print(wentLowAt);
      lowToLowTime = wentLowAt - previousLowAt;
      Serial.print(", low to low time ");
      Serial.println(lowToLowTime);
      previousLowAt = wentLowAt;
    }// 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

This measures end1 to start2:

// https://forum.arduino.cc/index.php?topic=663280
//  7 feb 2020
// how long was the high? (end of drip1 to start of drip2)


/*
  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 = 2;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
unsigned long wentLowAt;
unsigned long wentHighAt;
unsigned long highToLowTime;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("how long was the high");
  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 == HIGH) //... release
    {
      Serial.print("start of gap at ");
      wentHighAt = millis();
      Serial.print(wentHighAt);
    }// change to high
    else
    {
      Serial.print(", end of gap at ");
      wentLowAt = millis();
      Serial.print(wentLowAt);
      highToLowTime = wentLowAt - wentHighAt;
      Serial.print(", length of gap ");
      Serial.print(highToLowTime);
      Serial.println("ms");
    }

    // 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

It's B on the diagram. Thank you :slight_smile:

Thank you so much sir! I'm going to try this in the morning. It's pretty late here now. It's very rare to find someone who has patience with some newbie on this forum. Going to uodate on my progress later on. Thank you again! :smiley:

Do you know for a fact that the sensor is "seeing" the drops, by the way?

You're seeing activity on the sensor output?

Thank you again!

Let's see if it works, first....

grIMAG3:
I'm going to use this sensor to measure time between drops of intravenous fluid on an IV drip chamber.

Interested to know why you're doing this. My son's a paramedic and was telling me that roadside they just count drops in 10s and that's close enough, and the hospital gear meters it accurately anyway.

I've tried using a transparent straw used on canned juices to see if the obstacle sensor can detect transparent materials and it did. Haven't tried it on real water drops though.

fishboneDiagram:
Interested to know why you're doing this. My son's a paramedic and was telling me that roadside they just count drops in 10s and that's close enough, and the hospital gear meters it accurately anyway.

I'm trying to develop a system that measures the IV fluid level, measure drip rate, send SMS to user about IV fluid level, view the device Geolocation via an android app. I'm living in a third world country, so developing cheap and reliable alternatives may provide great impact in my community or my country even. This also serves as my undergrad thesis for my Computer Engineering degree.

The only thing I'm having difficulty on is measuring the drops. The other features on my system works fine. :smiley:

grIMAG3:
This also serves as my undergrad thesis for my Computer Engineering degree.

I hope you cite specific forum help in your references, and the forum in general in the bibliography.

grIMAG3:
The only thing I'm having difficulty on is measuring the drops.

Did you try my code?

fishboneDiagram:
I hope you cite specific forum help in your references, and the forum in general in the bibliography.

Did you try my code?

Yes, I will cite all the references I used in developing my system. Thank you. :smiley:

I currently have this code:

#include "math.h"
#define obs 3

unsigned long presentTime = 0;
unsigned long previousTime = 0;
int resultTime;

void setup() {
  // put your setup code here, to run once:
  pinMode(obs, INPUT);
  Serial.begin(9600);
  Serial.println("Initializing...");
  delay(500);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(obs) == HIGH)
  {
    /*Serial.println("No Obstacle Detected");
    delay(100);*/
  }
  
  else if (digitalRead(obs) == LOW)
  {
    /*Serial.println("Obstacle Detected");
    delay(100);*/
    presentTime = millis();
    Serial.println(presentTime);
    resultTime = presentTime - previousTime;
    Serial.println("Result Time: ");
    Serial.println(resultTime);
    previousTime = presentTime;
  }
}

My result are:

05:48:24.842 -> Initializing...
05:48:31.781 -> 6944
05:48:31.781 -> Result Time:
05:48:31.781 -> 6944
05:48:31.781 -> 6945
05:48:31.828 -> Result Time:
05:48:31.828 -> 1
05:48:31.828 -> 6945
05:48:31.828 -> Result Time:
05:48:31.875 -> 0
05:48:31.875 -> 6956
05:48:31.875 -> Result Time:
05:48:31.922 -> 11
05:48:31.922 -> 6981
05:48:31.922 -> Result Time:
05:48:31.922 -> 25

If the result is in milliseconds, then the result time is e.g. 0.025 seconds (on the last result time)?