Blink without delay

Thanks. Those are good points. I did some of the suggested changes and do save a few bytes of ram. I think I'd stick with my way using a state variable that I flip between 0 and 1. Using this approach my update method only has one function call, the digitalWrite(). The approach of using digitalRead() and no passed millis() has five function calls. Surely there is some overhead for all those function calls that my approach eliminates? At the very least it saves about 100 bytes of program space at the cost of a single byte of ram per instance.

I also tested on a couple of other IDE versions and sure enough it doesn't compile on 1.0.5 but does fine on 1.5.7 and 1.6.4. I can't figure out why. I'm baffled.

I've learned so much from this forum. Before I got into Arduino I could not get my head wrapped around OOP. Now I tend to look at everything as an object. I find myself coding and asking myself "WWPS". What would PaulS say. It's really improved my code.

Hi
I create very simple and very acurate "Blink without delay"
There is:

const int ledPin = 13;

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

void loop()
{
byte x = millis()/1000%2;
digitalWrite(ledPin, x);
}

ferojusko2:
Hi
I create very simple and very acurate "Blink without delay"
There is:

const int ledPin = 13;

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

void loop()
{
byte x = millis()/1000%2;
digitalWrite(ledPin, x);
}

Nice try - now what assumption are you making about the return value from millis()?

ferojusko2:
Hi
I create very simple and very acurate "Blink without delay"
There is:

const int ledPin =  13;    

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

void loop()
{
 byte x = millis()/1000%2;
 digitalWrite(ledPin, x);
}

Clever. But it has a discontinuity when millis() overflows and resets to zero. Because 2^32/1000 is not an integer. I think you can do the following:

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

void loop()
{
  digitalWrite(LED_BUILTIN, millis()/1024%2);
}

because 2^32/2^10 is an integer, 2^22.

If you post code again, please use code tags.

... a revived thread ... here's blinking any number of LEDs at different rates (limited by available pins, 8 shown):

const int ledPin[8] =  {2, 3, 4, 5, 6, 7, 8, 13};
const int qty = 8;
const unsigned long onInterval[8] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 50};
const unsigned long offInterval[8] = {500, 1500, 3000, 250, 5000, 150, 7000, 500};
unsigned long previousMillis[8];

void setup() {
  for (int i = 0; i < qty; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  unsigned long currentMillis = millis();
  for (int i = 0; i < qty; i++) {
    if (currentMillis - previousMillis[i] <= onInterval[i]) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
    if (currentMillis - previousMillis[i] >= onInterval[i] + offInterval[i]) {
      previousMillis[i] = currentMillis;
    }
  }
}

Or you can try my library.

E&OE, YMMV.... my first ever attempt at a library

/* Blink without Delay in a class in a library
*/

#include <BWOD.h>

BWOD myBWOD;
BWOD anotherBWOD;

void setup() {
  myBWOD.attach(8, 97, 110);      //pin, on interval, off interval
  anotherBWOD.attach(14, 1798, 1015);
}

void loop()
{
  myBWOD.manageBlink();
  anotherBWOD.manageBlink();
}

BWOD.zip (1.21 KB)

aarg:
Clever. But it has a discontinuity when millis() overflows and resets to zero. Because 2^32/1000 is not an integer. I think you can do the following:

void setup()

{
 pinMode(LED_BUILTIN, OUTPUT);      
}
void loop()
{
 digitalWrite(LED_BUILTIN, millis()/1024%2);
}



because 2^32/2^10 is an integer, 2^22.

If you post code again, please use code tags.

Hi
I don't undurstand why you divide millis()/1024?
2^32=4294967296; 4294967296/1000=4294967.296; 4294967.296%2=1;

I use this counting and works perfect thought millis() over count
In Arduino int=2^16 I thing.

ferojusko2:
Hi
I don't undurstand why you divide millis()/1024?
2^32=4294967296; 4294967296/1000=4294967.296; 4294967.296%2=1;

I use this counting and works perfect thought millis() over count
In Arduino int=2^16 I thing.

I said "integer" not int. Integers are a mathematical class of numbers, while ints are a C/C++ datatype. Don't get them confused, because C/C++ "long" type is also an integer.

Your math is correct. However, it is also the reason why your method won't work. It appears to work for you, because you didn't wait 42 days for millis to roll over. When it does, one of your pulses will be too short, and subsequent pulses will be out of sync.

Your last pulse before rollover is at t+4294967000 milliseconds. The next pulse should be at t+4294968000 milliseconds (which is evenly divisible by 1000). But at that time, millis will read 4294968000-4294967296 = 704. It should be evenly divisible by 1000, but it isn't. Now do you see?