Do something with second delay without using delay function

Hi, how i can do something in program without using delay function? My program need run refresh display and cannot wait on delay. But it need show on display mixed patern every second. How i can run this?

My program looks like:
(i have declared previousDestruction on begin of program ofcourse)

void destruction(){
unsigned long currentDestruction = millis();

if (currentDestruction - previousDestruction > 1000 && currentDestruction - previousDestruction < 1500){
data[0] = B1111;
data[1] = B1111;
data[2] = B1111;
data[3] = B1111;
showSprite(speed);
}

if (currentDestruction - previousDestruction > 1500 && currentDestruction - previousDestruction < 2000){
data[0] = B0000;
data[1] = B0000;
data[2] = B0000;
data[3] = B0000;
showSprite(speed);
}

It works fine when i just put function destruction on main loop. But when i run after random time, the pattern run from random place and never from begin. How can i fix it?

Go to the IDE, click on "File", "Examples", "Digital" "Blink without delay"
Play with it.
Understand it, apply it.

How can blinkwithoudelay help me? Blink just switch state after second, and i need to more than two patterns, in my program i need 17 different pattern. Pattern2 one second after Pattern1 and must Pattern1 be first...

Play with it.
Understand it, apply it

You replied within twenty minutes of me posting - I don't believe you could possibly have explored all the potential of blink-without-delay.

:astonished:

Like in loop...

I need one second run one pattern, another second another patter, third second another pattern. What is common with blinking with led, where is just two states. I tried something before give question on this forum...

What is common with blinking with led, where is just two states.

“two” is just a special case of “lots”

Telimektar:
void destruction(){
unsigned long currentDestruction = millis();
if (currentDestruction - previousDestruction > 1000 && currentDestruction - previousDestruction < 1500){
It works fine when i just put function destruction on main loop. But when i run after random time, the pattern run from random place and never from begin. How can i fix it?

In order to restart at the beginning of this pattern, then you need to make the first test TRUE again:

 if (currentDestruction - previousDestruction > 1000 && currentDestruction - previousDestruction < 1500){

So, if you made currentDestruction minus previousDestruction equal to 1001 you’d be fine. And since currentDestruction is the current millis() you really only need to do this:

currentDestruction - previousDestruction == 1001
...becomes
millis() - previousDestruction =1001
...becomes
previousDestruction = millis()-1001;

So if after a random interval you set previousDestruction like this, it should start off right away

and i need to more than two patterns, in my program i need 17 different pattern.

That means you need to repeat the pattern 17 times.
MIght be a put in some timing array…

currentDestruction - previousDestruction == 1001
…becomes
millis() - previousDestruction =1001
…becomes
previousDestruction = millis()-1001;

Always use inequallities when using millis() in an boolean expression
so use >= or <= iso ==
MInd you; if millis() needs just one more tick somewhere you might find the expression false

  if (currentDestruction - previousDestruction > 1000 && currentDestruction - previousDestruction < 1500){
    data[0] = B1111;
    data[1] = B1111;
    data[2] = B1111;
    data[3] = B1111;
    showSprite(speed);
  }

  if (currentDestruction - previousDestruction > 1500 && currentDestruction - previousDestruction < 2000){
    data[0] = B0000;
    data[1] = B0000;
    data[2] = B0000;
    data[3] = B0000;
    showSprite(speed);
  }

what if … currentDestruction - previousDestruction == 1500

the block can be optimized a bit by using an if else ladder - one boolean expression less - to

unsigned long duration = currentDestruction - previousDestruction;
 if (duration > 1000 && duration < 1500){
    data[0] = B1111;
    data[1] = B1111;
    data[2] = B1111;
    data[3] = B1111;
    showSprite(speed);
  } else if (duration  < 2000){
    data[0] = B0000;
    data[1] = B0000;
    data[2] = B0000;
    data[3] = B0000;
    showSprite(speed);
  }

Thanks guys, i not have time to optimised code, so i repeat function as mad. Today a try your new way. :slight_smile:

robtillaart:
currentDestruction - previousDestruction == 1001
…becomes
millis() - previousDestruction =1001
…becomes
previousDestruction = millis()-1001;

Always use inequallities when using millis() in an boolean expression

It wasn’t an expression in code - I was pointing out that 1001 satisfied the requirement of restarting the sprite, such that x=1001 satisfies 1000<x<1500; the rest is just walking it into a valid assignment, one step at a time, so the progression isn’t too abrupt to follow.

My code is for PHASER DOLPHIN from STAR TREK. Mode autodestruction works fine when i activate it 10 seconds after power up. When im too fast, i jump into middle of “destruction”.

I have another problem, i need set one LED to different of brightness and i not have any free PWM pins. I think the solution with software PWM for all pins is overhead for only two different brightness (30% 70%) on one LED. I understand princip of PWM but i not have any idea for easy realization without delay.

with

void LED(){
  delay (1);
  digitalWrite(13, HIGH);  
    delay (1);
  digitalWrite
}

i have 50% brightness and flickering on main matrix display…

void LED(){
  delay (1);
  digitalWrite(13, HIGH);  
    delay (1);
  digitalWrite
}

Posting code that won’t even compile wastes everyone’s time.

I want post here complete code, but there is limit on characters.

Telimektar:
Hi, how i can do something in program without using delay function?

Playground article - How to avoid using delay()

Telimektar:
I want post here complete code, but there is limit on characters.

Use the upload function and attach the file (or a zip of the files). While you're writing your post, there is an "Additional options" below the edit window

Telimektar:
I have another problem, i need set one LED to different of brightness and i not have any free PWM pins.

It might be best to focus on fixing the one problem first before trying to fix the other unrelated one. However, as has been already pointed out, you can do your own PWM using another pin.

Here is complete code. I know, it needs more modification but priority is function.

phaser.pde (9.2 KB)

OK, I’ve looked at the code - what I do not understand on this thread is what your question now is (it probably changed as this thread has evolved)

As far as I’ve understood the code, you’re loading a “pattern” into the data array, and then calling sprite() to display it. Does that work as you want it, or what is it doing instead?

A tiny note:

for(byte onlyOne = 1; onlyOne < 1; onlyOne++){
    <somecode> ;
  }

will not execute; the onlyOne value at initialization is greater then the test condition. But that may be a temporary fix while testing.