(I should also note that we could minimize and optimize the above program using C++, but I’m trying to keep things as simple and understandable as possible.) If we decided to add more LEDs, all flashing at different rates, then it would be trivial to modify our latest program to achieve this. The alternative, of course, is to return to using the millis() function as illustrated below: (Source: Max Maxfield)Īlthough this involves a bit more code, it makes our lives much, much easier. The worse-case scenario is that we stop pressing the button before the delay times out, in which case our program wouldn’t know the button had been pressed and, therefore, wouldn’t respond at all. Of course, this might not be a problem if we are working with delays in the order of 100 milliseconds (1/10 of a second), but what if our LEDs were flashing only once every 100 seconds or so? As a best case, we would have to hang around waiting for the delay to end so our program could check the state of the switch and respond. We can’t respond to anything while a call to the delay() function is clunking along, which means our program would have to wait until the end of the delay before testing the state of the button. To be honest, we can certainly manage this by adding various states and tests and making multiple calls to the delay() function, but this approach would quickly become more effort than it’s worth.įurthermore, suppose we desired to add some additional functionality at some stage down the line, such as turning one of our LEDs hard On or Off when a button was pressed. But now suppose that we want to blink our LEDs at different rates - say 100 and 150 milliseconds for LED1 and LED2, respectively. Similarly, if we decide we want to flash two LEDs at the same time (i.e., at the same rate), then we could do so as follows: (Source: Max Maxfield) Why does this matter? To be honest, if all we want to do is flash a single LED, then it doesn’t matter at all. During a delay(), all the Arduino is doing is twiddling its metaphorical thumbs while it counts “0, 1, 2, 3, 4, 5…” For example, let’s re-create our program in the “blink” style as illustrated below:įrom a trainee’s perspective, using the delay() function is much simpler, so why would anyone wish to employ the millis() approach? Well, one problem with using the delay() function is that it prevents the Arduino from performing any other task(s). After all, the first program an Arduino user is traditionally exposed to - the “blink” sketch - achieves the same effect using the delay() function. One thing a novice might ask is why we opted to employ the millis() function to control the flashing of the LED as illustrated in the previous program. Since so many things are confusing for beginners, I prefer to take things one step at a time (I’d love to hear your thoughts on this and receive your feedback as to whether you like or dislike my approach). We will return to consider how this test performs its magic before we do so, however, let’s first ponder the question as to why we are using the millis() function in this way.īe warned! Experienced users may consider the following discussions to be a tad trivial in nature, but this column isn’t really intended for presently proficient programmers - instead, it’s targeted at beginners who are befuddled, bemused, and bewildered by everything that is coming their way. In the case of the test performed on Line 22, we noted that this code will happily continue to work, flashing the LED without any change in delay and without any other disruption, even when the millis register overflows back to 0. As part of those discussions, we created the following program, which uses the millis() function to turn a LED On and Off again every 100 milliseconds. In my previous column, we discussed the fact that the time returned by the Arduino’s millis() function will overflow (wraparound) to 0 every 49.71 days. It’s possible to write one’s programs in such a way that they can accommodate an overflow/wraparound condition in the millis() function, but how does this actually work?
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |