How to use millis() like delays.

So i know how to make something blink with millis(), but I dont know how to make something where I need different delay values, for example: digitalwrite(pin, HIGH); delay(200); digitalwrite(pin, LOW); delay(500); so I need to make a code that does this, but with millis(), is this possible? thanks
8 Replies
AnonEngineering
AnonEngineering2mo ago
you set up a variable for each "delay" you need i.e. prevPin1Time, prevPin2Time, etc. then setup the various INTERVAL values https://wokwi.com/projects/436781592561891329 even better https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all
DarwinWasWrong
DarwinWasWrong2mo ago
void delayme(int thedelay)
{
long time_now = millis();
for (;;)
{
if (millis() - time_now > thedelay - 1)
{
time_now = millis();
break;
}
}
}
void delayme(int thedelay)
{
long time_now = millis();
for (;;)
{
if (millis() - time_now > thedelay - 1)
{
time_now = millis();
break;
}
}
}
DarwinWasWrong
DarwinWasWrong2mo ago
Delay function millis - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
TheStudier
TheStudierOP2mo ago
okay thanks so much
AnonEngineering
AnonEngineering2mo ago
were you asking about a toggle button the other day?
TheStudier
TheStudierOP2mo ago
yes, but I got It working
jim lee
jim lee2mo ago
!delay(1) - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
jim lee
jim lee2mo ago
There's paramiters you can set to get the timing you desire.

Did you find this page helpful?