Clueless on why time difference with millis is not working

Hi All -

Thank you in advance for your support in understanding, where I’m going wrong, I’m using distance measurement sensor, trying to find out the time taken for distance reduction from a position to other.

Below is the sketch, also enclosing the same.

Best,

Ramesha

[#include <Wire.h>
#include <VL6180X.h>
#define LED 13
VL6180X sensor;
float dist, sens;
int count;
unsigned long startMillis = 0;
unsigned long stopMillis = 0;
unsigned long tim = 0;
void setup()
{
  Wire.begin();
  sensor.init();
  sensor.configureDefault();

  sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 05);
  sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 10);

  sensor.setTimeout(500);

  sensor.stopContinuous();

  delay(300);

  sensor.writeReg(0x10A, 0x08);

  sensor.startRangeContinuous(10);

  Serial.begin(57600);

  count = 10;
}

void loop()
{
  sens = sensor.readRangeContinuous();
  if (sens < 255)
  {
    if ( abs(sens - dist) > 10.0) {
      dist = sens;
    }
    dist = .995 * dist + .005 * sens;
  }
  count = count - 1;
  if (count == 0)
  {
    count = 1;
    //  Serial.print("\t Range: ");
  }

  {
    if (dist >= 40.00 && dist <= 80.00)
    {
      Serial.print(dist);
      Serial.println();
    }
  }

  if (dist >= 40.00)
  { (startMillis = millis());
//    Serial.print(startMillis);
    digitalWrite (LED, HIGH);
    //    Serial.print("Measuring");
  }
  while (dist >= 80) {
    (stopMillis = millis());
    digitalWrite (LED, LOW);
//    Serial.println("Measurement Complete");
  }
  tim = (stopMillis - startMillis);
  Serial.print("Time msecs=  ");
  Serial.print(tim/1000ul);
  Serial.println();

  // if (int sensor.timeoutOccurred()) {
  //        Serial.print(" TIMEOUT");
  //
}]

DistaneMeasurement_FCUP.ino (2.33 KB)

What does “not working” mean here?

Thanks for quick reply.

Not working - correction, I'm not getting the correct difference between start time and stop time(millis)

rockwellramesha: Thanks for quick reply.

Not working - correction, I'm not getting the correct difference between start time and stop time(millis)

When you Serial.print() the values of start time and stop time, what are the values? And which one is correct?

Paul

while (dist >= 80) {
    (stopMillis = millis());
    digitalWrite (LED, LOW);
    //    Serial.println("Measurement Complete");
  }

I suspect stopMillis is never getting set to anything other than its initial value, because if this while() statement is ever true your code will be eternally waiting for the value of dist to change.

How do you know what the correct difference should be, and what is that value?

If it ever gets into your while() loop where stopMillis is set how do you think that it will get out again?

Steve

data from serial monitor

slipstick: If it ever gets into your while() loop where stopMillis is set how do you think that it will get out again?

Steve

Clipped Data from serial monitor - time msecs remains same from the beginning

Time msecs= 0 40

Time msecs= 4294965 Time msecs= 4294965 Time msecs= 4294965 Time msecs= 4294965 40.01

Time msecs= 4294965 40.02

Time msecs= 4294965 40.02

Time msecs= 4294965 40.04

Time msecs= 4294965 40.07

Time msecs= 4294965 40.1

Time msecs= 4294965 40.11

Time msecs= 4294965 40.12

Time msecs= 4294965 40.14

Time msecs= 4294965 40.15

Time msecs= 4294965 40.17

Time msecs= 4294963 63.41 startMillis3351

Time msecs= 4294963 63.4 startMillis3361

Time msecs= 4294963 63.38 startMillis3372

Time msecs= 4294963 63.37 startMillis3381

Time msecs= 4294963 63.35 startMillis3391

Time msecs= 4294963 63.35 startMillis3400

Time msecs= 4294963 63.35 startMillis3410

Time msecs= 4294963 63.34 startMillis3421

Time msecs= 4294963 63.34 startMillis3430

Time msecs= 4294963 63.33 startMillis3440

Time msecs= 4294963 63.33 startMillis3449

Time msecs= 4294963 63.32 startMillis3460

Time msecs= 4294963 63.31 startMillis3470

Time msecs= 4294963 63.31 startMillis3479

Time msecs= 4294963 63.3 startMillis3489

Time msecs= 4294963 63.32 startMillis3499

Time msecs= 4294963 63.31 startMillis3509

Time msecs= 4294963 63.3 startMillis3519

Time msecs= 4294963 63.3 startMillis3528

Time msecs= 4294963 63.29 startMillis3538

Time msecs= 4294963 63.29 startMillis3548

Time msecs= 4294963 63.27 startMillis3558

Time msecs= 4294963 63.26 startMillis3568

Time msecs= 4294963 63.25 startMillis3577

Time msecs= 4294963 63.25 startMillis3588

Time msecs= 4294963 63.24 startMillis3597

Time msecs= 4294963 63.25 startMillis3607

Time msecs= 4294963 63.25 startMillis3617

Time msecs= 4294963 63.25 startMillis3627

Time msecs= 4294963 63.23 startMillis3637

Time msecs= 4294963 63.23 startMillis3646

Time msecs= 4294963 63.24 startMillis3656

Time msecs= 4294963 63.25 startMillis3666

Time msecs= 4294963 63.26 startMillis3676

Time msecs= 4294963 63.28 startMillis3686

Time msecs= 4294963 63.28 startMillis3695

Time msecs= 4294963 63.27 startMillis3704

Time msecs= 4294963 63.27 startMillis3716

rockwellramesha: data from serial monitor

Clipped Data from serial monitor - time msecs remains same from the beginning

Time msecs= 0 40

Time msecs= 4294965 Time msecs= 4294965 Time msecs= 4294965 Time msecs= 4294965 40.01

Time msecs= 4294965 40.02

Time msecs= 4294965 40.02

Time msecs= 4294965 40.04

Time msecs= 4294965 40.07

Time msecs= 4294965 40.1

Time msecs= 4294965 40.11

Time msecs= 4294965 40.12

Time msecs= 4294965 40.14

Time msecs= 4294965 40.15

Time msecs= 4294965 40.17

Time msecs= 4294963 63.41 startMillis3351

Time msecs= 4294963 63.4 startMillis3361

Time msecs= 4294963 63.38 startMillis3372

Time msecs= 4294963 63.37 startMillis3381

Time msecs= 4294961 75.02 startMillis5249

Time msecs= 4294961 75.04 startMillis5258

Time msecs= 4294961 75.06 startMillis5268

Time msecs= 4294961 75.08 startMillis5277

Time msecs= 4294961 75.1 startMillis5287

Time msecs= 4294961 75.14 startMillis5298

Time msecs= 4294961 75.15 startMillis5307

Time msecs= 4294961 75.18 startMillis5317

Time msecs= 4294961 75.2 startMillis5326

Time msecs= 4294961 75.24 startMillis5337

Time msecs= 4294961 75.28 startMillis5347

Time msecs= 4294961 75.32 startMillis5356

Time msecs= 4294961 75.36 startMillis5366

Time msecs= 4294961 75.4 startMillis5376

Time msecs= 4294961 75.42 startMillis5386

Time msecs= 4294961 75.46 startMillis5396

Time msecs= 4294961 75.47 startMillis5405

Time msecs= 4294961 75.49 startMillis5415

Time msecs= 4294961 75.52 startMillis5425

Time msecs= 4294961 75.56 startMillis5435

Time msecs= 4294961 75.58 startMillis5445

Time msecs= 4294961 75.63 startMillis5454

Time msecs= 4294961 75.67 startMillis5465

Time msecs= 4294961 75.71 startMillis5474

Time msecs= 4294961 75.73 startMillis5484

Time msecs= 4294961 75.76 startMillis5494

Time msecs= 4294961 75.79 startMillis5504

Time msecs= 4294961 75.83 startMillis5514

Time msecs= 4294961 startMillis5523

startMillis5533

If you let it run long enough, you will see Time msecs slowly change, you are subtracting startMillis from 0, then dividing the answer by 1000 when printing it, so the value will only change once per second, and with your loop running at around 100 times per seconds that is a lot of output between changes.

Seriously, if you ever reach the condition to set the value of stopMillis, the program will appear to freeze completely.

david_2018: If you let it run long enough, you will see Time msecs slowly change, you are subtracting startMillis from 0, then dividing the answer by 1000 when printing it, so the value will only change once per second, and with your loop running at around 100 times per seconds that is a lot of output between changes.

Seriously, if you ever reach the condition to set the value of stopMillis, the program will appear to freeze completely.

Collected quick, short data - start millis is 1181 and stop 2663, effectively should be getting difference of 1,482 but results are different

⸮⸮+k⸮Time msecs= 0 Time msecs= 0 Time msecs= 0 Time msecs= 0 49 startMillis1181 Time msecs= 4294966115 49 startMillis1191 Time msecs= 4294966105 49 startMillis1202 Time msecs= 4294966094 48.99 startMillis1211 Time msecs= 4294966085 48.98 startMillis1221 Time msecs= 4294966075 48.97 startMillis1231 Time msecs= 4294966065 48.97 startMillis1241 Time msecs= 4294966055 48.98 startMillis1250 Time msecs= 4294966046 48.98 startMillis1260 Time msecs= 4294966036 49 startMillis1270 Time msecs= 4294966026 49.01 startMillis1280 Time msecs= 4294966016 49.02 startMillis1290 Time msecs= 4294966006 49.02 startMillis1300 Time msecs= 4294965996 49.05 startMillis1309 Time msecs= 4294965987 49.06 startMillis1319 Time msecs= 4294965977 49.07 startMillis1329 Time msecs= 4294965967 49.09 startMillis1339 Time msecs= 4294965957 49.12 startMillis1348 Time msecs= 4294965948 49.15 startMillis1358 Time msecs= 4294965938 49.18 startMillis1369 Time msecs= 4294965927 49.21 startMillis1378 Time msecs= 4294965918 49.25 startMillis1388 Time msecs= 4294965908 49.28 startMillis1398 Time msecs= 4294965898 49.32 startMillis1408 Time msecs= 4294965888 60 startMillis1417 Time msecs= 4294965879 60.01 startMillis1427 Time msecs= 4294965869 60.01 startMillis1437 Time msecs= 4294965859 60.01 startMillis1446 Time msecs= 4294965850 59.99 startMillis1457 Time msecs= 4294965839 60.01 startMillis1466 Time msecs= 4294965830 60.02 startMillis1476 Time msecs= 4294965820 60.04 startMillis1485 Time msecs= 4294965811 60.05 startMillis1496 Time msecs= 4294965800 60.07 startMillis1506 Time msecs= 4294965790 60.09 startMillis1515 Time msecs= 4294965781 60.12 startMillis1525 Time msecs= 4294965771 60.14 startMillis1534 Time msecs= 4294965762 60.14 startMillis1545 Time msecs= 4294965751 60.17 startMillis1554 Time msecs= 4294965742 60.19 startMillis1564 Time msecs= 4294965732 60.21 startMillis1574 Time msecs= 4294965722 60.24 startMillis1584 Time msecs= 4294965712 60.27 startMillis1594 Time msecs= 4294965702 60.28 startMillis1603 Time msecs= 4294965693 60.29 startMillis1613 Time msecs= 4294965683 60.32 startMillis1623 Time msecs= 4294965673 60.37 startMillis1633 Time msecs= 4294965663 60.41 startMillis1643 Time msecs= 4294965653 60.46 startMillis1652 Time msecs= 4294965644 60.49 startMillis1662 Time msecs= 4294965634 60.52 startMillis1672 Time msecs= 4294965624 60.57 startMillis1682 Time msecs= 4294965614 60.61 startMillis1691 Time msecs= 4294965605 60.65 startMillis1701 Time msecs= 4294965595 60.69 startMillis1712 Time msecs= 4294965584 60.72 startMillis1721 Time msecs= 4294965575 60.77 startMillis1731 Time msecs= 4294965565 60.81 startMillis1740 Time msecs= 4294965556 60.84 startMillis1751 Time msecs= 4294965545 60.89 startMillis1760 Time msecs= 4294965536 60.92 startMillis1770 Time msecs= 4294965526 60.96 startMillis1780 Time msecs= 4294965516 61 startMillis1789 Time msecs= 4294965507 61.04 startMillis1800 Time msecs= 4294965496 61.08 startMillis1809 Time msecs= 4294965487 61.12 startMillis1819 Time msecs= 4294965477 61.16 startMillis1828 Time msecs= 4294965468 61.21 startMillis1839 Time msecs= 4294965457 72 startMillis1849 Time msecs= 4294965447 72 startMillis1858 Time msecs= 4294965438 71.98 startMillis1868 Time msecs= 4294965428 71.97 startMillis1878 Time msecs= 4294965418 71.97 startMillis1888 Time msecs= 4294965408 71.96 startMillis1897 Time msecs= 4294965399 71.96 startMillis1907 Time msecs= 4294965389 71.96 startMillis1917 Time msecs= 4294965379 71.96 startMillis1927 Time msecs= 4294965369 71.95 startMillis1937 Time msecs= 4294965359 71.96 startMillis1946 Time msecs= 4294965350 71.96 startMillis1956 Time msecs= 4294965340 71.95 startMillis1966 Time msecs= 4294965330 71.96 startMillis1976 Time msecs= 4294965320 71.96 startMillis1986 Time msecs= 4294965310 71.95 startMillis1995 Time msecs= 4294965301 71.95 startMillis2006 Time msecs= 4294965290 71.96 startMillis2016 Time msecs= 4294965280 71.96 startMillis2025 Time msecs= 4294965271 71.96 startMillis2034 Time msecs= 4294965262 71.95 startMillis2044 Time msecs= 4294965252 71.96 startMillis2055 Time msecs= 4294965241 71.97 startMillis2064 Time msecs= 4294965232 71.99 startMillis2074 Time msecs= 4294965222 72 startMillis2084 Time msecs= 4294965212 72.01 startMillis2094 Time msecs= 4294965202 72.02 startMillis2103 Time msecs= 4294965193 72.02 startMillis2113 Time msecs= 4294965183 72.03 startMillis2123 Time msecs= 4294965173 72.03 startMillis2132 Time msecs= 4294965164 72.03 startMillis2143 Time msecs= 4294965153 72.03 startMillis2153 Time msecs= 4294965143 72.04 startMillis2162 Time msecs= 4294965134 72.03 startMillis2171 Time msecs= 4294965125 72.05 startMillis2182 Time msecs= 4294965114 72.05 startMillis2192 Time msecs= 4294965104 72.05 startMillis2201 Time msecs= 4294965095 72.06 startMillis2211 Time msecs= 4294965085 72.07 startMillis2222 Time msecs= 4294965074 72.08 startMillis2231 Time msecs= 4294965065 72.08 startMillis2240 Time msecs= 4294965056 72.08 startMillis2250 Time msecs= 4294965046 72.1 startMillis2260 Time msecs= 4294965036 72.09 startMillis2270 Time msecs= 4294965026 72.1 startMillis2280 Time msecs= 4294965016 72.12 startMillis2289 Time msecs= 4294965007 72.12 startMillis2299 Time msecs= 4294964997 72.12 startMillis2309 Time msecs= 4294964987 72.14 startMillis2319 Time msecs= 4294964977 72.16 startMillis2329 Time msecs= 4294964967 72.19 startMillis2338 Time msecs= 4294964958 72.2 startMillis2349 Time msecs= 4294964947 72.21 startMillis2359 Time msecs= 4294964937 72.21 startMillis2368 Time msecs= 4294964928 72.24 startMillis2377 Time msecs= 4294964919 72.25 startMillis2387 Time msecs= 4294964909 72.27 startMillis2398 Time msecs= 4294964898 72.29 startMillis2407 Time msecs= 4294964889 72.29 startMillis2417 Time msecs= 4294964879 72.32 startMillis2427 Time msecs= 4294964869 72.32 startMillis2437 Time msecs= 4294964859 72.33 startMillis2446 Time msecs= 4294964850 72.35 startMillis2456 Time msecs= 4294964840 72.37 startMillis2466 Time msecs= 4294964830 72.38 startMillis2476 Time msecs= 4294964820 72.39 startMillis2486 Time msecs= 4294964810 72.41 startMillis2496 Time msecs= 4294964800 72.44 startMillis2505 Time msecs= 4294964791 72.47 startMillis2514 Time msecs= 4294964782 72.48 startMillis2525 Time msecs= 4294964771 72.5 startMillis2535 Time msecs= 4294964761 72.53 startMillis2544 Time msecs= 4294964752 72.54 startMillis2554 Time msecs= 4294964742 72.56 startMillis2565 Time msecs= 4294964731 72.56 startMillis2574 Time msecs= 4294964722 72.58 startMillis2583 Time msecs= 4294964713 72.6 startMillis2593 Time msecs= 4294964703 72.63 startMillis2604 Time msecs= 4294964692 72.65 startMillis2613 Time msecs= 4294964683 72.68 startMillis2623 Time msecs= 4294964673 72.68 startMillis2633 Time msecs= 4294964663 72.72 startMillis2642 Time msecs= 4294964654 72.75 startMillis2652 Time msecs= 4294964644 startMillis2662 StopMillis2662 StopMillis2662 StopMillis2663

Please post your current code.

In your original code, you are setting startMillis whenever (dist >= 40), and the condition that would set stopMillis is (dist >=80), so whenever the (dist >= 80) condition is true, the other conditon of (dist >= 40) will always be true and will effectively set startMillis and stopMillis to the same value, or possibly 1mS difference.

I think what you intend is to set startMillis when dist first reaches 40 or more, and then stopMillis when dist first reaches 80 or more, but you are continuing setting startMillis whenever dist is 40 or more, not when it first occurs.

david_2018:
Please post your current code.

In your original code, you are setting startMillis whenever (dist >= 40), and the condition that would set stopMillis is (dist >=80), so whenever the (dist >= 80) condition is true, the other conditon of (dist >= 40) will always be true and will effectively set startMillis and stopMillis to the same value, or possibly 1mS difference.

I think what you intend is to set startMillis when dist first reaches 40 or more, and then stopMillis when dist first reaches 80 or more, but you are continuing setting startMillis whenever dist is 40 or more, not when it first occurs.

Thanks, you seem to have pointed out, enclosing the code, could you please help me with correcting it.

#include <Wire.h>
#include <VL6180X.h>
#define LED 13
VL6180X sensor;
float dist, sens;
int count;
unsigned long startMillis = 0;
unsigned long stopMillis = 0;
unsigned long tim = 0;
void setup()
{
Wire.begin();
sensor.init();
sensor.configureDefault();

// Reduce range max convergence time and ALS integration
// time to 30 ms and 50 ms, respectively, to allow 10 Hz
// operation (as suggested by Table 6 (“Interleaved mode
// limits (10 Hz operation)”) in the datasheet).
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 05);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 10);

sensor.setTimeout(500);

// stop continuous mode if already active
sensor.stopContinuous();
// in case stopContinuous() triggered a single-shot
// measurement, wait for it to complete
delay(300);

//READOUT__AVERAGING_SAMPLE_PERIOD
sensor.writeReg(0x10A, 0x08);

// start interleaved continuous mode with period of 100 ms
//sensor.startInterleavedContinuous(100);
sensor.startRangeContinuous(10);

Serial.begin(57600);

//ideas
// Look at docs to see how to improve accuracy
// only use samples that converged
// look into whether samples are used twice or other issues with the uncoltrolled loop
// allow more variability in the output of the sensor and average in the mcu
// measure drift by connecting to the CNC machine to get precise moves

count = 10;
}

void loop()
{
sens = sensor.readRangeContinuous();
if (sens < 255)
{
if ( abs(sens - dist) > 10.0) {
dist = sens;
}
dist = .995 * dist + .005 * sens;
}
count = count - 1;
if (count == 0)
{
count = 1;
// Serial.print("\t Range: ");
}

{
if (dist >= 40.00 && dist <= 80.00)
{
Serial.print(dist);
Serial.println();
}
}

if (dist >= 40.00)
{ (startMillis = millis());
Serial.print("\t startMillis");
Serial.println(startMillis);
digitalWrite (LED, HIGH);
// Serial.print(“Measuring”);
}
while (dist >= 80) {
(stopMillis = millis());
Serial.print("\t StopMillis");
Serial.println(stopMillis);
digitalWrite (LED, LOW);
// Serial.println(“Measurement Complete”);
}
tim = (stopMillis - startMillis);
Serial.print("Time msecs= ");
Serial.print(tim);
Serial.println();

// if (int sensor.timeoutOccurred()) {
// Serial.print(" TIMEOUT");
//
}

Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


you have an infinite loop here

while (dist >= 80) {
  (stopMillis = millis());
  Serial.print("\t StopMillis");
  Serial.println(stopMillis);
  digitalWrite (LED, LOW);
  //    Serial.println("Measurement Complete");
}

as you don't update dist in the while loop... so if you enter this loop, your code is locked there forever

J-M-L: Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


you have an infinite loop here

while (dist >= 80) {
  (stopMillis = millis());
  Serial.print("\t StopMillis");
  Serial.println(stopMillis);
  digitalWrite (LED, LOW);
  //    Serial.println("Measurement Complete");
}

as you don't update dist in the while loop... so if you enter this loop, your code is locked there forever

Thanks and appreciate your support

rockwellramesha: Thanks and appreciate your support

but you did not fix your posts.... ??

[quote author=J-M-L date=1582513699 link=msg=4489196]
but you did not fix your posts.... ??
[/quote]

my changed code is here, not initializing start millis, stop millis and time millis

#include <Wire.h>
#include <VL6180X.h>
#define LED 13
VL6180X sensor;
float dist, sens;
int count;
unsigned long startMillis;
unsigned long stopMillis;
unsigned long tim;
void setup()
{
Wire.begin();
sensor.init();
sensor.configureDefault();
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 05);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 10);

sensor.setTimeout(500);

// stop continuous mode if already active
sensor.stopContinuous();
// in case stopContinuous() triggered a single-shot
// measurement, wait for it to complete
delay(300);

//READOUT__AVERAGING_SAMPLE_PERIOD
sensor.writeReg(0x10A, 0x08);

// start interleaved continuous mode with period of 100 ms
//sensor.startInterleavedContinuous(100);
sensor.startRangeContinuous(10);

Serial.begin(57600);

count = 10;
}

void loop()
{
sens = sensor.readRangeContinuous();
if (sens < 255)
{
if ( abs(sens - dist) > 10.0) {
dist = sens;
}
dist = .995 * dist + .005 * sens;
}
count = count - 1;
if (count == 0)
{
count = 1;
// Serial.print("\t Range: ");
}

{
if (dist >= 40.00 && dist <= 80.00)
{
Serial.print(dist);
Serial.println();
}
}
if (dist >= 39.00 && dist <= 81.00)
{ (startMillis = millis());
Serial.print("\t startMillis");
Serial.println(startMillis / 1000ul);
digitalWrite (LED, HIGH);
// Serial.print(“Measuring”);
}
if (dist >= 80) {
(stopMillis = millis());
Serial.print("\t StopMillis");
Serial.println(stopMillis / 1000ul);
digitalWrite (LED, LOW);
// Serial.println(“Measurement Complete”);
}
tim = (stopMillis - startMillis);
Serial.print("Time msecs= ");
Serial.print(tim/1000ul);
Serial.println();

// if (int sensor.timeoutOccurred()) {
// Serial.print(" TIMEOUT");
//
}
[/code]

Does not look good...

J-M-L:
Does not look good…

To be fair, the topic title does start “clueless”

J-M-L: Does not look good...

I'm still clueless