Arduino Tutorial: A Better Delay Function

preview_player
Показать описание
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!"
Рекомендации по теме
Комментарии
Автор

Thank you. I needed this. You are awesome! It works, but I made a couple modifications it to make it even more versatile for my personal projects application.
Modification#: I changed the identifier from delay to delay_alt, and the value from 1000 to 1 .
Reason I did this: I had a variety of delay times for various pixel animations and other things. So if I needed 10ms, 85ms, or 1526ms, I was able to change existing delays and use their custom values. This also kept the default delay available for this I actually need to pause on. It works.
Please see modified code below.


//redefine original delay with our new function
#define delay_alt DELAY

void setup() {
}

void loop() {
delay_alt(1, true);
}

void DELAY(long delayTIME, boolean readPINS){
//set var to current time
long tmpTIME = millis();

//loop until delaytime is reached
do{
//code to execute while waiting
}while (millis() - tmpTIME < delayTIME);

//check if readpins is true
if (readPINS == true)
READPINS();
}

void READPINS(){
//analogRead(A0);
}

louiscelenza
Автор

Can I delay a keyboard input with this code? And not loop

chadunnering