Go Down

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

#### runaway_pancake

##### Dec 17, 2011, 01:21 am
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.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

#### PaulS

#1
##### Dec 17, 2011, 01:32 am
Code: [Select]
`  if (LED13state = false)`
The second hardest concept might be = vs ==.

#### nickgammon

#2
##### Dec 17, 2011, 01:37 am
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).

Please post technical questions on the forum, not by personal message. Thanks!

#3
##### Dec 17, 2011, 03:02 am

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.

#### nickgammon

#4
##### Dec 17, 2011, 04:01 am
Trouble is, people "graduate" from VBscript (or Visual Basic), where you do in fact use "=" to test for equality.
Please post technical questions on the forum, not by personal message. Thanks!

#### runaway_pancake

#5
##### Dec 17, 2011, 04:46 am
OK
I have the 'double equals' / "equal to" implemented.

Using..
Code: [Select]
`long waitUntil = 0;boolean LED13state = true;   // init as True = 1 = onvoid 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.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

#6
##### Dec 17, 2011, 04:58 am
A better way to do this with less code, IMO, is with a while loop.

#### runaway_pancake

#7
##### Dec 17, 2011, 05:12 am
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.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

#### nickgammon

#8
##### Dec 17, 2011, 05:40 amLast 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`
Please post technical questions on the forum, not by personal message. Thanks!

#### GoForSmoke

#9
##### Dec 17, 2011, 06:31 am
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?

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### nickgammon

#10
##### Dec 17, 2011, 07:14 am

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?
Please post technical questions on the forum, not by personal message. Thanks!

#### runaway_pancake

#11
##### Dec 17, 2011, 03:11 pm
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. ]
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

#### PeterH

#12
##### Dec 17, 2011, 04:40 pm
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'.

#### runaway_pancake

#13
##### Dec 17, 2011, 05:33 pm
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.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

#### PaulS

#14
##### Dec 17, 2011, 05:57 pm
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.