system
January 19, 2014, 5:36am
1
How to get the time between 2 events please
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(38400);
}
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 - 0.005)) { // compare them
digitalWrite(led, HIGH);
delay (20);
digitalWrite(led, LOW); // do something, they are different
{if (tension > 2.54 && tension < (prevtension - 0.005)) {digitalWrite(led, LOW);}
} }
}
Serial.print("elapsedTime = ");
Serial.println(elapsedTime);
Serial.print("previoust= ");
Serial.println(prevtension);
Serial.print("current= ");
Serial.println(tension);
Serial.println();
Serial.println();
}
I want to get the time from HIGH to next HIGH please
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
Use millis() . Should use unsigned long)
unsigned long startTime ; // start time for stop watch
unsigned long elapsedTime ;
// then start clock with:
startTime = millis();
// and later on:
elapsedTime = millis() - startTime;
system
January 19, 2014, 5:44am
3
but on the code wrotten before how to take the time from the HIGH to next High like wher shoud I insert these lines.
thanks
Perhaps something like this. See the lines marked //<<<<<<<<<<<<<<<<<<<<<
void setup() {
pinMode(analogPin, INPUT);
pinMode( led, OUTPUT );
digitalWrite(led, HIGH);
startTime = millis(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
delay (1000);
digitalWrite(led, LOW); // 2.5cm
Serial.begin(38400);
}
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 - 0.005)) { // compare them
digitalWrite(led, HIGH);
elapsedTime = millis() - startTime; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// perhaps display it here with a Serial.print.... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
startTime = millis(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< reset for next one
delay (20);
digitalWrite(led, LOW); // do something, they are different
{if (tension > 2.54 && tension < (prevtension - 0.005)) {digitalWrite(led, LOW);}
} }
}
system
January 19, 2014, 6:13am
5
how you tell to reset it?
thanks
Jhjh:
how you tell to reset it?
thanks
Don't forget that millis() is changing all the time.... so
startTime = millis(); //<<<<<<<<<<<<< reset for next one
... puts a "new" millis value into startTime, effectively resetting it. It now comtains the new "high time"