Millis program not working

Hi All, This program printing out a constant 95. Should be going up. The function 'millis' measures the time the program has been running. The reading should be going up.
unsigned long currentMillis;
void setup() {
Serial.begin(9600);}
void loop() {
currentMillis=millis;
Serial.println(currentMillis);}
Anybody know what is wrong?

The code that you posted does not compile

Are you sure that you have posted the correct sketch ?


unsigned long currentMillis;

void setup() 
{
   Serial.begin(9600);
}

void loop() 
{
   currentMillis = millis();
   Serial.println(currentMillis);
}



1 Like

millis is the unchanging address of the millis() function: https://www.arduino.cc/reference/en/language/functions/time/millis/

It does for me Bob. But getting continuous 95 as the answer. No compiling problem. Here is another program where the same expression works just fine. Two separate timing periods of 10 seconds and 30 seconds;
const long interval = 10000;
const long interval1 = 30000;
unsigned long previousMillis=0;
unsigned long previousMillis1=0;
unsigned long currentMillis;
unsigned long currentMillis1;

void setup() {
Serial.begin(9600);}
void loop() {
currentMillis = millis();
Serial.println(currentMillis);
currentMillis1 = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println(10);
}
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
Serial.println(30);
}
}

You are right. Because of the poor formatting and the fact that you did not use code tags, when I copied the code I missed the first line of code. Please use code tags when posting code and Auto Format the code in the IDE before posting it so that it looks like this

unsigned long currentMillis;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  currentMillis = millis;
  Serial.println(currentMillis);
}

Have you seen post #4 which explains what is wrong with the sketch ?

1 Like

Hi Larry, Yes that’s my program that just prints out a steady 95. Are you saying it works for you? It should be going up as it times how long the program has been running. Check my other answer in a program that works using the same expression.

Never ignore the compiler warnings.

BTW, C++ is not a tokenised, interpreted language; there is no advantage to squeezing as much as you can onto a single line of code.

Same?
Have you seen an optometrist lately?

No, it isn't.

Please remember to use code tags when posting code.

unsigned long (*currentMillis)();
void setup() {  
Serial.begin(9600); }
void loop() {
currentMillis = millis;
Serial.println(currentMillis ());}
1 Like

If that prints out the same 95, then:

must be assigning currentMills the same value every time, so

must be returning the same value every time.

so maybe

Look at my post #5 Dave. The expression working just fine here. After currentMillis = millis(); the print out showing an increasing currentMillis. So why isn't it working in my shorter program?

I am just referring to the line currentMillis = millis; in the much longer program. The print out shows an increasing millis as it should be because millis measures the length of time the program has been running. My eyes are just fine thank you.

You've now had a day to look at and read the posts. Didn't you pick up what several people have told you.

void loop() {
currentMillis=millis;  //post1 does not increment

void loop() {
currentMillis = millis(); //post5 increments normally

As @anon73444976 says

Same?
Have you seen an optometrist lately?

1 Like

That's right. They both contain the same expression currentMillis=millis; In post 5 it works but not in post1.

Because in your shorter program you don't have the parentheses, so instead of calling the function and getting a result, the compiler thinks you want the to assign the address of the function to your variable, and the address is "95".

// Edit: Oops -- "95". You can see it in Wokwi:

1 Like

Paris in the the spring…

a7

1 Like

Here's something dirty:

unsigned long currentMillis;
void setup() {
Serial.begin(9600);}
void loop() {
currentMillis=millis;
//currentMillis=millis();
//Serial.println(currentMillis);}

// or for fun, try one of these lines instead that casts the '95' back into a function:
//
// Serial.println(((unsigned long (*)(void))currentMillis)());}
Serial.println(((unsigned long (*)(void))95UL)());}
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
2
5
8
11
15
19
23
27
31
35
39
44
48
53
57
61
65
69
73
77
...

Yes I have made a big blunder. Just picked it up from the other program. Shows how careful you have to be in putting in everything correctly. Didn't include '()' at end of millis. Compiler didn't pick it up as you suggest because it thought I was assigning the address of millis to the variable.
Also I am aware that nobody else on the forum picked it up except you Dave.
Works fine now. Wasted a lot of time with this error as did others on the forum. Sorry about that guys.

You were shown how to make it work by adding ( ) to millis back in post #3, it’s now post #18
:wink:

3 Likes

The compiler gave you a warning.
See my earlier comment about not ignoring warnings

Hmmm.

Maybe your Occam's Razor needs honing.

Whats the difference between

millis;

AND

millis();

?

Try

unsigned long currentMillis;
void setup() {  
Serial.begin(9600); }
void loop() {
CurrentMillis=millis();
Serial.println( currentMillis );

}