I am relatives new to arduino and dont understand how to Code millis
Check out this guide
1. The millis() is a function that you can use to know the elapse time since you have RESET the Arduino UNO to begin the execution of a sketch.
2. The millisCounter is a 32-bit unsigned counter that continues recording of the elapse time at 1 ms interval on interrupt basis in the background.
3. You can use the following code to know and record the time accumulated by the millisCounter by executing he following code:
unsigned long int presentMillis = millis();
If you have understood the above three theory, then you can easily apply millis() function to compute the time gap between the occurrence of two events.
Example:
Assume you have turned on the onBoard LED-L of Arduino UNO board now at time t1. You want to turn off LED-L exactly after 1-sec (1000 ms) without blocking the MCU (Microcontroller).
If you propose the following solution that will be wrong as the delay(1000); function blocks the MCU which means that the MCU cannot do any other job until the declared 1-sec time has elapsed. The MCU is blocked.
You can use mills() function as illustrated below to see when the desired 1-sec (1000 ms) time has gone; at the same time, you can also assign some othet task to the MCU for execution like checking if a button has been closed.
unsigned long int presentMillis = millis(); //record pres time t1
void setup()
{
pinMode(13, OUTPUT); //LED-1 is connected with DPin-13
digitalWrite(13, HIGH); //LED-1 is on at time t1 = n ms
}
void loop()
{
if (millis() - presentMillis >= 1000) //1-sec = 1000 ms time has elapsed
{
digitalWrite(13, LOW); //LED-L off; time gap = millis - t1 = n+1000 - n = 1000 ms
while (true); //wait here for ever
}
else
{
//code to check if a Button is closed and then take action
}
}
As this is not a question about the IDE, I have moved your post to this section. Please be more careful where you post in future.
For extra information and examples look at
- Using millis() for timing. A beginners guide
- Several things at the same time
- Flashing multiple LEDs at the same time
EDIT @AresXT had already posted those
If three LEDs are contiuously blinked at different rates using millis() function in Arduino UNO Platform, can it be termend as "multi-tasking"?
Hello hyperjragon
Welcome to the best Arduino forum ever
You can select a project such as a traffic light system or similar to start with.
I would say it depends at what level you look
For an observer looking at the leds, it seems that the arduino is doing multiple tasks at once, so colloquially you could say it’s multitasking
For a developer looking at the code, he will see only one process/thread/task as commonly referred to in software engineering, so it’s not.
I apologize to OP for hijacking the thread for a while.
1. If I blink those three LEDs at different rates using RTOS, then the process is really a multi-tasking process - am I correct?
2. Now when a developer looks at the codes, he observes that there are three tasks running concurrently -- is it correct?
Perhaps start a new thread with your new question.
Thank you for the advice.
I don't have enough queries so as to justify a new Thread. I have just wanted to know yes/no answer from the veteran poster on-the-fly.
it all depends on how you define multi-tasking or the word process
In a general Operating System, a process is a program in execution, consisting of code, data, and resources. A thread is a subset of a process that can run independently, allowing for multitasking within a single process and we use Tasks (colloquially) to refer to the units of work that need to be executed, which can be processes or threads.
With this definition, tasks are scheduled by the OS based on their priority and timing requirements and are the individual units of work, processes represent the general program, threads enable concurrent execution within a process,.
But not everyone goes with that definition, in FreeRTOS, a task is the units of execution that can run independently and concurrently within the operating system. Each task in FreeRTOS has its own stack and program counter, similar to threads in other operating systems.
so you need to clarify the definitions of the words you use for "multi-tasking process"
In a nutshell, if you have one unit of work for each led to blink and let the OS schedule them, then you are colloquially multi-tasking or multi-threading or multi-processing.
Plain and popular English for the conceptual subject. Your write-up has worked as a strong reinforcement and add-on for what I have already known on multi-tasking playing with FreeRTOS in Arduino UNO and ESP32 Platforms. I am adding your post #13 in my Class Lecture Sheets.