How to control Arduino without using delay()
What is a delay()?
We delay()ishave a functiontutorial that pauses the program for the amount of time (in milliseconds) specified as a parameter. (1000 milliseconds = 1 second.)about delay()
isand veryhow commonlyto usedcode butwithout itusing hasit. itsHere drawbacks.we Itwill doestry notto just pause one sensor or actuator that you wish but pauses everything controlled bysimplify the sameprocess Arduino andusing the same code. This often leads to the slow down of sensors and thus is not accurate anymore.FireTimer
read morelibrary.
GettingReplace starteddelay()
There is aan example that is modified from the built-in example calledBlink
code which demonstrates controlling timing without using BlinkWithoutDelaydelay()
. Rather than pausing everything to keep the light on/off for a specific amount of time, we are setting up a timer to count the time for the actuator to action. In this example, you can see the value of the variable interval is actually equal to the same amount of delay() you will need to get the same result of blinking every 1 second.
//#include constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;"FireTimer.h"
// theCreate numbera ofFireTimer theobject
LEDFireTimer pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)ledTimer;
void setup() {
// set theInitialize digital pin LED_BUILTIN as output:an output
pinMode(ledPin,LED_BUILTIN, OUTPUT);
// Initialize the FireTimer with a delay of 1000 milliseconds
ledTimer.begin(1000);
}
void loop() {
// check to see if it's time to blink the LED; that is,Check if the differencetimer //has between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();fired
if (currentMillis - previousMillis >= interval)ledTimer.fire()) {
// save the last time you blinkedToggle the LED previousMillisstate
static bool ledState = currentMillis;LOW; // Keep track of the current LED state
ledState = !ledState; // Toggle the state
digitalWrite(LED_BUILTIN, ledState);
}
}
Change Timer in the loop()
This is the code to keep the LED on for 1 second and off for 2 seconds using the FireTimer library.
#include "FireTimer.h"
// Create a FireTimer object
FireTimer ledTimer;
void setup() {
// Initialize digital pin LED_BUILTIN as an output
pinMode(LED_BUILTIN, OUTPUT);
// Start the FireTimer with an initial delay of 1 second (LED ON duration)
ledTimer.begin(1000);
}
void loop() {
// Check if the timer has fired
if (ledTimer.fire()) {
// Toggle the LED state
static bool ledState = LOW; // Keep track of the current LED state
// Toggle the state
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
// Update the timer for the next duration:
// 1 second when LED is offON, turn2 itseconds onwhen andLED vice-versa:is OFF
if (ledState == LOW)HIGH) {
ledStateledTimer.begin(1000); =// HIGH;LED is ON for 1 second
} else {
ledState = LOW;
}ledTimer.begin(2000); // set the LED withis theOFF ledStatefor of2 theseconds
variable:
digitalWrite(ledPin, ledState);}
}
}
Multiple Actuators and Multiple Timers
You can set up multiple timers for multiple actuators, like giving each person a watch and ask them to action based on their own watches.
Wiring
There are three wires to connect on the Arduino side:
- LED short pin (-) to Ground
- LED long pin (+) to PWM Digital pin (D3), via 220Ω resistor
Code
In this code we are adding one more LED to the circuit. The built-in LED will stay the same, on and off every 1 second. The second LED will be one for 1 second and off for 5 second.
const#include int"FireTimer.h"
ledPin// =Create LED_BUILTIN;FireTimer objects for two LEDs
FireTimer led1Timer; // Timer for LED1 (1-second interval)
FireTimer led2Timer; // Timer for LED2 (3-second interval)
const int ledPin2LED1_PIN = 13; // Pin for the first LED
const int LED2_PIN = 3; unsigned// longPin previousMillisfor =the 0;second unsigned long previousMillis2 = 0;LED
void setup() {
// Initialize the LED pins as outputs
pinMode(ledPin,LED1_PIN, OUTPUT);
pinMode(ledPin2,LED2_PIN, OUTPUT);
// Start the timers with their respective intervals
led1Timer.begin(1000); // LED1 blinks every 1 second
led2Timer.begin(3000); // LED2 blinks every 3 seconds
}
void loop() {
ledPattern();
ledPattern2();
}
void ledPattern(){
unsigned long currentMillis = millis();
digitalWrite(ledPin, LOW); //turn offCheck atif the beginningtimer //afterfor 1s,LED1 lighthas up, ie 1000fired
if (currentMillis - previousMillis >= 1000)led1Timer.fire()) {
static bool led1State = LOW; // Keep track of LED1 state
led1State = !led1State; // Toggle LED1 state
digitalWrite(ledPin,LED1_PIN, HIGH)led1State);
}
//after lightCheck upif the timer for 1s,LED2 off,has ie 1000+1000 = 2000fired
if (currentMillisled2Timer.fire()) -{
previousMillisstatic >bool led2State = 2000){
digitalWrite(ledPin, LOW);LOW; //reset timerKeep previousMillistrack of LED2 state
led2State = currentMillis;
}
}
void ledPattern2(){
unsigned long currentMillis2 = millis();
digitalWrite(ledPin2, LOW);!led2State; //turn offToggle atLED2 the beginning
//after 5s, vibrate, ie 5000
if (currentMillis2 - previousMillis2 >= 5000) {state
digitalWrite(ledPin2,LED2_PIN, HIGH)led2State);
}
//after vibrating for 1s, stop, ie 5000+1000 = 6000
if (currentMillis2 - previousMillis2 >= 6000){
digitalWrite(ledPin2, LOW);
//reset timer
previousMillis2 = currentMillis2;
}
}