Hello All!
I am trying to measure the time when the sensor level is above/below threshold, namely HT.
I have put the code together, but somehow I am stuck with using it within the main loop.
For example, if I use the if () to check the timings, I have a problem with the fact that timing of above HT is 0 when it starts running... Not sure how to go beyond this problem, except with the force, see the code below I am printing out the values of the functions and it seems to be ok... Anyway, please have a look at the code, and let me know what do you think about it.
Here is my serial print out:
Sensor set!
TAHT: 0
TBHT: 909
TAHT: 73
TBHT: 1064
ledState: 1
TAHT: 57
TBHT: 1393
ledState: 0
TAHT: 91
TBHT: 851
ledState: 1
TAHT: 74
TBHT: 922
ledState: 0
TAHT: 98
TBHT: 631
ledState: 1
Best.
void loop()
static unsigned long aboveHT;
static unsigned long belowHT;
aboveHT = timeAboveHT();
//forcing to start
if(aboveHT==0){
aboveHT=50;
}
belowHT = timeBelowHT();
Serial.print(" TAHT: "); Serial.println(aboveHT);
Serial.print(" TBHT: "); Serial.println(belowHT);
if (aboveHT > 20 && aboveHT < 100 && belowHT > 500) {
ledState = !ledState;
Serial.print("ledState: "); Serial.println(ledState);
}
Function that gives me time in ms
// ok time above ht
unsigned long timeAboveHT() {
//measure time while above HT
if (Sensor.readProximity() > HT) {
startTime = millis();
elapsedTime = 0;
//if (startTime > 150) {
while (Sensor.readProximity() > HT) {
currentTime = millis();
elapsedTime = currentTime - startTime;
}
//return elapsedTime;
}
//Serial.print("t A HT: "); Serial.println(elapsedTime);
return elapsedTime;
}
// ok time below ht
// you can define the lenght of the time to close the window, namely room
unsigned long timeBelowHT() {
//measure time while below HT
if (Sensor.readProximity() < HT) {
elapsedTime = 0;
startTime = millis();
while (Sensor.readProximity() < HT) {
currentTime = millis();
elapsedTime = currentTime - startTime;
}
}
return elapsedTime;
}