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
You're welcome, but you could still answer my question
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.
Yes . 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.
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
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!
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.