In the image attached below, I want to replace the delay() with something (millis()?) Which can generate required delay without blocking other tasks in the code. Can someone please help me?
Thanks!!
In the image attached below, I want to replace the delay() with something (millis()?) Which can generate required delay without blocking other tasks in the code. Can someone please help me?
Thanks!!
Strictly speaking the problem is not the delay(), rather it is the for loop. Why not use the loop() function to do what its name implies ?
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 1000;
void setup()
{
Serial.begin(115200);
}
void loop()
{
currentTime = millis();
if (currentTime - startTime >= period)
{
Serial.println("Hi");
startTime = currentTime;
}
//other code that must not be blocked goes here
}
Ah I am aware of this method.
But my application is that I need to print something on a 16x2 lcd display from a buffer. The size of the buffer is 10 and I using for loop to print them on the screen (kind of vertical scroll).
Then add a counter to the code
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 1000;
byte counter =0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
currentTime = millis();
if (currentTime - startTime >= period)
{
Serial.println(counter++);
counter = counter % 10;
Serial.println("Hi");
startTime = currentTime;
}
//other code that must not be blocked goes here
}
is a tricky way to count 0 to 10 9 . % is the modulus-operator it returns the remainder
1 % 10 = 1 1 divided by 10 does not fit remainer is 1
2 % 10 = 2 2 divided by 10 does not fit remainer is 2
...
10 % 10 = 0 10 fits into 10 remainder 0
Here is an example / tutorial how to use void loop() as a for-loop-replacement.
best regards Stefan
Are you sure that it counts from 0 to 10 ?
you are right it counts from 0 to 9
@nachiiiketa03
it seems you want print Hi just 10 times every second.
Just combine the example "Blink without Delay" with the check of a counter (replacing the for loop).
I would do that in this way:
// https://forum.arduino.cc/t/help-with-delay-function-replacement-inside-a-for-loop/1142792/4
// to be deleted 2023-09
uint32_t previousMillis; // will hold the last call
const uint32_t interval = 1000; // the periode (replacing the "delay")
byte counter = 0; // replacing i of for loop
void setup()
{
Serial.begin(115200);
}
void loop()
{
uint32_t currentMillis = millis();
if (counter < 10 && currentMillis - previousMillis >= interval ) // do something in the defined interval but just n times
{
Serial.print(counter++); Serial.print(" "); // just as demo
Serial.println("Hi");
previousMillis = currentMillis;
}
//other code that must not be blocked goes here
}
to simulate:
Create a new Arduino Uno simulation project, run it in your browser and share your code and schematics.
Hi, thanks a lot. Unfortunately, it did not workout. So here is my code which I am calling from the loop.
void display_text(char **ptr)
{
U16 passed_delay = get_vertical_scroll_delay();
U8 row_pos = get_passed_string_pos();
for(int i=0; i<no_of_str; i++)
{
if (i<(no_of_str-1))
{
lcd.setCursor(0, row_pos);
lcd.print(ptr[i]);
lcd.setCursor(0, (!row_pos));
lcd.print(ptr[i+1]);
delay(passed_delay);
lcd.clear();
}
else
{
//do nothing
}
}
}
Now, in the code.. the delay() needs to be replaced with something which does not blocks.
You can't do that. You can't call a function one time and expect it to run without blocking other code that is outside of that function. The function must exit, for the other code to run. So you have no choice but to complicate things a little.
Here is a small example of what you can do, without delay
( and also without lcd.clear
) : t1142792 - Wokwi ESP32, STM32, Arduino Simulator
I really appreciate you took time to write/find the code. Thanks a lot!!
@guix @noiasca @paulpaulson @StefanL38 @UKHeliBob
Thanks a lot for all the help guys. I have figured out the logic and this code works for me:
void display_text(char **ptr)
{
if (i >= (no_of_str - 1))
{
i = 0;
}
U16 passed_delay = get_vertical_scroll_delay();
U8 row_pos = get_passed_string_pos();
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis >= interval) && (i < (no_of_str - 1)))
{
lcd.setCursor(0, row_pos);
lcd.print(ptr[i]);
lcd.setCursor(0, (!row_pos));
lcd.print(ptr[i + 1]);
i++;
previousMillis = currentMillis;
}
}
Here i is static to the file.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.