Go Down

Topic: millis() blink != 50/50 (Read 2 times) previous topic - next topic

Runaway Pancake

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.
Code: [Select]

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.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

PaulS

Code: [Select]
  if (LED13state = false)
The second hardest concept might be = vs ==.

Nick Gammon

Quote
Code: [Select]
  if (LED13state == true)
  {
...
        LED13state = !(LED13state); // LED13state = false


Wouldn't it be easier to write:


Code: [Select]
        LED13state = false;

After all, that's what the comment says.

(Note, corrected = to == when quoting). ;)

http://www.gammon.com.au/electronics

magnethead794


Code: [Select]
  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.
KF5RVR

Nick Gammon

Trouble is, people "graduate" from VBscript (or Visual Basic), where you do in fact use "=" to test for equality.
http://www.gammon.com.au/electronics

Runaway Pancake

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

Using..
Code: [Select]

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.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

magnethead794

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

Runaway Pancake

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.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Nick Gammon

#8
Dec 17, 2011, 05:40 am Last Edit: Dec 17, 2011, 07:11 am by Nick Gammon Reason: 1

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:

Code: [Select]
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?

Code: [Select]

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:

Code: [Select]

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
http://www.gammon.com.au/electronics

GoForSmoke

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:
Code: [Select]

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:
Code: [Select]
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?

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Nick Gammon


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?
http://www.gammon.com.au/electronics

Runaway Pancake

Quote

"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:
Code: [Select]

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
Code: [Select]

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. ]
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

PeterH

When you're trying to make something happen at a regular interval, I recommend that you use
Code: [Select]
nextTime = nextTime + INTERVAL;

Rather than:
Code: [Select]
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'.
I only provide help via the forum - please do not contact me for private consultancy.

Runaway Pancake

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...
Code: [Select]

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.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

PaulS

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.

Go Up