counting total time problem

so i need to count the total time for a sensor reading.

i need to count how long it takes for the sensor to do a reading twice.

so i did this

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );      // variable for reading the pushbutton status
unsigned long tstart,tend,ttime,brt,time;
int senspin = 3;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();

}

void loop() {
  time = millis();
  int sensread = digitalRead(senspin);
  if (sensread ==  HIGH){
    delay(1000);
    brt++;

     lcd.clear();
     lcd.print("total brt: ");
     lcd.print(brt);
     delay(1000);
  }

  if (brt == 1){
  tstart = millis();
  Serial.println(tstart/1000);
  }

  if (brt == 2){
    brt = 0;
    tend = millis();
    Serial.println(tend/1000);
    ttime = (tend - tstart)/1000;
    lcd.clear();
  
   
   lcd.print("total time: ");
  lcd.print(ttime);
  delay(5000);
  }

the problem is the total time is always 2.
but when i change the brt == 3 or 4 or bigger, the total time is always correct.

any idea when brt == 2 is always giving me the total time = 2?

thanks

i need to count how long it takes for the sensor to do a reading twice.

not sure what you are exactly trying to do and measuring time it takes when you have delays() in your code is not the best idea.

why don't you do just something like (assuming readSensor() is the function returning a reading)

unsigned long t0 = micros();
readSensor(senspin); // first read
readSensor(senspin); // second read
unsigned long t1 = micros();
Serial.println(t1 - t0);

J-M-L:
not sure what you are exactly trying to do

lets say i need to count the time between a twice pressed pushbutton

measuring time it takes when you have delays() in your code is not the best idea.

but when i set the brt == 3 or something bigger, the time is correct.

why don't you do just something like (assuming readSensor() is the function returning a reading)

unsigned long t0 = micros();
readSensor(senspin); // first read
readSensor(senspin); // second read
unsigned long t1 = micros();
Serial.println(t1 - t0);

gonna try this, thanks

[/quote]

panducdrmwn:
so i need to count the total time for a sensor reading.

you have two 1000 msec delays in the condition when sensread == HIGH and you don’t test for when the button released and pressed again. so when you press the button and hold is down (or is pressed when tested 2 seconds later) the brt== 2 case is exercised and there is always a 2 second delay since brt == 1

gcjr:
you have two 1000 msec delays in the condition when sensread == HIGH and you don't test for when the button released and pressed again. so when you press the button and hold is down (or is pressed when tested 2 seconds later) the brt== 2 case is exercised and there is always a 2 second delay since brt == 1

i see, but why when i change the brt == 2 to bigger value the time works just fine?

Here is one way to do it (not tested though), assuming the timer is launched when the button is first time released.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );      // variable for reading the pushbutton status
unsigned long tstart, tend;
float ttime;
byte number = 0;
const int senspin = 3;
bool oldSens = false;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  pinMode (senspin, INPUT); // button connected pull-down
}

void loop() {
  bool sensread = digitalRead(senspin); // HIGH is pushed
  if (sensread) { // button is pushed
    if (oldSens != sensread) ++number;
    lcd.clear();
    lcd.print("number is: ");
    lcd.print(number);
    delay(30);  // debounce
  }

  switch (number) {
    case 1: {
        if (sensread) { // timer is launched when button is released
          tstart = millis();
          Serial.println(tstart / 1000);
        }
        break;
      }
    case 2: {
        number = 0;
        tend = millis();
        Serial.println(tend / 1000);
        ttime = (float)(tend - tstart) / 1000.0;
        lcd.clear();
        lcd.print("total time: ");
        lcd.print(ttime,2);
        delay(5000);
        break;
      }
    default:
      break;
  }
  oldSens = sensread;
}

But as J-M-L said, there is a mcuh simpler way of doing it:

void loop() {
  while (!digitalRead(senspin)); // HIGH is pushed
  tstart = millis();
  delay(30);  // debounce
  while (!digitalRead(senspin)); // HIGH is pushed
  tend = millis();
  ttime = (float)(tend - tstart) / 1000.0;
  lcd.clear();
  lcd.print("total time: ");
  lcd.print(ttime, 2);
  delay(5000);
}

But it is a blocking code.

yet another way (tested)

#include <LiquidCrystal_I2C.h>

unsigned long brt;

int senspin = 3;

LiquidCrystal_I2C lcd(0x27, 16, 2);

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);
    pinMode (senspin, INPUT_PULLUP);

    lcd.init ();
    lcd.setCursor (0, 1);
    lcd.print ("Pandu 3");
}

// -----------------------------------------------------------------------------
void
loop () {
    static unsigned long usecLst = 0;
           unsigned long usec    = micros ();

    int sensread = digitalRead(senspin);

    if (sensread == HIGH){
        if (! (brt++ % 1000))  {
            lcd.clear();
            lcd.print ("time: ");
            lcd.print (usec - usecLst);
            lcd.print (" usec ");
            lcd.print (brt / 10000);
        }
    }
    else
        Serial.println (usec);

    usecLst = usec;
}