millis() blink != 50/50

There has been a spate of millis() questions.
Seems like it might be the hardest part of Arduino to grasp.
I'm trying to expand on an example with equal on and off times and have a blink with different on_time and off_time.

long waitUntil = 0;
boolean LED13state = true;

void setup()
{
   pinMode(13, OUTPUT);
}

void loop()
{
  if (LED13state = true)
  { 
    digitalWrite(13, LED13state);
    if (millis() >= waitUntil)
      {
        LED13state = !(LED13state); // LED13state = false
        waitUntil = millis() + 50;
      }
  }
  if (LED13state = false)
  {
    digitalWrite(13, LED13state);
    if (millis() >= waitUntil)
      {
        LED13state = !(LED13state); // LED13state = true
        waitUntil = millis() + 950;
      } 
  }
}

It gets it once, on 50ms, off 950ms, and then back on where it stays.

  if (LED13state = false)

The second hardest concept might be = vs ==.

  if (LED13state == true)

{
...
        LED13state = !(LED13state); // LED13state = false

Wouldn't it be easier to write:

        LED13state = false;

After all, that's what the comment says.

(Note, corrected = to == when quoting). :wink:

PaulS:

  if (LED13state = false)

The second hardest concept might be = vs ==.

I think Paul his the finishing nail square on the head with a sledge hammer.

Time and Time again, I see this.

Trouble is, people "graduate" from VBscript (or Visual Basic), where you do in fact use "=" to test for equality.

OK
Thanks for your Replies.
I have the 'double equals' / "equal to" implemented.

Using..

long waitUntil = 0;
boolean LED13state = true;   // init as True = 1 = on

void setup()
{
   pinMode(13, OUTPUT);
}

void loop()
{
  if (LED13state == true)
  {
    digitalWrite(13, LED13state);  // D13 = 1 (true)?
    if (millis() >= waitUntil)
      {
        LED13state = !LED13state; // LED13state = true
        waitUntil = millis() + 950;
      } 
  }
  if (LED13state == false)
  { 
    digitalWrite(13, LED13state);  // D13 = 0 (false)?
    if (millis() >= waitUntil)
      {
        LED13state = !LED13state; // LED13state = false
        waitUntil = millis() + 50;
      }
  }
}

I get 50ms ON time and 950ms OFF time.
Since I start with LED13state = true in the first place, I expected the opposite, that my ON time would be 950 and the OFF 50.

A better way to do this with less code, IMO, is with a while loop.

OK
I can't get why I'm stuffing it with my example, the hole in my perception.
But if you've a better example I welcome it.
Thanks.

runaway_pancake:
I get 50ms ON time and 950ms OFF time.
Since I start with LED13state = true in the first place, I expected the opposite, that my ON time would be 950 and the OFF 50.

You have:

 if (LED13state == true)
  {
    digitalWrite(13, LED13state);  // D13 = 1 (true)?
    if (millis() >= waitUntil)
      {
        LED13state = !LED13state; // LED13state = true
        waitUntil = millis() + 950;
      } 
  }

So once it is ON, you then set it up to wait until time is up (ie. 50mS) then turn it off and wait for 950 mS.

Maybe toggle the boolean, then set the pin. In fact, who needs variables?

void loop()
{
  if (digitalRead (13) == HIGH)
  {
    if (millis() >= waitUntil)
      {
        digitalWrite(13, LOW);  
        waitUntil = millis() + 50;  // off for 50 mS
      } 
  }
  else
  { 
    if (millis() >= waitUntil)
      {
        digitalWrite(13, HIGH); 
        waitUntil = millis() + 950;  // on for 950 mS
      }
  }
} // end ofloop

Also, this won't work when millis wraps around. Better to do a subtraction:

unsigned long waitUntil;
unsigned long startTime;

void loop()
{
  if (millis() - startTime >= waitUntil)
    {
     if (digitalRead (13) == HIGH)
     {
      digitalWrite(13, LOW);  
      startTime = millis ();
      waitUntil = 50;  // off for 50 mS
     }
    else
     { 
      digitalWrite(13, HIGH); 
      startTime = millis ();
      waitUntil = 950;  // on for 950 mS
     }
  }  // end if time up
} // end ofloop

I need to learn to think, type, test and debug faster....
I can't think of an easier way to say all this in English... 1st though, now you digital write only on state change.

Tested code:

unsigned long waitUntil = 0UL; // because millis() returns UL 
boolean LED13state = false; // so the light turns on 1st time through loop()   

void setup()
{
   pinMode(13, OUTPUT);
   digitalWrite(13, LED13state);  
}

void loop()
{
    if (waitUntil - millis() > 1000UL)  // welcome to unsigned math
    {
        LED13state = ! LED13state;
        if (LED13state == true)    waitUntil = millis() + 50UL; 
       else                                   waitUntil = millis() + 950UL; 
       digitalWrite(13, LED13state);  
    }
}

Sketch to show unsigned math across unsigned overflow:

unsigned long a, b, c;

void setup() {
  Serial.begin( 9600 );
  a = 0xffffff00UL;
  b = 0x10UL;
  Serial.println( "unsigned math" );
  Serial.print( "a = ");
  Serial.print( a, DEC );
  Serial.print( " = 0x");
  Serial.println( a, HEX );
  Serial.print( "b = ");
  Serial.print( b, DEC );
  Serial.print( " = 0x");
  Serial.println( b, HEX );
  if ( b >= a ) Serial.println( "b >= a" );
  else          Serial.println( "a > b" );
  c = a - b;
  Serial.print( "a - b = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.println( c, HEX );
  c = b - a;
  Serial.print( "b - a = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.println( c, HEX );
  c = b - (b + 1);
  Serial.print( "b - (b + 1) = ");
  Serial.print( c, DEC );
  Serial.print( " = 0x");
  Serial.println( c, HEX );
}

void loop() {};

How do you copy the Serial Monitor to clipboard?

GoForSmoke:
How do you copy the Serial Monitor to clipboard?

Just select it and Copy. Doesn't that work for you? What operating system are you on?

"Who needs variables?"

Well, I'm a "magic numbers" guy, my tendency is to eschew variables. In long passages, I can see how they facilitate understanding, esp. when assistance is requested.
(I didn't want to get gigged for not having used variables. "When in Rome...")

Anyway, I'm using an example written by a fairly influential forum contributor:

long waitUntil = 0;
boolean LED13state = true;
const byte led_pin = 13;

void setup()
{
   pinMode(13, OUTPUT);
}

void loop()
{
  digitalWrite(13, LED13state);
  if (millis() >= waitUntil)
    {
      LED13state = !(LED13state);
      waitUntil = millis() + 500;
    }
}

So, I thought I could take advantage of his boolean flag for asymmetry (desired).

Why does

if (LED13state == true)
  {
    digitalWrite(13, LED13state);  // D13 = 1 (true)?
    if (millis() >= waitUntil)
      {
        LED13state = !LED13state; // LED13state = true
        waitUntil = millis() + 950;
      } 
  }

only get me 50ms of On time and not 950ms? [Because I'm a total dork, yes?]
I mean...
here it comes into the IF with LED13state being True, and also by that time millis() is > 0 (waitUntil's init value); so LED13state gets inverted [now = false] and waitUntil becomes millis() + 950
and.. ??
I'm stumped. (Oh, me.)
What's going on that's evading me, that I ought consider but I'm blind to?

[ Nick G. - That subtraction example wouldn't compile for me. ]

When you're trying to make something happen at a regular interval, I recommend that you use

nextTime = nextTime + INTERVAL;

Rather than:

nextTime = now + INTERVAL;

By the time you make this assignment you are some unknown and variable time past when the event was due to have occurred. To avoid cumulative error, schedule the next event based on when the previous event was actually due, not based on 'now'.

OK
No progress in nailing that "state" deal.
I went back to the shop, as it were, for what I could hammer out on my own.

Using...

const byte led_pin = 13;    // using on-board "LED D13"
const long time_on = 100;
const long time_off = 900;
unsigned long change_pt = 0;

void setup()
{
  pinMode(led_pin, OUTPUT);
}

void loop()
{
  change_pt = (millis() + time_on);
  while (millis() < change_pt)
  {
    digitalWrite(led_pin, HIGH);
  }
  
  change_pt = (millis() + time_off);
  while (millis() < change_pt)
  {
    digitalWrite(led_pin, LOW);
  }  
}

...the blink stays in sync with the klacker on WWV.

It's likely not roll-over proof and if I've minimised the effect of any cumulative error, unlikely, that was purely by accident.

There are still a couple of issues with your code. Well, three.

First, adding time variables is not a good idea. Addition can cause rollover.

Second, there is no reason to have digitalWrite() in the while body. It needs to be called once, not over and over again.

Third, congratulations on re-writing the delay() function. Not a particularly necessary activity, since there is a perfectly good way to waste time already.

The whole reason for using millis() is to do things in a non-blocking way.
while(now - later)
{
// Do nothing useful
}
is blocking, exactly like delay() is.

Well, I was anticipating accolades.
Yes, having the digitalWrite inside the while[/] didn't buy me anything.
I'd already thought on that and took it out.
```
*const byte led_pin = 13;    // using on-board "LED D13"
const long time_on = 100;
const long time_off = 900;
unsigned long change_pt = 0;

void setup()
{
  pinMode(led_pin, OUTPUT);
}

void loop()
{
  digitalWrite(led_pin, HIGH);
  change_pt = (millis() + time_on);
  while (millis() < change_pt)
  {
    // opportunity for useful stuff
    // as I'm not blocked up!
  }
 
  digitalWrite(led_pin, LOW);
  change_pt = (millis() + time_off);
  while (millis() < change_pt)
  {
    // another opportunity for useful stuff
    // as above
  } 
}*
```
If I don't do addition then what's my reference?

If I don't do addition then what's my reference?

If I step on your foot, and some time later, you ask me to get off:

unsigned long steppedOnFooot;
unsigned long timeToWaitForIgnormousToGetOff = 1000;

if(digitalRead(footPin) == HIGH)
{
   steppedOnFoot = millis();
}

if(millis() - steppedOnFoot >= timeToWaitForIgnormousToGetOff)
{
  shoveIgnoramousOff();
}

void shoveIgnoramousOff()
{
  Serial.print("Kind sir, please stop standing on my foot.");
}

runaway_pancake:
[ Nick G. - That subtraction example wouldn't compile for me. ]

I left out the setup function - I didn't want to clutter up the post too much. This compiles, and blinks the LED - on for long, off for a little bit.

unsigned long waitUntil;
unsigned long startTime;

void setup()
{
   pinMode(13, OUTPUT);
}

void loop()
{
  if (millis() - startTime >= waitUntil)
    {
     if (digitalRead (13) == HIGH)
     {
      digitalWrite(13, LOW);  
      startTime = millis ();
      waitUntil = 50;  // off for 50 mS
     }
    else
     { 
      digitalWrite(13, HIGH); 
      startTime = millis ();
      waitUntil = 950;  // on for 950 mS
     }
  }  // end if time up
} // end ofloop

If I don't do addition then what's my reference?

Look at the above - it works, and it doesn't do addition. After all subtraction is just inverse addition. However with unsigned numbers it rolls over properly when millis () overflows, addition doesn't.

runaway_pancake:
...

void loop()

{
...
  while (millis() < change_pt)
  {
    // opportunity for useful stuff
    // as I'm not blocked up!
  }
 ...
  while (millis() < change_pt)
  {
    // another opportunity for useful stuff
    // as above
  } 
}

There's not really much point in putting two loops inside loop. The method I posted above does not add any additional loops.

#include <WProgram.h>

#define HAS_TIME_PASSED(VAR_FUTURE_)    (((long)(millis() - VAR_FUTURE_)) >= 0)

const uint8_t           pinLED          = 13;

const unsigned long     LED_TIME_NOW    = 0UL;
const unsigned long     LED_TIME_OFF    = 900UL;
const unsigned long     LED_TIME_ON     = 100UL;

uint8_t                 stateLED;
unsigned long           triggertimeLED;


void toggleLED()
{
    stateLED        = ((LOW == stateLED) ? HIGH : LOW);
    triggertimeLED += ((LOW == stateLED) ? LED_TIME_OFF : LED_TIME_ON);
    digitalWrite(pinLED, stateLED);
}

void loop()
{
    if ( HAS_TIME_PASSED(triggertimeLED) )
    {
        toggleLED();
    }
}

void setup()
{
    stateLED        = LOW;
    triggertimeLED  = millis() + LED_TIME_NOW;

    pinMode(pinLED, OUTPUT);
}