Measuring time between two inputs

I am designing an fatigue life tester, which consists of an arm that fluctuates between two points. At each point is a sensor telling the Arduino bord to move the arm to the other point. The problem I am sitting with is to determine when my part breaks which could be at any unknown time. I know that once the part breaks the arm will be allowed to move much faster and therefore will have a quicker cycle time. So my first though was to get the time it takes to complete one cycle and compare that to the previous cycle or to a given time value. And if those times differ by a significant amount I want to the code to stop executing and store the amount of cycles it has completed.

So far I got the counter to work, but I am stuck on the time issue, and I have read every forum post that I could find as well as all the YouTube videos but still I can't find a solution to my problem. Does any body have any suggestions or solutions? Please also see my code that takes the two inputs from my sensors and count whenever it reaches sensor1 and outputs a signal which moves my arm.

int sensor1 = 2;
int sensor2 = 3;
int relay = 4;
int counter = 0;
bool statetriggered = false;
bool previousstate = false;
bool currentstate = false;

void setup() {
Serial.begin(9600);

pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(relay,OUTPUT);

digitalWrite(relay,LOW); //dont fill piston until bottom sensor is triggered

//this is to compare the previous state of the sensor with the current state, if it is different,
//the sensor was triggered and the counter should be incremented.
previousstate = false;
currentstate = false;

}

void loop() {
  // get the current state of sensor 1
  currentstate = digitalRead(relay);

  if (digitalRead(sensor1)==HIGH) {
    digitalWrite(relay,LOW);
  
  }

  if (digitalRead(sensor2)==HIGH) {
    digitalWrite(relay,HIGH);
  }

  if (currentstate != previousstate){
    if (currentstate==LOW) {
    counter++;
    Serial.println(counter);
    }
  }

  // the previous state of sensor1 in the next loop will be the last known state at the end of the current loop
  previousstate = currentstate;
}

To compute the time in milliseconds between state changes on two inputs, save the value of millis() when each input changes state, and subtract the smaller number from the larger.

1 Like

Save the value of millis() as the start time when a cycle starts. Save the value of millis() when the cycle ends. The difference between the 2 values will give you the cycle time

Before the next cycle starts save the cycle time as the previous cycle time

Keep going until the current cycle time differs significantly from the previous time

1 Like

Add a line like:

sensor1_time = millis();

etc.

What is the plan with currentState and previousState? Are you going to change currentState on with one button and off with the other, so you can report one duration per cycle?

1 Like

Cross post to Read time between rise on 3 separate input pins

1 Like

I have tried something like this now, the values I get for time and time1 are in milliseconds and I can see they increase as the the time elapse since the code started running, but the diff variable that I created keeps fluctuating around a relatively large number which I can't think is in milliseconds, does anyone know why it's so big?

int sensor1 = 2;
int sensor2 = 3;
int relay = 4;
int counter = 0;
bool statetriggered = false;
bool previousstate = false;
bool currentstate = false;
unsigned long time; unsigned long time1; unsigned long diff = 4;
void setup() {
Serial.begin(9600);

pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(relay,OUTPUT);

digitalWrite(relay,LOW); //dont fill piston until bottom sensor is triggered

//this is to compare the previous state of the sensor with the current state, if it is different,
//the sensor was triggered and the counter should be incremented.
previousstate = false;
currentstate = false;

}

void loop() {
  // get the current state of sensor 1
  currentstate = digitalRead(relay);

  if (digitalRead(sensor1)==HIGH) {
    digitalWrite(relay,LOW);
  
  }

  if (digitalRead(sensor2)==HIGH) {
    digitalWrite(relay,HIGH);
  }

  if (currentstate != previousstate){
    if (currentstate==LOW) {
    
    counter++;
    Serial.println(counter);
    time = millis();
    diff = time1 - time;
    time1 = time;
    Serial.println(diff);
    }
    
  }

  // the previous state of sensor1 in the next loop will be the last known state at the end of the current loop
  previousstate = currentstate;
}

And no we are not busy with the same project I work for a compony that produces most of it's parts in house and therefore would like to test our parts the way they are going to be used in the field.

'time1 will always be less or equal to 'time', so this difference should always be non-positive (which gets interpreted as 2^32 - value. ) Maybe you should interpret the difference as a signed number by changing the declaration to signed long:

long diff = 4;

or switch the direction of the difference:

diff = time - time1;

How long is a cycle? right now you are doing falling edge detection on your relay output, which you lower at sensor 1 HIGH and raise at sensor 2 HIGH.

Ah thank you very much it now works perfectly.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.