Creating a delay in printing temperature

I am just starting out and trying to print the temperature from a DS18B20 thermometer to a 16x4 display.

Learnt lots so far but struggling to only update the temp every 3 seconds. I can see that using millis() would be the way to go rather than blocking the code with a delay(). I figure if I can reduce the amount of times the screen is refreshing, it won't flicker on certain characters.

I have been reading UKHeliBob's great tutorial on using millis() for timing but just can't understand why my simple implementation doesn't work. Stripped back code below:

// A bit to create timers
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long tempdelay = 2000;  //the value is a number of milliseconds

void setup()
{ 
//initial start time
 startMillis = millis(); 
}


void loop()


{
// timer - print the temp every 2 seconds
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= tempdelay)  //test whether the period has elapsed
  {
lcd.setCursor(0,1);
lcd.print("Tank temp:   ");

lcd.print(sensors.getTempCByIndex(0));

lcd.print((char)223);
lcd.print("c  ");
  }
}

Any help really appreciated as I have tried for hours before admitting defeat and posting here.

The LCD is still getting readings around every half second - not the 2 secs I am aiming for.

Your stripped-back code doesn't update the

unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
These really are dumb names. Perhaps if you changed them to names that made sense, the problem would be clear. Something like lastScreenUpdateTime and now.

//initial start time
 startMillis = millis();

The start of what? See why the name is stupid. You can't answer that question, can you?

But, the real problem is that you missed part of Bob's tutorial. You never update startMillis, and I can understand why not, since the name means nothing.

But, if you changed startMillis to lastScreenUpdateTime, it would be real obvious where you need to assign it a new value.

This is NOT to put down Bob's efforts. It is meant to make anyone that wants to adopt them THINK about what meaningful names would look like FOR THEIR APPLICATION.

AWOL:
Your stripped-back code doesn't update the

Hit post a bit soon?

It was a hint. Should have used ...

tryingmybest:
I have been reading UKHeliBob's great tutorial on using millis() for timing but just can't understand why my simple implementation doesn't work. Stripped back code below:

It looks like you are not updating startMillis after you use it. And, previousUpdateMillis might be a better name for the variable giving something like this

currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - previousUpdateMillis >= tempdelay)  //test whether the period has elapsed
{
    previousUpdateMillis += tempdelay;
    lcd.setCursor(0,1);
    // etc
}

Have a look at how timing is done in Several Things at a Time.

...R

PaulS:
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
These really are dumb names. Perhaps if you changed them to names that made sense, the problem would be clear. Something like lastScreenUpdateTime and now.

//initial start time

startMillis = millis();



The start of what? See why the name is stupid. You can't answer that question, can you?

But, the real problem is that you missed part of Bob's tutorial. You never update startMillis, and I can understand why not, since the name means nothing.

But, if you changed startMillis to lastScreenUpdateTime, it would be real obvious where you need to assign it a new value.

This is NOT to put down Bob's efforts. It is meant to make anyone that wants to adopt them THINK about what meaningful names would look like FOR THEIR APPLICATION.

To be honest, I don't normally change the names of anything in a tutorial until it works for me - I can then go back and change names to something a bit more meaningful. It's probably the wrong to do things but I normally find it causes less errors.

Semantics aside, changing the names doesn't really fix the issue. I feel I am tackling this the wrong way.

Semantics aside, changing the names doesn't really fix the issue. I feel I am tackling this the wrong way.

You could use G99999 and J88823 for all I care. The reason I mentioned the names was to show how easy it is to omit something important, when the names don't make sense.

How would YOU do something periodically? If the period is one hour, and you do the first thing on the hour, then the rest of the event happen on the hour, too.

But, if the period is 37 minutes, you need to keep track of the last time you did whatever, so that you know when another 37 minutes has elapsed.

You are checking that the 37 minutes have elapsed, in your code, but you are not writing down when you last did whatever, so once 37 minutes have passed, you will, on every pass through loop(), do the action again.

You are missing a startMillis = currentMillis; statement in your code (where you write down when you last did whatever.

Robin2:
It looks like you are not updating startMillis after you use it. And, previousUpdateMillis might be a better name for the variable giving something like this

currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)

if (currentMillis - previousUpdateMillis >= tempdelay)  //test whether the period has elapsed
{
    previousUpdateMillis += tempdelay;
    lcd.setCursor(0,1);
    // etc
}




Have a look at how timing is done in [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0).

...R

Aha - that makes perfect sense. Thanks for that link too. Much appreciated.

PaulS:
Hit post a bit soon?

No. It was a stripped-back answer, to highlight the fact that when we can't see all the code, we can't see what actually has been stripped-out.

AWOL:
No. It was a stripped-back answer, to highlight the fact that when we can't see all the code, we can't see what actually has been stripped-out.

I missed that part about "stripped back". I can't imagine why people do that. Have we started charging more for uploading all the code?