blink lib's

okay so im kinda new at this still, but i have a mega i will be using to creat a flash pattern for led's i have a mosfet driver circuit that is made s I'm all good for hardware but making a code for it is painful looking and scrolling though 8 x 150 line of code for each pattern. could i not just make a pattern using a blink code, then rebfrefrence it into the main code as a library? How would i save it? thanks

Can you share your code first? I am assuming that you have 8 LEDS to flash and you want them to go through 150 patterns. You can do it with array but post your code first.

For repeating stuff, consider writing a function. After all, you don't want to embed the code that is behind a digitalWrite or analogWrite tens of times in your code; so you write a function for that (OK, somebody already did that for you) :wink:

as an example it will be

//|startup|
// Loop

if input one //(coming from a computer touch screen interface ill need help with that later)
{
led pattern 1-
(led 1 HIGH)
(led 2 HIGH)
(led 3 HIGH)
(led 4 LOW)
(led 5 LOW)
(led 6 HIGH)
(led 7 HIGH)
(led 8 LOW)
(led 9 LOW)
(led 10 HIGH)
(led 11 HIGH)
(led 12 HIGH)
(led 13 HIGH)
(led 14 HIGH)
(led 15 LOW)
(led 16 LOW)
(led 17 LOW)
(led 18 LOW)
(led 19 HIGH)
(led 20 HIGH)

;delay 500;

(led 1 HIGH)
(led 2 HIGH)
(led 3 HIGH)
(led 4 LOW)
(led 5 LOW)
(led 6 HIGH)
(led 7 HIGH)
(led 8 LOW)
(led 9 LOW)
(led 10 HIGH)
(led 11 HIGH)
(led 12 HIGH)
(led 13 HIGH)
(led 14 HIGH)
(led 15 LOW)
(led 16 LOW)
(led 17 LOW)
(led 18 LOW)
(led 19 HIGH)
(led 20 HIGH)

;delay 500;

(led 1 HIGH)
(led 2 HIGH)
(led 3 HIGH)
(led 4 LOW)
(led 5 LOW)
(led 6 HIGH)
(led 7 HIGH)
(led 8 LOW)
(led 9 LOW)
(led 10 HIGH)
(led 11 HIGH)
(led 12 HIGH)
(led 13 HIGH)
(led 14 HIGH)
(led 15 LOW)
(led 16 LOW)
(led 17 LOW)
(led 18 LOW)
(led 19 HIGH)
(led 20 HIGH)
//....
//x10

if input two
{
LED pattern 2
// blablabla
// lots of code again

//.... and repeat until all buttons have an assigned patten

could i not just make a library with pattern and reference lib1 or lib2 where that pattern would be stored? just at work right now trying to prepare myself for when i get home with my code.

also i have a question regarding my led's can i assign a delay on each led that old repeat say ..

LED 1,10,11 & 20 blink at 500ms
while LED 2,9,12 & 19 blink at 450ms?
and on and on?

in stead of writing them all as a high low with a delay?

Something like this - use arrays and blink without delay for timing

byte pinArray1[] = {2,3,4,5,6,7,8,9,10,11,}; // leave 0,1 free for serial
byte pinArray2[] = {12,13,14,15,16,17,18,19,20,21,};
byte x;
unsigned long currentMillis;
unsigned long startTime1;
unsigned long endTime1;
unsigned  long duration1 = 500UL;
unsigned long startTime2;
unsigned long endTime2;
unsigned  long duration2 = 450UL;
pins1 = 0;
pins2 = 0;
void setup(){
for (x=0; x<10; x=x+1){
pinMode (pinArray1[x], OUTPUT);
pinMode (pinArray2[x], OUTPUT);
}
void loop(){
currentMillis = millis(); // capture the current time
// 1st set of pins
if ((currentMillis - startTime1) >= duration1){ // enough time passed?
startTime1 = startTime1 + duration1; // set up for next time interval;
pins1 = 1 - pins1; // 1-0-1-0-1-0 // toggle output state to be driven
for (x=0; x<10; x+x+1){
digitalWrite(pinArray1[x], pin1); // write the outputs
}
// next set of pins
if ((currentMillis - startTime2) >= duration2){
startTime2 = startTime2 + duration2;
pins2 = 1 - pins2; // 1-0-1-0-1-0
for (x=0; x<10; x+x+1){
digitalWrite(pinArray2[x], pin2);
}
}

Just for demo purposes
declare an array with the bit patterns

unsigned long pattern1[] = {

  0x55555,
  0xC3E67,
  0x12345,

};

You can loop through the bits

void setup() {
  Serial.begin(9600);
  for (int patterncnt = 0; patterncnt < 3; patterncnt++)
  {
    for (int bitcnt = 0; bitcntcnt < 20; bitcntcnt++)
    {
      bool value = (pattern1[0] & (1 << bitcntcnt)) == (1 << bitcntcnt);
      Serial.print("bit "); Serial.print(bitcntcnt);
      Serial.print(", value "); Serial.println(value);
    }
    delay(500);
  }
}

I’ve used Serial.print. You can use CrossRoads’ pinArray approach and replace the Serial.print by digitalWrite(pinArray[cnt], value);.

The above code can cater for up to 32 LEDs, the for loop limits it to 20. The pinArray also needs 20 elements in that case.

whats the advantage to either?

My approach does not use delay, so you can have 2 different time selections going on.
A third way is to specify the time for each pin individually:

byte pinArray[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,};
unsigned long cycleTime[] = {450, 500, 450, 500, 600, 650, 600, 650, 700, 750, 700, 750,450, 500, 450, 500, 600, 650, 600, 650, };
unsigned long pinTime[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte pinState[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};

then in loop, something like this should be pretty close:

void loop(){
currentTime = millis();
for (x=0; x<20; x=x+1){ // add more pins if desired, increase arrays above accordingly
if ((currentTime - pinTime[x]) >= cycleTime[x]){ // time for a change?
pinTime[x] = currentTime + cycletime[x]; // set up the next time
pinState[x] = 1 - pinState[x]; // change the output state
digitalWrite (pinArray[x], pinState[x]); // write the output pin with the new state
}
}

okay i thank thats more i deal because i want to have them all blink at the same time but on differ cycles. however... how can a make two the same time but blink alternatively? AND is it possible to make something blink twice and sustain for a time. go on on for 50 off for 50 on for 400 to =500ms?
example

pin2 and pin 11 @ 1000ms alternatively
pin3 and pin 10 @ 500ms (on50 off50)then hold until the remainder(400)
pin4 and pin 9 @ 250ms alternatively

if you understand what I'm getting at i hope you can forgive my last of understanding. dyslexic at nature XD

Anything is possible.
2 & 11 for example - give them the same duration (1000), but change their starting pinState to 1 and 0.

2 & 9 - same thing - same duration, change their starting pinState to 1 and 0.

3 & 10 - trickier. Maybe create 4 duration arrays, with on time/off time/on time/off time.

For all pins, on time/off time/on time/off time would be the same (1000, or 250), but for 3 & 10 could be 40/40/400/0 (maybe 399/1 if things act weird).
Repeat this for the 4 arrays, with cycleTime0, cycleTime1, cycleTime2, cycleTime3,

for (x=0; x<20; x=x+1){ // add more pins if desired, increase arrays above accordingly
if ((currentTime - pinTime[x]) >= cycleTime[x]){ // time for a change?
pinTime[x] = currentTime + cycletime[x]; // set up the next time
pinState[x] = 1 - pinState[x]; // change the output state
digitalWrite (pinArray[x], pinState[x]); // write the output pin with the new state
}

okay so in theory i just change the start state. from start on to off and vice versa. okay awesome.

I have a few more questions and its ppm related. whats the code for making the fade in and fade out in a directional travel.. "knight rider" to the left , right or centre out. each pattern is going to need its own trigger Left arrow, right arrow and centre-out.

Knight Rider, that's totally different than just pulsing on & off.
I don't have enough time to gin that up at the moment. You could just have say 10 arrays with the pattern you want to display, example outer to inner:

1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,
0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,
0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0
0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0
0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0
0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0
0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
then repeat in reverse order

Just google arduino knight rider :slight_smile:

just looking for some help assimilating it in with the rest is all :stuck_out_tongue: ah ill take a look on my console at home

A blink without delay library/function would be no good unless the sketch that uses it has no execution-blocking code of its own.

BWD (blink without delay) does teach how to make things happen on time but the REAL lesson is to not block and run a fast loop(). Without that you can if ( millis() - startMillis >= waitMillis ) all you want and the sketch will run poorly.

Full lesson written in common sense and code.

---- snip ----

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time.
Glance at watch from time to time. When one minute is up then ...
Start cooking bacon. Look at watch and note the time.
Glance at watch from time to time. When another minute is up then ...
Put coffee on. Look at watch and note the time.
When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

Crossroads has been very helpful to your original question, but the otiginal spec is creeping up a bit!
Mixing the idea of PWM with arbitrary sequencing is quite possible, but I suggest you should get a firm understanding of the sequencing first, and the various methods available - as each has it's benefits.

The simplest way (for a beginner) to achieve the mixed-mode operation - is to put the sequencing on the high side, and the PWM on the low side (or v-v), but if you are a bit more advanced, you can perform TDM to achieve the PWM result - on the sequenced outputs. (You have to read from here!)

okay and @ crossroads 2 things.
FIRST.. THANK YOU FOR YOUR HELP
second. can to make a code for a blink w/o delay from pin 4 to 12 for so i can underatand and build off it? or anyone i wold appricate it. :slight_smile: not too worried abt the knight rider at this time, i would always do a IF() and then make a blink w/ delay in there baiesd on a trigger from a input(right?)

"can to make a code for a blink w/o delay from pin 4 to 12 for so i can underatand and build off it? "
I've already done that, but from 2 to 21. Time for you to start building off it. Surely you can edit the arrays and such to support less pins?

sorry about grammar btw, im at work right now trying to do 8 things at once. also i ran it into my arduino and got nothing maybe im not setting it up right.. found something that explained yours out nicely im gonna try and tinker with it and work off what you have givvn me ... again all of us as ontario safety solutions and myself specifically, thank you