Go Down

Topic: Measuring time between two events  (Read 284 times) previous topic - next topic

trobs3

Hey Guys, new to arduino and need some help!

I have a load scale all calibrated and working. What I am trying to do is measure time between two events.

Event one: weight off scale leaves (Load Scale reads less than 10 lbs)
Event two: weight comes back onto the scale (Load Scale reads greater than 10 lbs)

How can i track (measure) the time when the weight is off the scale accurately?


And then use the time that was measured for another part in my coding?


Thank you guys so much for your help, these forums have seriously saved me!


Thanks,
Ty



Here is what my code looks like:

#include <SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h>

#include <HX711.h>

#include "HX711.h"

#define DOUT  3
#define CLK  2

HX711 scale;

float calibration_factor = -180000; //-180000 calibration is what worked

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");
 
  scale.begin(DOUT, CLK);
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
 
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); // Remove the need to tare the scale.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust my calibration factor
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" lbs"); //Change this to kg if needed
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

    if(scale.get_units() < -10)
    {Serial.println("Available ");
    }
      else
      {Serial.println("Unavailable ");
      }
     
     
     
}



larryd

Record the value returned by millis() at the first event.
When the second event happens subtract the above value from the current value of millis().

The difference is time in milliseconds.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

trobs3

Hey larryd,

Thank you so much for replying so fast! I understand the logic you presented that the difference in millis() would measure the time, but i am having a really hard time coding this into my sketch.


I dont know where to put it or how to code it so the measured time i find can be reported somewhere else..

would you mind guiding me?

larryd

#3
Aug 22, 2019, 09:36 pm Last Edit: Aug 22, 2019, 09:40 pm by larryd
"Event one: weight off scale leaves (Load Scale reads less than 10 lbs)
Event two: weight comes back onto the scale (Load Scale reads greater than 10 lbs)"

Something similar to this;

if(timingFlag == false && scale.get_units() < -10)
{
  timingFlag = true;
  timingMillis = millis();
}
 
if(timingFlag == true && scale.get_units() >= -10)
{
  Serial.print("Time less than 10 = ");
  timeOff = millis() - timingMillis;
  Serial.println( timeOff );
  timingFlag = false;
}


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

trobs3

so i applied you code and am very grateful for you help! Sorry i have one more question... i know it might be super basic but,

how do i declare timingFlag, timingMillis, and timeOff so it corresponds and reacts with my code?


Seriously thank you so much for all your help!

econjack

These statements give you a hint:

Code: [Select]

  timingFlag   = true;
  timingMillis = millis();


Because timingFlag is set to logic state true, it should be a boolean data type, so its definition becomes:

   bool timingFlag;


Because timingMillis and timeOff are used with the millis() function call (without a cast), they should have the same data type as millis(). Looking up the millis() function as seen here, we see that the return data type is an unsigned long. Therefore, the definitions must be:

Code: [Select]

   unsigned long timingMillis;
   unsigned long timeoff;

trobs3

so here is my updated code:

boolean timingFlag;
timingFlag = true;
unsigned long timeOff;
unsigned long timingMillis;

      if(timingFlag == false && scale.get_units() < -10)
{
  timingFlag = true;
  timingMillis = millis();
}
 

if(timingFlag == true && scale.get_units() >= -10)
{
  Serial.print(" Time less than 10 = ");
  timeOff = millis() - timingMillis;
  Serial.println( timeOff );
  timingFlag = false;
}
}
     
       
It does exactly what i need it to do. It starts measuring time when the weight is below 10 lbs. (WHICH IS AWESOME, THANK YOU GUYS!)

Now what i am trying to figure out is how to use the time measured to determine a a dollar amount in my code...

i am having trouble finding and using the time that was measured when the weight was off the scale..

I want the board to determine every second is worth 20 cents..

any ideas on how i could you the exact time amount the weight was off the scale in seconds and then multiple that by .20 to find the exact dollar amount?



larryd

#7
Aug 23, 2019, 08:29 pm Last Edit: Aug 23, 2019, 09:02 pm by larryd
Make this change:
if(timingFlag == false && scale.get_units() < -10)
{
  timingFlag = true;
  timingMillis = millis();
  timeOff = 0;  // <------<<<<
}

'timeOff' can be used externally in determining when the item is off the scale.

When 'timeOff' is 'not' zero, you know the item was just removed. Then you make the last value of a variable 'lastTimeOff' equal to the new 'timeOff'. You can now do your price calculation.





BTW
Suggest you make this change as there might be an assumption there is initially an item on the scale.

boolean timingFlag;
timingFlag = false; <------<<<<
unsigned long timeOff;




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

IamFof

Tut, tut, larryD.  Not using code tags?

trobs3

LarryD

can you please explain further about timeOff being used externally, i do not fully understand how it use it and find the total time the weight was off the scale for one time frame.

Event one: weight off scale leaves (Load Scale reads less than 10 lbs)
       Time begins to count

Event two: weight comes back onto the scale (Load Scale reads greater than 10 lbs)
        Time stops counting

how do I code in short words: Time stops counting - Time begin count = total time weight was being used?


sorry, if i am being so slow. But i really do appreciate all your guys help!

larryd

#10
Aug 24, 2019, 06:14 am Last Edit: Aug 24, 2019, 06:53 am by larryd
Quote
I want the board to determine every second is worth 20 cents..

any ideas on how i could you the exact time amount the weight was off the scale in seconds and then multiple that by .20 to find the exact dollar amount?
Not really sure what is being explained in your quote up above.

Code: [Select]

//global variables area
float seconds;
float cost;
float runningTotal;

boolean timingFlag = false;
unsigned long timingMillis;
unsigned long timeOff;
. . .

//code area
. . .
if(timingFlag == false && scale.get_units() < -10)
{
  timingFlag = true;
  timingMillis = millis();
}
 
if(timingFlag == true && scale.get_units() >= -10)
{
  Serial.print("Time less than 10 = ");
  timeOff = millis() - timingMillis;
  Serial.println( timeOff );
  timingFlag = false;
}

if(timeOff != 0)
{
  seconds = timeOff / 1000ul;  //milli seconds divided by 1000 is seconds
  cost = seconds * .20;
  Serial.print("Cost = ");
  Serial.println(cost);
  runningTotal = runningTotal + cost;
  Serial.print("Running total = ");
  Serial.println(runningTotal);
  timeOff = 0;
}   
 


You could move the cost code into the timing code.


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

trobs3

these changes have really helped my code out and has almost made my project become what i was hoping it would be, thank you!


if(timeOff != 0)
{
  seconds = timeOff / 1000ul;  //milli seconds divided by 1000 is seconds
  cost = seconds * .00333333;
  Serial.print("Cost = $ ");
  Serial.println(cost);
  runningTotal = runningTotal + cost;
  Serial.print("Running total = ");
  Serial.println(runningTotal);
  timeOff = 0;
}   


one of the problems that i am facing is the running total of cost does not reset after the weight returns on the scale.


I am hoping that each time the weight leaves the scale it will determine the cost based on the time it is off the scale and when it returns it will have the total cost due.

and then once the weight returns to the scale the cost will return to zero and be ready for the weight to be rented again.




My goal for this project is to make an automated rental system. The weight is a piece of equipment. So each time the person rents the equipment the system tracks how long it was rented. When the customer returns the equipment it will tell them how much $ they owe (based on the time they used the equipment) So once the equipment is returned, the system must be ready to track the amount of time the next customer will use the equipment and charge them the correct amount. (This will be an on going repeated processes)

your help has and will be greatly appreciated!

larryd

#12
Aug 24, 2019, 07:38 pm Last Edit: Aug 24, 2019, 07:48 pm by larryd
"one of the problems that i am facing is the running total of cost does not reset after the weight returns on the scale."

All you have to do is reset 'runningTotal' to zero when the weight is back on the scale.
However, if you do this, maybe you don't even need 'runningTotal' in the first place.

Code: [Select]
//global variables area
float seconds;
float cost;

boolean timingFlag = false;
unsigned long timingMillis;
unsigned long timeOff;
. . .

//code area
. . .
if(timingFlag == false && scale.get_units() < -10)
{
  timingFlag = true;
  timingMillis = millis();
}
 
if(timingFlag == true && scale.get_units() >= -10)
{
  Serial.print("Time less than 10 = ");
  timeOff = millis() - timingMillis;
  Serial.println( timeOff );

  seconds = timeOff / 1000ul;  //milli seconds divided by 1000 is seconds
  cost = seconds * .00333333;
  Serial.print("Cost = $ ");
  Serial.println(cost);

  timingFlag = false;
}   


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

trobs3

how would i reset the runningTotal to zero when the weight returns to the scale?

this would be exactly what i need to do!

larryd

#14
Aug 25, 2019, 06:43 am Last Edit: Aug 25, 2019, 06:46 am by larryd
"one of the problems that i am facing is the running total of cost does not reset after the weight returns on the scale. "

how would i reset the runningTotal to zero when the weight returns to the scale?

this would be exactly what i need to do!
if(timingFlag == true && scale.get_units() >= -10)
{
. . .
runningTotal = 0;
}

But more seriously, the last bit of code you where offered in post #12 shows you what to do.

From the comments you have been making, it appears you need to review the basics of C++ (Arduino) programming.

There are many basic examples that are in the examples of the IDE.

You need to stop your current project and review these to the point where you fully understand them.

Only then should you start up your project again.






No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Go Up