how to time the time between tow events
int led = 13; // Alimentation de la bobine
int hall = A0; // Analyse du capteur a effet Hall
long startTime ; // start time for stop watch
long elapsedTime = 0 ;
unsigned long valeur; //Donnees directes du capteru a effet Hall
float tension; // Valeur actuelle en voltage du capteur
float pretension; // Valeur precedente en voltage du capteur
void setup() // Effectuer une seule fois
{
pinMode (hall, INPUT); // Declarer capteur a effet Hall comme INPUT
pinMode (led, OUTPUT); // Declarer led comme OUTPUT
Serial.begin (38400); // Vitesse du retour des donnees
}
void loop() // Effectuer à chaque fois
{
pretension = tension; // Tension precedente vs tension actuelle
valeur = analogRead (hall); // Lecture du capteur a effet Hall
tension = valeur * 5.0 / 1024; // Formule de calcul du voltage du capteur a effet Hall
float initime = 0.1 * elapsedTime;
if (tension < 2.55)
{if (tension < (pretension - 0.005)) { // compare them
elapsedTime = micros() - startTime;
Serial.print("valeur= "); // Imprimer les valeurs pecedentes du capteur
Serial.println(elapsedTime);
startTime = micros();
digitalWrite(led, HIGH);
delayMicroseconds (initime);
digitalWrite(led, LOW); // do something, they are different
{if (tension > 2.54 && tension < (pretension - 0.005)) {digitalWrite(led, LOW);}
} }
}
Serial.print("initime= "); // Imprimer les valeurs pecedentes du capteur
Serial.println(initime);
Serial.println(); // Sauter une ligne lors de l'affichage
Serial.println();
}
the elapsedTime is wrong why
float initime = 0.1 * elapsedTime; //elapsedTime is 0 so what math you want to happen here?
if (tension < 2.55)
{if (tension < (pretension - 0.005)) { // compare them
elapsedTime = micros() - startTime;
startTime is undefined
In your code you never set the startTime, you had just declared it
Shouldn't you be using millis() to get the time ?
oh thats right but actually i want him to calculate the time in microseconds but the time is too short and not long as its suposed to be
i want him to calculate the time in microseconds but the time is too short and not long as its suposed to be
You should use micros() to get the microseconds since your program is running.
See the example of micros()
unsigned long time;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = micros();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
thank you very much so know it calculate the time of a loop
but does the time initialize
but does the time initialize
What you mean by that?
does the time restart
Jhjh:
does the time restart
Yes, or possibly, no.
how to make him restart
Why would you want to?
to restart so at every loop it another value I dont want him to addition them.
You keep repeating the same gibberish.
Stop that too
The difference between a later and an earlier time is the elapsed time.
yeah ok but those the value reset at every loop
Can you please stop the knee-jerk responses?
Engage brain and start working through the examples in the IDE
Start to think and ask yourself "Why would I want to reset the clock?"
If you don't stop asking random questions, I'm going to give you a time-out to give you time to think.
Here is my code;
long startTime ; // start time for stop watch
long elapsedTime ;
float prevtension;// global variables are retained on each iteration of loop()
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 (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;
if (tension < 2.55)
{if (tension < prevtension ) {
elapsedTime = micros() - startTime;
startTime = micros();
digitalWrite(led, HIGH);
delay (20);
digitalWrite(led, LOW); // do something, they are different
}
}
Serial.print("elapsedTime = ");
Serial.println(elapsedTime);
Serial.print("previoust= ");
Serial.println(prevtension);
Serial.print("current= ");
Serial.println(tension);
Serial.println();
Serial.println();
}
but i want that the elapsed time reset every loop please and the problem is that its additionning the values
And the problem is that its not reseting the counter
You're printing the elapsed time whether or not it has changed.
Only print the elepased time if you've calculated a new value.
ok so it immediatly reset it right