Exiting a loop though a timer (?)

Hi there,
I' ve a small problem making some timer-based code. Let me explain: I' ve an Arduino mega board and I would like to receive and sample some pwm signals (I' m going to use pulseIn) so storage in two variables the min&max values I get. I' ve written this really simple code

for(int i; i<50 ; i++){
Input = pulseIn(channel, HIGH, TIMOUTPWM);
*Input_min = min(*Input_min, Input);
*Input_max = max(*Input_max, Input);
Serial.print('.');
delay(50);//100
}

So here, how you can see, the pulseIn function is looped 50 times. That' s the matter: I would like to get and sample the pulses, -> storage the min and max value and if these values don' t change for a time (for example 10 seconds) then exit the loop... I really don' t know how I could compare Input_min while time is going forward. It might be a stupid question, but please help me! :slight_smile:
Thank you,
Pentium

Pentium:
Hi there,
I' ve a small problem making some timer-based code. Let me explain: I' ve an Arduino mega board and I would like to receive and sample some pwm signals (I' m going to use pulseIn) so storage in two variables the min&max values I get. I' ve written this really simple code

for(int i; i<50 ; i++){
Input = pulseIn(channel, HIGH, TIMOUTPWM);
*Input_min = min(*Input_min, Input);
*Input_max = max(*Input_max, Input);
Serial.print('.');
delay(50);//100
}

So here, how you can see, the pulseIn function is looped 50 times. That' s the matter: I would like to get and sample the pulses, -> storage the min and max value and if these values don' t change for a time (for example 10 seconds) then exit the loop... I really don' t know how I could compare Input_min while time is going forward. It might be a stupid question, but please help me! :slight_smile:
Thank you,
Pentium

You want to look into "interrupt-driven" design.

If using your design, I would have an extra couple of variables. I would store the "last changed" min and max, and compare those with the incoming values each iteration. If they differ, then set a third variable to the current time and set the "last changed" min and max to the current min and max.

Then I compare the "last changed" time with the current time, and if the difference is greater than or equal to 10,000 (10 seconds) then either set i to be more than the limit of the loop, or execute a "break" instruction to drop out of the loop.

You want to look into "interrupt-driven" design.

Or you could do it the simple way, with a timeout (use "millis()") and a "break"

majenko:
If using your design, I would have an extra couple of variables. I would store the "last changed" min and max, and compare those with the incoming values each iteration. If they differ, then set a third variable to the current time and set the "last changed" min and max to the current min and max.

Then I compare the "last changed" time with the current time, and if the difference is greater than or equal to 10,000 (10 seconds) then either set i to be more than the limit of the loop, or execute a "break" instruction to drop out of the loop.

Mmm...how can I store the "last changed"? I mean, that value should be stored in the loop cycle so it would be always equals with the incoming value. I' m a bit confused ahah

Mmm...how can I store the "last changed"? I mean, that value should be stored in the loop cycle so it would be always equals with the incoming value. I' m a bit confused ahah

So store it outside the loop :stuck_out_tongue:

int lastMin,lastMax;
unsigned long lastChangeTime;

for(int i; i<50 ; i++){
    Input = pulseIn(channel, HIGH, TIMOUTPWM);
    *Input_min = min(*Input_min, Input);
    *Input_max = max(*Input_max, Input);
    Serial.print('.');

    if((*Input_min != lastMin) || (*Input_max != lastMax))
    {
      lastMin = *Input_min;
      lastMax = *Input_max;
      lastChangeTime = millis();
    }

    if(millis() - lastChangeTime > 10000)
    {
      break;
    }

    delay(50);//100
  }

That is remembering the min and max only when they change. At the time they change it sets a timestamp. Then, on every iteration, it checks the difference between the timestamp and the current time. If it exceeds a certain timeout (10,000 ms), then exit the loop.

Now it' s all clear! Really really Thank you a lot :slight_smile: