-2

I've tried to implement millis(); like such down below, but each variation failed.

void ledMode(byte mode)
{
    unsigned long currentTime = millis();
    if (currentTime - previousTime >= Interval)
    {
        switch (mode)
        {
        case 1:
            digitalWrite(RLED, HIGH); //Red
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, LOW);
            Serial.println("Red!");
            previousTime = currentTime;
            break;

        case 2:
            digitalWrite(RLED, LOW);  //Green
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("GREEN!");
            previousTime = currentTime;
            break;

        case 3:
            digitalWrite(RLED, LOW);  //blue
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("BLUE!");
            previousTime = currentTime;
            break;

        case 4:
            digitalWrite(RLED, HIGH);  //orange
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("ORANGE!");
            previousTime = currentTime;
            mode = 5;
            break;

        case 5:
            digitalWrite(RLED, LOW);  //teal
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("TEAL!");
            previousTime = currentTime;
            break;
        case 6:
            digitalWrite(RLED, HIGH); //purple
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("PURPLE!");
            previousTime = currentTime;
            break;

        case 7:
            digitalWrite(RLED, HIGH); //white
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("WHITE!");
            previousTime = currentTime;
            break;
        }
    }
}

void RGBoff()
{
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, LOW);
}

void loop()
{
    button();
   if (count % 2 == 1)
{
    unsigned long currentTime = millis();
    for (byte x = 1; x < 8;)
    {
        ledMode(x);
        if (currentTime - previousTime >= Interval)
        {
            Serial.println(x);
            previousTime = currentTime;
            x++;
        }
    }   
}
}

I'd like x to increment every 1000 milliseconds...

Any help or suggestions would be greatly appreciated!

  • 2
    Please try to include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). `millis()` is not a function implementation in this code, it is a function call. `previousTime` being declared in `ledMode` doesn't sound very good. Have a look at something like this to sleep for 1 second: https://stackoverflow.com/a/62804585/260313 – rturrado Sep 05 '21 at 21:23
  • @rturrado This is Arduino code; you can't run it on a computer. `millis()` is a perfectly valid fuction call, and is used correctly in this code. – Sylvester Kruin Sep 05 '21 at 22:44
  • @SamMatzko It is trivial to make a Windows, MacOS, or Linux implementation of millis(). The only difference is that it doesn't run on a hardware timer, and so likely will exhibit more jitter than one based on a hardware time. Even so, it can be helpful. If you cannot test your main algorithms on a known platform, you're likely doing it wrong. – TomServo Sep 05 '21 at 23:50
  • Search the internet for "c++ arduino sleep". You are looking for a *sleep* function rather than a *delay* function. A *sleep* function allows other tasks to run while your task is sleeping. A *delay* function *spins* until the duration is elapsed, not allowing the OS to run other tasks (interrupts exempted). – Thomas Matthews Sep 06 '21 at 01:28

1 Answers1

1

You can do such things, but your code doesn't. It doesn't wait until the time you want has elapsed, it just checks – and if that didn't happen, it just moves on: that's literally what you implemented!

This looks a lot like arduino code (this isn't just C++, it uses Arduino functionality; be sure you can tell the language from the framework!), so you should use arduino's tools for delays, or, much better, use your microcontroller's timer hardware to execute things at the right time (don't know whether Arduino allows that – I often think that arduino was written for especially anything but microcontrollers...)

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Yes, sorry, this is Arduino code as well... Ah I see, well, I tried this variation as well - (The updated code in my question) But, it seems to only delay the for() loop itself, not the increments... –  Sep 05 '21 at 21:50
  • you've not changed anything about the problem: you're still not *waiting*, just checking. If I ask you "is it midnight?", then your answer doesn't make me automatically stop till midnight. – Marcus Müller Sep 05 '21 at 21:51