filmov
tv
Arduino Tutorial: A Better Delay Function
![preview_player](https://i.ytimg.com/vi/mNmUdTqggjA/maxresdefault.jpg)
Показать описание
In this video i show how to write a bettery delay function for your arduino sketches.
Arduino Code:
Video Script:
"Welcome to the arduino programming series. In this video i will teach you how to write a better delay function for your sketches.
The default delay function works fine in most cases, but it freezes the micro controller, and doesn't allow the coder to run code while waiting. QUite often it is necessary to run code during the delay process.
Another way to make it better is by adding Options, in this case i add an option to read pins after delaying.
I Assign the name DELAY to the function, and then add 2 variables for it to be passed. delayTIME for amount of time to delay, and readPINS for the option to read pins when finished.
The equation for the delay time is simple. Time started - Time now = Time delayed. I used the variable tmpTIME to store the start time, then used the following loop to repeat until the delayed time is achieved. Other code can go in the loop to execute while waiting, but remember. Don't call the delay function in the delay function.
At the end of the function, an if statement is placed to check the option readPINS. If the value is true then it runs the function to read the pins.
This is a simple function where the code to read the pins will be placed.
It is not necessary but if we want to replace the original delay function, we need to redefine it as our new function. To call the new function we use the following. This will delay for 1 second and then call the readPINS function before continuing.
I add in the serial print to test if the new functions are working.
The code works just as it should.
I add in a counter to further test the code.
The functions work and I plan to use them in my future sketches.
Leave down in the comments what features you would add and why? I hope you've learned somethin from this video, have a great day!"
Arduino Code:
Video Script:
"Welcome to the arduino programming series. In this video i will teach you how to write a better delay function for your sketches.
The default delay function works fine in most cases, but it freezes the micro controller, and doesn't allow the coder to run code while waiting. QUite often it is necessary to run code during the delay process.
Another way to make it better is by adding Options, in this case i add an option to read pins after delaying.
I Assign the name DELAY to the function, and then add 2 variables for it to be passed. delayTIME for amount of time to delay, and readPINS for the option to read pins when finished.
The equation for the delay time is simple. Time started - Time now = Time delayed. I used the variable tmpTIME to store the start time, then used the following loop to repeat until the delayed time is achieved. Other code can go in the loop to execute while waiting, but remember. Don't call the delay function in the delay function.
At the end of the function, an if statement is placed to check the option readPINS. If the value is true then it runs the function to read the pins.
This is a simple function where the code to read the pins will be placed.
It is not necessary but if we want to replace the original delay function, we need to redefine it as our new function. To call the new function we use the following. This will delay for 1 second and then call the readPINS function before continuing.
I add in the serial print to test if the new functions are working.
The code works just as it should.
I add in a counter to further test the code.
The functions work and I plan to use them in my future sketches.
Leave down in the comments what features you would add and why? I hope you've learned somethin from this video, have a great day!"
Комментарии