开发者

How to run a pattern without using delay()

开发者 https://www.devze.com 2023-02-21 05:15 出处:网络
I want to run an LED patt开发者_JAVA百科ern again and again using the for(), and without interrupting the other code that is running. But have encountered the problem of using delay() too much.

I want to run an LED patt开发者_JAVA百科ern again and again using the for(), and without interrupting the other code that is running. But have encountered the problem of using delay() too much.

So, the BlinkWithoutDelay example repeats only one thing: turn the LED on, and off every second. If I were to do it with a pattern (and not just turning the LED on and off), how would I do it?

The problem is with millis()

What other options are there of running a pattern without using delay?


Agree your code would be nice to see, to underdatnd what you're trying to do. Assuming that the changes come LESS often than a loop without delay()'s in it, you can either use interrupts (actually very easy to set up on the arduino) or a library called Metro that lets you trigger timed events without using delay() or interrupts.


You've two options if you want to display a sequence, and have something else going on in the background.

First, you could sprinkle your sequence within the main loop(). It looks like a good few of the "LED chasers" and "KnightRider" effects on the internet are coded like this.

void loop () {
    // do something
    digitalWrite();
    // do something
    digitalWrite();
}

Or, you can use timer interrupts. This is a bit trickier to set up, but again, a quick internet search should bring up loads of examples. In this case, run the timer on the Arduino and set up the interrupts to trigger every xns. This will let the main loop do what it's doing, and every xns there will be a quick interruption to update your sequence.


If you want to run a time sensitive pattern, you might want to try using timer interrupts.

The tutorial here has a pretty good explanation and several examples of how to use the Arduino timer interrupts.

If you want to run the code while the led pattern is going on, I don't think it is possible on the arduino. That requires parallel processing of the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号