How to time between two events

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();

}

Hi everyone first thank you for reading this message.

This is my code but the problem is that I want that my code count the time since the led is HIGH to the next led HIGH.
please help thank you
John

Changing your user name is not going to get you more responses. Frankly, you need to put the Arduino away. Take a programming class. Learn to develop and debug programs on your own. Learn to read books. Once you know something about programming, you can come back to the Arduino. By then, you'll know how to read the reference page, put in Serial.print() statements, and understand what the code is doing.

Am sorry !?!?!

PaulS:
Changing your user name is not going to get you more responses. Frankly, you need to put the Arduino away. Take a programming class. Learn to develop and debug programs on your own. Learn to read books. Once you know something about programming, you can come back to the Arduino. By then, you'll know how to read the reference page, put in Serial.print() statements, and understand what the code is doing.

what are you talking about am new on this forum and I think there is a mistake.
My name is John Drewan it's my first day on arduino forum so please can you help me?
thank you !

It is your code.
http://forum.arduino.cc/index.php?topic=214205.0
Are you in the same class?

john191498:
what are you talking about am new on this forum and I think there is a mistake.

In that case, @Jhjh is going to be seriously angry with you for using his internet connection and stealing his source code.

My name is John Drewan it's my first day on arduino forum so please can you help me?

Given the fact that your code is identical to @Jhjh's code (how to calculate the time between two loops - Programming Questions - Arduino Forum) I suggest, at least while that other thread is active, you follow and participate in that thread to make best use of your time and the other forum members' time.

Thread locked. Replies go here... how to calculate the time between two loops - Programming Questions - Arduino Forum