hi everyone how to count the time

long currentMillis  ;                    // start time for stop watch
long previousMillis ;
long elapsedMillis ;
int preincrementation = 8;
int vrai = 9;


float prevtension;// global variables are retained on each iteration of loop()
float 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  / elapsedMillis;
  long initime = 0.05 * elapsedMillis;
  long RPMconsigne = 3000;
  long consigne = 60000000 / RPMconsigne;
long petit = consigne - (consigne * 0.02);
long grand = consigne + (consigne * 0.02);



  if (tension < 2.8)
  {
    if (tension < (prevtension - 0.005)) {  // compare them
      currentMillis = micros(); //end point of time measurement
      elapsedMillis = currentMillis - previousMillis;
      previousMillis = micros(); // starting point
      digitalWrite(led, HIGH); 

      if(elapsedMillis > consigne)
      {
        time = initime;
      } 

      if(elapsedMillis < consigne)
      {
        time = 3;
        if ( petit < elapsedMillis < grand )
          digitalWrite (preincrementation, HIGH);
        { 
          if ( elapsedMillis < consigne )
          {

            if ( elapsedMillis > (consigne - (consigne * 0.0001)) )
            {
              
              time += 0.1;
            }
            if ( elapsedMillis < (consigne + (consigne * 0.0001)) )
            {
            
              time -= 0.1;
            }

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

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

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

  Serial.print("elapsedMillis = ");
  Serial.println(elapsedMillis);
  Serial.println();


Serial.print("consigne = ");
  Serial.println(consigne);
Serial.print("grand = ");
  Serial.println(grand);
Serial.print("petit = ");
  Serial.println(petit);
Serial.println();



  Serial.print("currentMillis = ");
  Serial.println(currentMillis);
  Serial.print("previousMillis = ");
  Serial.println(previousMillis);
  Serial.println();



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

  Serial.print("initime = ");
  Serial.println(initime);
  Serial.print("time = ");
  Serial.println(time);
  Serial.println();

  Serial.print("tension = ");
  Serial.println(tension);


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

}

time between my high to next high because this code is not working thank you

Hello!
Could you please attach a image/ code for the error code?

kind regards,
-Goldfile

its is only not giving the good elapsed time
thank you

long currentMillis  ;                    // start time for stop watch
long previousMillis ;
long elapsedMillis ;
int preincrementation = 8;
int vrai = 9;
long duration1;
long duration2;
float prevtension;// global variables are retained on each iteration of loop()
float time;
int valeurLue;

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

void setup() {
  pinMode(analogPin, INPUT);
  pinMode( led, OUTPUT ); 
  digitalWrite(led, HIGH); 
  delay (1000);
  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  / elapsedMillis;
  long initime = 0.05 * elapsedMillis;
  long RPMconsigne = 3000;
  long consigne = 60000000 / RPMconsigne;
  long petit = consigne - (consigne * 0.02);
  long grand = consigne + (consigne * 0.02);


  if (tension < 2.55)
  {
    if (tension < (prevtension - 0.005)) {  // compare them

     
      long elapsed1 = micros()  - startTime;   
      long elapsed2 = micros()  - endTime; 
elapsedMillis = endTime + startTime;  
      digitalWrite(led, HIGH); 
      startTime = micros();
      if(elapsedMillis > consigne)
      
        delayMicroseconds (initime);
        digitalWrite(led, LOW); // do something, they are different
endTime = micros();

        {
        } 
      }
    }
  

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

  Serial.print("elapsedMillis = ");
  Serial.println(elapsedMillis);
  Serial.print("Start = ");
  Serial.println(startTime);
  Serial.print("Stop = ");
  Serial.println(endtime);
  Serial.println();


  Serial.print("consigne = ");
  Serial.println(consigne);
  Serial.print("grand = ");
  Serial.println(grand);
  Serial.print("petit = ");
  Serial.println(petit);
  Serial.println();



  Serial.print("currentMillis = ");
  Serial.println(currentMillis);
  Serial.print("previousMillis = ");
  Serial.println(previousMillis);
  Serial.println();



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

  Serial.print("initime = ");
  Serial.println(initime);
  Serial.print("time = ");
  Serial.println(time);
  Serial.println();

  Serial.print("tension = ");
  Serial.println(tension);


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

}

how to calculate the elapsed time

(the time now) - (the time you started)

Questions:

  1. You define elapsedMillis as a global, and then use it in
long RPMactuel = 60000000  / elapsedMillis;

but never assign anything into it until about a dozen lines later. Dividing 60000000 by zero will probably
overflow a long.

  1. In the code:
  long RPMconsigne = 3000;
  long consigne = 60000000 / RPMconsigne;

RPMconsigne is never used again, so why bother? Why not just assign consigne = 20000 and be
done with it? Indeed, you could do the same factoring on petit and grand.

      currentMillis = micros(); //end point of time measurement

I quit reading right here. currentMillis is a stupid name. The name should reflect the event of interest. Why are you interested in now?

It is even stupider when the value being stored in it is not the time in milliseconds.

I know that you've been asked to use Tools + Auto Format before posting code. DO NOT POST AGAIN IF YOU CAN"T DO THAT!