Sensor Timer/ Serial Monitor!!

Hi, I have been working on this project for a couple months now, I have a code that runs Sensor, What I having problems with is A timer. What it does is, Times from start to end and prints the time in serial Monitor. What I needing help with is when the sensor get flagged I need it Start a timer until it get to the next sensor then I need it to Display the time in sensor Monitor, I have it to where it will do that but as long as that sensor is flag it keeps printing lines of time every (100) Ms, and that the same as the start Sensor, I only need the time printed one time even if the Sensor is Stays Flagged, But is I put delays in it with Mess with other Sensor and motors on my Machine. If anyone could help it be greatly Appreciated.

Arron12312:
I have it to where it will do that

So, please enlighten us with the code :slight_smile:

Hint, state change :slight_smile:

 void displayResult()

  float h,m,s,ms;
  unsigned long over;
  elapsed=finished-start;
  h=int(elapsed/3600000);
  over=elapsed%3600000;
  m=int(over/60000);
  over=over%60000;
  s=int(over/1000);
  ms=over%1000;
  Serial.print("Raw elapsed time: ");
  Serial.println(elapsed);
  Serial.print("Elapsed time: ");
  Serial.print(h,0);
  Serial.print("h ");
  Serial.print(m,0);
  Serial.print("m ");
  Serial.print(s,0);
  Serial.print("s ");
  Serial.print(ms,0);
  Serial.println("ms");
  Serial.println();
 if (digitalRead(7)==HIGH)
  {
    start=millis();
    delay(100); // for debounce
  }
  if (digitalRead(8)==HIGH)
  {
    finished=millis();
    delay(100); // for debounce
    displayResult();
  }

I have more of the code if there more parts you may need to see

What

the

heck

is

up

with

all

the

blank

lines???

Arron12312:
I have more of the code if there more parts you may need to see

Normally I would say, yes, all of it. Because that's the forum rule :wink: But I spotted the error:

 if (digitalRead(8)==HIGH)

When is that true? Aka, when will it run displayResult?
Then, look at my hint :wink:

Couple of weird things I already notice:

  • Why on earth are h, m, s and ms floats????

  • Spaces are free and do make your code look better

  • So do comments :wink:

delay(100); // for debounce

That is a very ugly way of doing it. Especially because you want to time something up to the ms.

Alright After the delay, I need to add in A digital Write Pin 8 Low? So it will only count it one time, and on the code I'm Still lost on What error are in it, other then space, is there a better way to write it with out Float?

Arron12312:
Alright After the delay, I need to add in A digital Write Pin 8 Low?

But pin 8 is used as in input... So driving it high probably isn't a good idea :wink:

Did you look for state change? Did you notice the IDE has an example called state change detection?

Arron12312:
is there a better way to write it with out Float?

What about a byte? Non of them is going to be signed or bigger then 255.

I gotta you now, I see what you mean, But I'm Using 2 different inputs so would it matter with in the state change?

I'm sorry, I don't get what you mean. What about the two inputs?

You need to understand the difference between an input being high (that's a state) and an input becoming high (change of state / an event). Because remember, the loop() will continue to loop (fast!).

Right, What im trying to say is, Sensor B will start the timer and then when Sensor C gets flags and will display the time it took to get from B to C, If I use a state change, want that make the timer just keep starting every loop until it get to C sensor, then it will Print 100s of different times?

No, you start the time when B changes (to HIGH or to LOW, whatever you want) and you stop when C changes (to HIGH or to LOW).

I'm sorry but im still lost, I get state change, but i'm lost to how I can get it work with my code, I don't get how I get more then one input to start it and stop it, I can get it work with one sensor but I don't get how to make it work with more the one?

 if (digitalRead(7)==HIGH){
    start=millis();
    delay(10); // for debounce
    Serial.println("Started...");
  }  
   if (digitalRead(8) == HIGH){
       finished=millis();
       delay(10); // for debounce
       displayResult();
   }
const int LiftSensor = 7;
const int MainBrakeSensor = 8;
if (digitalRead(7)==HIGH){
    start=millis();
    delay(10); // for debounce
    Serial.println("Started...");
  }

Why then don't you use state change detection? Because that will run as long as pin 7 is HIGH...

I could use state change, but I don't know how to set that up with 2 different inputs, I can't find anything online to get more then one input as the on and off, I don't think it will mess with the rest of my code or machine if the sensor 7 stays high, did you get my code I post?

Yes it does, because it will keep setting the start variable.

And if you get state change, what is hard to duplicate that for two variables? So just have two variables to hold the last state..

First, cross posting is very rude.

Second, why can't we just keep everything nice and public here? A PM from him:

Arron12312:

f (InputState2 != LastInputState){

if (InputState2 == HIGH){
   start=millis();
   delay(10); // for debounce
   Serial.println("Started...");
   
  if (InputState3 != LastInputState){
   if(InputState3 == HIGH){
      finished=millis();
      delay(10); // for debounce
      displayResult();

LastInputState = InputState2 & InputState3;
   }
  }
}
 }




Alright so this can't be right, it's not displaying time now?

And of course that doesn't work. Like I said:

septillion:
what is hard to duplicate that for two variables**?**
[/quote]
How do you think you can store the previous state of two booleans into a single boolean?
And now it's time to post compilable code. I already mentioned in the starting post it's not Snippets R Us around here. It doesn't need to be your full program is it's part of a huge program but at least make a simple sketch that can compile (or has the same error) that demonstrates the same problem.

@Arron12312, do not cross-post. Threads merged.

I appreciate all you help but I just don't understand, how to put it in th code. Thank you anyway.

septillion:
And now it's time to post compilable code.

:wink:

And a hint (because that's all you give us) use two variables :wink:

Oh wait so you mean like lastSensor1 and last Sensor2 and set them both to LOW or 0?

I'm out, sorry. Good luck!