initialize millis if condition is true

is their a way to initialize a variable if condition is true

if(value==TRUE)
{
myVariable = 12345; //initialize myVariable
}

but it<s not working with millis why

  startTp = millis(); 
  pretension = tension; // Tension precedente vs tension actuelle
  valeur = analogRead (capteur); // Lecture du capteur a effet capteur
  tension = valeur * 5.0 / 1024; // Formule de calcul du voltage du capteur a effet capteur


  while ( tension == pretension )
  {  
    pretension = tension; // Tension precedente vs tension actuelle
    valeur = analogRead (capteur); // Lecture du capteur a effet capteur
    tension = valeur * 5.0 / 1024; // Formule de calcul du voltage du capteur a effet capteur
    
    if (startTp < 3000)
    {
      digitalWrite(bobine, LOW);
    }
    else {
     
     digitalWrite(bobine, HIGH); 
      delay (50);
      digitalWrite(bobine, LOW);
      delay (300);
       startTp = 0;
      
    }
  }

We usually suggest you place Serial.print at strategic locations in your code to help you debug your code.
You then use the serial monitor to see if that part of the ever gets executed and you can see the values of variables when the Serial prints are executed.

For example:
pretension = tension; // Tension precedente vs tension actuelle
valeur = analogRead (capteur); // Lecture du capteur a effet capteur
tension = valeur * 5.0 / 1024; // Formule de calcul du voltage du capteur a effet capteur
Serial.println("the code executed to this point and the value of tension was ");
Serial.println(tension);

millis() keeps incrementing until it overflows every ~50days so it will only be < 3000 for 3 seconds every 50 days.

Jhjh:
but it<s not working with millis why

As LarryD said, millis() is the time since the arduino was powered up, so after three seconds if (startTp < 3000) will always be false.
What you need is:

 if (millis() - startTp => 3000){  //has 3 seconds elapsed?
 startTp = millis(); //reset for next time
//rest of your code here
}
else{ .........//3 seconds has not elapsed
}

Jhjh:
is their a way to initialize a variable if condition is true

but it<s not working with millis why

Well firstly you can’t initialise millis becasue millis() is a function which returns the time since last reset. So not sure what you mean there.

Or, did you mean this line, where you maybe think you can’t intialise startTp using the millis() function?

startTp = millis();

Well as suggested above, put a Serial.print just below that and see what the value is…

startTp = millis();
Serial.print("startTp= ");
Serial.println(startTp);

What makes you think the initialisation has not happened?

I'm thinking of suggesting to the forum authorities that the OP get his or her own board in the forum so that all the zillion posts are together as one subject....

Jhjh
You probable don't want to take this advice but, you should go through all the basic sketches offered with the IDE.
Once you fully understand all of them, then and only then, go back to your project and proceed with it.
IMO

LarryD:
Jhjh
You probable don't want to take this advice but, you should go through all the basic sketches offered with the IDE.
Once you fully understand all of them, then and only then, go back to your project and proceed with it.
IMO

I'll second that motion

Think you need something like

while ( tension == pretension )
{  
  pretension = tension; // Tension precedente vs tension actuelle
  valeur = analogRead (capteur); // Lecture du capteur a effet capteur
  tension = valeur * 5.0 / 1024; // Formule de calcul du voltage du capteur a effet capteur

  if (millis() - startTp < 3000)
  {
    digitalWrite(bobine, LOW);
  }
  else 
  {
    digitalWrite(bobine, HIGH); 
    delay (50);
    digitalWrite(bobine, LOW);
    delay (300);                                    
    startTp = millis();
  }
}

this has still an unusual construct

while ( tension == pretension )
{  
  pretension = tension; // Tension precedente vs tension actuelle
..

while the value of tension and pretension are the same I still assign one to the other …
makes only sense if tension is volatile.