Millis Function only returning Zero

Hey guys!

I'm very new with using arduino and also with coding in general so please bear with me:

For part of my code I would like to turn a magnet off, wait five seconds, and turn it back on again. Currently I am using the millis() function with a while loop to write in the delay.

unsigned long currentMillis = millis();
const int INTERVAL = 5000;

while (millis() < INTERVAL + currentMillis){
Serial.print(millis());
Serial.print("\n");
}

The board wasn't exiting the while loop, so I included serial.print(millis()) inside the loop to check the value of millis(), and realized that my millis() function was only returning zero.

I've looked on lots of forums and have tried a few different things including #include <WProgram.h> (which my compiler didn't recognize so it returned an error message), and declaring the millis function at the beginning of my code, but neither of those things have worked. I've also tried downloading and using a couple different libraries (MillisTimer and elapsedMillis) and using those instead, but still with no luck.

If any of you have any ideas on what to try next, that would be greatly appreciated!

Also, I am testing the code with a sparkfun RedBoard and an arduino Uno, and it will eventually be written onto an arduino nano, if that makes a difference.

Thank you!

You need to post all your code.
In code tags.

In that case, you might as well use delay() in you program.

Did you set up the serial port ?

Your claim "Millis Function only returning Zero" is obviously wrong.

void setup() {
  Serial.begin(115200);
}
void loop() {
  static uint32_t lastPrint;
  uint32_t topLoop = millis();
  if (topLoop - lastPrint >= 2000) {
    lastPrint = topLoop;
    Serial.println(topLoop);
  }
}
2000
4000
6000
8000
10000
12000
14000
16000
18000
20000

I actually had the delay function in my code initially, but it wasn't working as expected. If I ran everything in my code except for the delay (which I commented out) it worked, except of course the program didn't wait five seconds. I'm not sure why that is, but I tried millis() as an alternative that I could check with serial.print.

Yeah, I think so. There may be more that I need to do. It's set for an arduino uno on com7, and it has been uploading to the board properly. It just gets stuck in the loop.

Here is the full code

#include <arduino.h>


unsigned long millis(void);


const int Photogate = 2;
const int Button = 3;
const int Magnet = 10;
const int LED = 12;

void setup() {

    Serial.begin(9600);

    pinMode(Photogate, INPUT);
    pinMode(Button, INPUT);
    pinMode(Magnet, OUTPUT);
    pinMode(LED, OUTPUT);

    digitalWrite(Magnet, HIGH);
    digitalWrite(LED, LOW);

}

int main() {

    int Armed = 0;
    int GateState = 0;
    int ButtonState = 0;

    setup();

    while (Armed == 0) {
        ButtonState = digitalRead(Button);
        if (ButtonState == LOW) {
            Serial.print("Button Pushed\n");
            digitalWrite(LED, HIGH);
            Armed = 1;
            while (Armed == 1) {
                GateState = digitalRead(Photogate);
                if (GateState == LOW) {
                    Serial.print("Second button pushed\n");
                    digitalWrite(Magnet, LOW);
                    digitalWrite(LED, LOW);
                    //delay(5000);
                    unsigned long currentMillis = millis();
                    const int INTERVAL = 5000;
                    while (millis() < INTERVAL + currentMillis) {
                        pinMode(8, OUTPUT);
                        digitalWrite(8, HIGH);

                        Serial.print(millis());
                        Serial.print("\n");
                    }
                    digitalWrite(8, LOW);
                    digitalWrite(Magnet, HIGH);
                    Armed = 0;
                } else {
                    Armed = 1;
                }
            }
        } else {
            Armed = 0;
        }
    }
    return 0;
}

Your code is incomplete.
It's missing init(), so not a real surprise millis isn't working.

25 wasted minutes.

Okay, what do I still need to do?

Yeah, I'm sorry about that. It's my first time using an arduino and am not sure about what setup functions are necessary. I'll look that up and see if it fixes my problem.

So, if you're new, why go so far off piste?

Use setup(), use loop(), until you know better.

1 Like

That worked! Thank you!

Normally, main() is provided by the Arduino core, and the user provides setup() and loop(), which are called by the core's main() AFTER various things like the millis() timer have been set up and initialized.

By including main() in your sketch, you have over-ridden the core function, the timers have never been initialized, and millis() won't work. It's the sort of mistake we usually see from "experienced C programmers", since main() is The Way in normal C and C++ programs. You're probably looking at a generic C programming book that is "not quite" applicable to the Arduino. :frowning:

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

1 Like