how to calculate the time between two loops

long startTime ;                    // start time for stop watch
long elapsedTime ;
int preincrementation = 8;
int vrai = 9;


float prevtension;// global variables are retained on each iteration of loop()
long time;
int valeurLue;

int analogPin = A0;
const int led =  13; 
float tension; //variable pour stocker la valeur lue après conversion


void setup() {
  pinMode(analogPin, INPUT);
  pinMode( led, OUTPUT ); 
  digitalWrite(led, HIGH); 
  delay (500);
  digitalWrite(led, LOW); // 2.5cm
  Serial.begin(115200);

}



void loop() {
  prevtension = tension;

  valeurLue = analogRead(analogPin);

  //on traduit la valeur brute en tension (produit en croix)
  tension = valeurLue * 5.0 / 1024;
  long RPMactuel = 60000000  / elapsedTime;
  long initime = 0.3 * elapsedTime;
  long RPMconsigne = 3000;
  long consigne = 60000000 / RPMconsigne;

  if (tension < 2.8)
  {
    if (tension < (prevtension - 0.005)) {  // compare them
      elapsedTime =   micros() - startTime;   
      startTime = micros(); 
      digitalWrite(led, HIGH); 

      if(elapsedTime > consigne)
      {
        time = initime;
      } // 4%

      if(elapsedTime < consigne)
      {
        time = 4;
        if ( (consigne - (consigne * 0.02)) < elapsedTime < (consigne + (consigne * 0.02)))
          digitalWrite (preincrementation, HIGH);
        { 
          if ( elapsedTime < consigne )
          {

            if ( elapsedTime > (consigne - (consigne * 0.0001)) )
            {
              time = 4;
              time += 0.001;
            }
            if ( elapsedTime < (consigne + (consigne * 0.0001)) )
            {
              time = 4;
              time -= 0.001;
            }

            if ((consigne - (consigne * 0.0001)) < elapsedTime < (consigne + (consigne * 0.0001)))
            {
              digitalWrite (vrai, HIGH);
            }
          }   
        }

        delayMicroseconds (time);
        digitalWrite(led, LOW); // do something, they are different

      }
    }
  }



  Serial.print("elapsedTime = ");
  Serial.println(elapsedTime);
  Serial.print("consigne = ");
  Serial.println(consigne);
  Serial.print("rpm actuel = ");
  Serial.println(RPMactuel);
  Serial.print("rpm consigne = ");
  Serial.println(RPMconsigne);
  Serial.print("initime = ");
  Serial.println(initime);
  Serial.print("time = ");
  Serial.println(time);




  Serial.println();
  Serial.println();

}

I want to calculate the time from digitalWrite(led, HIGH); to next loop digitalWrite(led, HIGH);
thank you everyone

On a UNO, 18.4ms "mostly" however now in then it is 9.3ms and sometimes 27.8ms (using a scope).
You could use the profile timer HERE to measure it. Maybe.

Jhjh:

long startTime ;                    // start time for stop watch

long elapsedTime ;

elapsedTime =   micros() - startTime;   
      startTime = micros();
      digitalWrite(led, HIGH);



I want to calculate the time from `digitalWrite(led, HIGH);` to next loop `digitalWrite(led, HIGH);`
thank you everyone

The code above seems to be doing that for you. Isn't it working? You should be using unsigned long variables to hold time values but apart from that I don't see anything wrong with what you have.

yeah but the problem is that is see that the timer stops at the end of the loop
But I want him to calculate from HIGH to next HIGH
so when its low it have to still counting the time. until it's HIGH

The logic is a bit ugly, but I suspect the problem is that the code inside the if block (lines 43 - 83) executes multiple times while the LED output remains high; I suspect you only want it to execute once.

It would be easy to add code to do that, but the existing code is already pretty complex and I have a strong suspicion that you could actually get rid of a lot of that complexity if you were clearer about what behaviour you're trying to produce. Do you want to add an extra layer of complexity to the existing code, or look for ways to simplify it?

hi everyone I have a problem the same as jhjh
please help us we are in the same class

You want us to do your assignment? Why don't you work together?

we blocked here since 2 weeks we have read all the sites and need your help for this only question please

http://forum.arduino.cc/index.php?topic=213595.0

Well, that's not entirely true. jhjh has made 89 posts, many of which are essentially the same question asked a second (third?) time. My guess is that all 89 have occurred during the current semester. This forum should be where you come when there's blood running between your eyes from banging your head against the wall, your hands are so cramped from trying different code that you can't pick up a Jolt Cola, and your eyes scream from squinting at example code. Methinks you two haven't been there yet.

econjack:
Well, that's not entirely true. jhjh has made 89 posts, many of which are essentially the same question asked a second (third?) time. My guess is that all 89 have occurred during the current semester. This forum should be where you come when there's blood running between your eyes from banging your head against the wall, your hands are so cramped from trying different code that you can't pick up a Jolt Cola, and your eyes scream from squinting at example code. Methinks you two haven't been there yet.

Well, I'd encourage people to come here just a bit earlier than that! 8)
But, yeah, you two need to try something. Create a sketch that does something observable in loop(), like turn an LED on, wait, turn the LED off, and wait again.

Get that working. Then, note what time it is when you enter loop() AFTER copying the previous time to another global variable.

The time it takes for one iteration of loop() is then the difference between now and then.