Issue using millis

I am using millis to flash leds at different speeds and this works fine. However i would like to randomly change the offtime, and i cant get this to work. To see if the "if" condition is triggering a change i changed it to simply change offtime to a new number and still nothing happens. The following code is in void loop.

 unsigned long currentMillis = millis();
  if(currentMillis - previousMillis1 >= 20000)
  {
    previousMillis1 = currentMillis;
    OffTime1 = 900;
  }

However offtime remains unchanged at the original value

That code does not compile and is not in a function.

Help us help you.

This code has nothing to do with your problem. Could you please to show a relevant code?

consider


const byte PinLed    = LED_BUILTIN;

unsigned long msecPeriod;
unsigned long msecLst;
unsigned long msecLed [] = { 500, 2000};

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();
    if (msec - msecLst >= msecPeriod)  {
        msecLst = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));

        if (LOW == digitalRead (PinLed))  {
            msecPeriod = random(1,10) * 100;
            Serial.println (msecPeriod);
        }
        else
            msecPeriod = msecLed [digitalRead (PinLed)];
    }
}

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

    pinMode (PinLed,    OUTPUT);
}
1 Like

Excluding the code which initialises the variables, here is the full void loop.

void loop()
{
  // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis1 >= 20000)
  {
    previousMillis1 = currentMillis;
    OffTime1 = 900;
  }
  if(currentMillis - previousMillis2 >= 20000)
  {
    previousMillis2 = currentMillis;
    OffTime2 = random(600, 1500);  
  } 
  if((switchpos1 == HIGH) && (currentMillis - previousMillis >= 1000)) //1st octal tranceiver
    {	
      switchpos1 = LOW;
      switchpos2 = HIGH;
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(switch1, switchpos1);  // Update the actual LED
      digitalWrite(switch2, switchpos2);
    }
    else if((switchpos1 == LOW) && (currentMillis - previousMillis >= 500))
    {
     switchpos1 = HIGH;
     switchpos2 = LOW;
     previousMillis = currentMillis;  // Remember the time
     digitalWrite(switch1, switchpos1);
     digitalWrite(switch2, switchpos2);
    }
 
  if((chipState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    chipState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(chipPin1, chipState1);  // Update the actual LED
  }
  else if ((chipState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    chipState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(chipPin1, chipState1);	  // Update the actual LED
  }
  
  if((chipState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    chipState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(chipPin2, chipState2);  // Update the actual LED
  }
  else if ((chipState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    chipState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(chipPin2, chipState2);	  // Update the actual LED
  }
}

whether using the random function or just setting a new number, OffTime1 and OffTime2 retain their original values.

Your code contains an eight millis conditions and use only three previousMillis variables. In general, each such condition must use a separate variable.

Hello cookie1310

Below you see another solution for your school assignment.

#define usl unsigned long // I´m lazy to type
// make names
enum Switch{Off,On};
// make variables
usl offOn[] {2000,500}; 
constexpr int LedPin {9};
usl timeTag; 

void setup() 
{
  pinMode(LedPin,OUTPUT);
}
void loop()
{
  if (millis()-timeTag>= offOn[digitalRead(LedPin)])
  {
    timeTag=millis();
    digitalWrite(LedPin,digitalRead(LedPin)?LOW:HIGH);
    if (digitalRead(LedPin)==LOW) offOn[Off]=(usl)random(100,2000);
  }
}

Have a nice day and enjoy coding in C++.

it would help if you posted how the various timer variables are initialized

looks like previousMillis2 is reset (to currentMillis) in the chipState2 conditions, preventing the case where offTime2 being set to a random variable is ever set

again, guessing because you didn't post the entire code

Ah yes, well spotted. Problem solved. Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.