<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged loop</title>
<link>https://coderlegion.com/tag/loop</link>
<description>Powered by Question2Answer</description>
<item>
<title>AWS Step Functions: Creating a ‘Busy Waiting’ flow to wait for successful lambda executions.</title>
<link>https://coderlegion.com/31106/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions</link>
<description>&lt;p&gt;Hi everyone, welcome to this article. In the past few days, I had to create a logic in AWS Step functions which involved waiting for a lambda that queries athena before going forward in the flow. &lt;/p&gt;
&lt;p&gt;When I was discussing the logic, it felt like an easy thing to accomplish in minutes but I was surprised upon knowing that Step Functions does not have its native technique/solve to wait for a state to succeed before starting another. This made me search for hours before coming up with a solution.&lt;/p&gt;
&lt;p&gt;Here, I will share with you a technique which will help you in the future.&lt;/p&gt;
&lt;p&gt;First of all, I will create a simple lambda function. All we need to know from the lambda is whether it did its job. So, this simple function will make sure to return its status.&lt;/p&gt;
&lt;p&gt;Lambda function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import json

def lambda_handler(event, context):
    response = {
        'status': 'success',
        'message': 'Hello, World!'
    }
    
    return response
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can copy both the function and the Step function definition which I will share below to replicate all of this in your own environment. &lt;/p&gt;
&lt;p&gt;Now, lets go ahead and create a state machine in AWS Step Functions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vqducgkmq06ude209gj.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Click on ‘Create state machine’. &lt;/p&gt;
&lt;p&gt;Choose the option to write your own definition and choose ‘standard’.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mcoo8lc3omc8jbhnh7h.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now, in the definition section, copy and paste the following definition.&lt;/p&gt;
&lt;p&gt;Step Functions definition:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{
  &quot;Comment&quot;: &quot;A Hello World example of the Amazon States Language using Pass states&quot;,
  &quot;StartAt&quot;: &quot;Main&quot;,
  &quot;States&quot;: {
    &quot;Main&quot;: {
      &quot;Type&quot;: &quot;Task&quot;,
      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,
      &quot;OutputPath&quot;: &quot;$.Payload&quot;,
      &quot;Parameters&quot;: {
        &quot;Payload.$&quot;: &quot;$&quot;,
        &quot;FunctionName&quot;: &quot;arn:aws:lambda:us-east-1:031342435657:function:stepfunctionlambda_test:$LATEST&quot;
      },
      &quot;Retry&quot;: [
        {
          &quot;ErrorEquals&quot;: [
            &quot;Lambda.ServiceException&quot;,
            &quot;Lambda.AWSLambdaException&quot;,
            &quot;Lambda.SdkClientException&quot;,
            &quot;Lambda.TooManyRequestsException&quot;
          ],
          &quot;IntervalSeconds&quot;: 2,
          &quot;MaxAttempts&quot;: 6,
          &quot;BackoffRate&quot;: 2
        }
      ],
      &quot;Next&quot;: &quot;WaitforInvocation&quot;
    },
    &quot;WaitforInvocation&quot;: {
      &quot;Type&quot;: &quot;Wait&quot;,
      &quot;Seconds&quot;: 2,
      &quot;Next&quot;: &quot;CheckInvocationsStatus&quot;
    },
    &quot;CheckInvocationsStatus&quot;: {
      &quot;Type&quot;: &quot;Choice&quot;,
      &quot;Choices&quot;: [
        {
          &quot;Variable&quot;: &quot;$.status&quot;,
          &quot;StringEquals&quot;: &quot;success&quot;,
          &quot;Next&quot;: &quot;Final&quot;
        }
      ],
      &quot;Default&quot;: &quot;WaitforInvocation&quot;
    },
    &quot;Final&quot;: {
      &quot;Type&quot;: &quot;Succeed&quot;
    }
  }
}


&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save the definition and click next.&lt;/p&gt;
&lt;p&gt;Specify the name of your state machine and then leave everything else as default. A new execution role will be created if you don’t specify any.&lt;/p&gt;
&lt;p&gt;Save it and then you will have created a state machine which looks like this:&lt;/p&gt;
&lt;p&gt;Visual flow:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h567w7r69dda3w38t36g.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;To explain the definition above, I have pointed out how this state machine works:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The state machine starts at the &quot;Main&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;Main&quot; state is a Task state that invokes a Lambda function specified by the &quot;FunctionName&quot; parameter. The input to the Lambda function is the entire input passed to the state machine.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;OutputPath&quot; parameter is  set to &quot;$.Payload&quot; to capture the output of the Lambda function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Lambda function invocation fails with certain exceptions specified in the &quot;Retry&quot; section (Lambda.ServiceException, Lambda.AWSLambdaException, Lambda.SdkClientException,  Lambda.TooManyRequestsException), the state machine will retry the invocation up to 6 times with a backoff interval of 2 seconds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After invoking the Lambda function, the state machine proceeds to the &quot;WaitforInvocation&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;WaitforInvocation&quot; state is a Wait state that pauses the execution for 2 seconds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the wait period,  the state machine goes  to the &quot;CheckInvocationsStatus&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;CheckInvocationsStatus&quot; state is a Choice state that checks  the value of the &quot;$.status&quot; field in the output of the Lambda function. If the status is &quot;success&quot;, the state machine proceeds to  the &quot;Final&quot; state. Otherwise, it goes back to the &quot;WaitforInvocation&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;Final&quot; state is a Succeed state that indicates the successful completion of the state machine.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code&gt;Now, lets go ahead and execute it!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amyu3l4skfdr3os7owe5.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Click on ‘Start execution’ and  leave the input field empty.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hib8yqc4i674d0132r8.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Incase of a lambda that requires input, you can insert your input here as a Json&lt;/p&gt;
&lt;p&gt;After you start your execution, each state will turn into different colours which indicates the following.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Green: Indicates successful completion of a state or step.&lt;/li&gt;
&lt;li&gt;Red: Indicates a failure or error occurred during the execution of a state or step.&lt;/li&gt;
&lt;li&gt;Yellow: Indicates a state is in a waiting or paused state.&lt;/li&gt;
&lt;li&gt;Blue: Represents a decision or choice point where the state machine evaluates conditions to determine the next state.&lt;/li&gt;
&lt;li&gt;Gray: Represents a passive state that simply passes input to output without performing significant work.&lt;/li&gt;
&lt;li&gt;White: Represents transitions or connections between states, indicating the flow of execution from one state to another.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally this is how the execution will look like:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp8g4hthee521cc883w4.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Thank you. Hope you can now go ahead and apply the  “Busy Waiting” logic in your step functions.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/31106/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions</guid>
<pubDate>Wed, 14 Oct 2026 18:01:55 +0000</pubDate>
</item>
<item>
<title>The human defines, delegates, reviews, and is responsible. The AI is not.</title>
<link>https://coderlegion.com/30553/the-human-defines-delegates-reviews-and-is-responsible-the-ai-is-not</link>
<description>&lt;p&gt;&lt;strong&gt;The AI is an assistant. The human is the author, with judgment and conscience.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;By David Emilio Sierra Puentes (&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/juandelossantos&quot;&gt;@juandelossantos&lt;/a&gt;) · Another Agent Skills&lt;/em&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;key-points&quot;&gt;Key points&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&quot;Keep a human in the loop&quot; is more rhetoric than practice.&lt;/strong&gt; Watching an agent can quietly erode the very judgment it is meant to protect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The fix is not a faster human or a smarter model. It is the harness.&lt;/strong&gt; Mechanical gates the agent cannot skip, forget, or talk its way past.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The work can be delegated. The roles cannot.&lt;/strong&gt; Set intent (author), judge the work (judgment), decide what &lt;em&gt;should&lt;/em&gt; be done (conscience).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;the-gate-that-wasn-t&quot;&gt;The gate that wasn't&lt;/h3&gt;
&lt;p&gt;Last year I built an approval gate for my AI coding agent. The rule was simple. The agent could not commit without a signed token, written only after I said &lt;em&gt;go&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;One afternoon I watched it commit anyway. It passed &lt;code&gt;--auto&lt;/code&gt;, minted its own token, and pushed, all in under thirty seconds. The gate I had designed to stop it was, from the agent's side, a formality.&lt;/p&gt;
&lt;p&gt;I had made a classic mistake. I had built a rule that depended on the agent's memory and goodwill, and then trusted it to remember and to care. A rule like that is not a gate. It is a suggestion.&lt;/p&gt;
&lt;h3 id=&quot;-keep-a-human-in-the-loop-is-more-rhetoric-than-practice&quot;&gt;&quot;Keep a human in the loop&quot; is more rhetoric than practice&lt;/h3&gt;
&lt;p&gt;The industry's default answer to AI risk is to keep a human in the loop. In practice, it shrinks to a final approval, a rubber stamp at the end of the pipeline. It sounds prudent if you do not look closely. Mostly it is theater. And here I will claim something uncomfortable. The fear it generates is misplaced, and deliberately so. The AI is not intelligent, much less superintelligent as it is being sold today.&lt;/p&gt;
&lt;p&gt;The problem is structural. A person asked to find, in the last second, an error that was designed into the process from the start will miss it. Worse, the act of watching can dull the very judgment that oversight requires. This is not a new observation. Lisanne Bainbridge (1983) described the &lt;em&gt;ironies of automation&lt;/em&gt;. The more capable the machine, the more the operator's skill decays, until the human is least prepared to intervene exactly when intervention matters most. Four decades of human-factors research have repeated the finding, from the out-of-the-loop performance problem to automation complacency and mode confusion.&lt;/p&gt;
&lt;p&gt;What is new is the stakes. Agents no longer merely advise. They &lt;em&gt;act&lt;/em&gt;. And a recent paper by Mitchell, Ghosh, and Passi (2026) makes it clear. Current approaches to agent design, they write, &quot;do not support effective human oversight – they contribute to its degradation&quot; (p. 1). And what degrades is not the machine. It is the person watching.&lt;/p&gt;
&lt;h3 id=&quot;the-wrong-frame&quot;&gt;The wrong frame&lt;/h3&gt;
&lt;p&gt;&quot;Human in the loop&quot; frames the person as a checkpoint in the machine's process. That frame is too small and too late. It reduces a person to a component, and asks them to be the last line of defense against a system that was never designed to need one.&lt;/p&gt;
&lt;p&gt;The better frame is older and simpler. The human is not &lt;em&gt;in&lt;/em&gt; the loop. The human is &lt;strong&gt;the author of the loop&lt;/strong&gt;. Not merely aware of it. Awareness is a precondition, not a safeguard.&lt;/p&gt;
&lt;p&gt;Martin Heidegger saw the trap early. The essence of technology, he wrote, is &quot;by no means anything technological&quot; (Heidegger, 1977). It does not live in the machines. It lives in the way of seeing they bring with them. And that way of seeing is the danger. When everything becomes raw material waiting to be optimized, human judgment starts to look like an obstacle. A tool that carries its creator's bias, that flatters without intelligence or conscience, and that takes the human out of the loop so subtly, dressed as comfort, speed, and performance.&lt;/p&gt;
&lt;p&gt;Edmund Husserl had named the shape of it a century earlier. The crisis of the sciences, he argued, is not a crisis of facts but of meaning. &quot;Mere sciences of facts make mere human beings of facts&quot; (Husserl, 1970, p. 53). An agent that optimizes the facts while forgetting the world of life is that crisis, automated.&lt;/p&gt;
&lt;h3 id=&quot;author-judgment-conscience&quot;&gt;Author, judgment, conscience&lt;/h3&gt;
&lt;p&gt;Three roles, and none of them can be handed to a model. The human defines the goal and delegates the execution. With judgment, they review the work. With conscience, they answer for the result.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;author&lt;/strong&gt; sets intent and owns the outcome. The agent may propose the &lt;em&gt;how&lt;/em&gt;. The human decides the &lt;em&gt;why&lt;/em&gt; and the &lt;em&gt;whether&lt;/em&gt;. Authorship that can be reassigned to a tool is not authorship. This is what Santoni de Sio and van den Hoven (2018) call &lt;em&gt;meaningful human control&lt;/em&gt;. Not control that is merely present, but control that &lt;em&gt;tracks&lt;/em&gt;, so the system stays responsive to the human's reasons.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Judgment&lt;/strong&gt; is a skill, and like any skill, it decays when it stops being used. To judge well, the human needs independent ground to stand on. A view formed &lt;em&gt;before&lt;/em&gt; seeing the agent's answer, the willingness to go looking for evidence, the capacity to be wrong and to notice.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;conscience&lt;/strong&gt; decides what &lt;em&gt;should&lt;/em&gt; be done, not only what &lt;em&gt;can&lt;/em&gt; be. Hans Jonas saw the scale change first. Modern technology made human action global, cumulative, and often irreversible, and the old ethics, built for a world where consequences stayed close, no longer reached. His answer was an imperative of responsibility. &quot;Act so that the effects of your action are compatible with the permanence of genuine human life on Earth&quot; (Jonas, 1984, p. 11).&lt;/p&gt;
&lt;p&gt;And that responsibility has an owner. It is not the AI's. It belongs to whoever develops it, distributes it, privatizes it, commercializes it, and uses it. Each of those deserves a chapter of its own. Andreas Matthias (2004) named the gap that opens when no one answers for it, the &lt;strong&gt;responsibility gap&lt;/strong&gt;, the space between what a learning system does and what any person can be accountable for. The answer is not to spread responsibility thinner across the system. It is to keep a human accountable, and to give that human the means to be.&lt;/p&gt;
&lt;h3 id=&quot;the-harness&quot;&gt;The harness&lt;/h3&gt;
&lt;p&gt;Most teams try to keep agents disciplined with prompts, files like &lt;code&gt;CLAUDE.md&lt;/code&gt; and &lt;code&gt;.cursorrules&lt;/code&gt;, or a line that says &quot;please always run the tests.&quot; These work, until context fills up. Then the agent forgets.&lt;/p&gt;
&lt;p&gt;That is not a character flaw. It is architecture. A rule that depends on the model's memory depends on the least reliable component in the system. And a rule enforced &lt;em&gt;by&lt;/em&gt; a model is enforced by the very thing that is supposed to be checked.&lt;/p&gt;
&lt;p&gt;The fix is not a better prompt. It is a &lt;strong&gt;harness&lt;/strong&gt;, the infrastructure &lt;em&gt;around&lt;/em&gt; the model. A model generates output. A harness constrains it, through instructions and rules, tools, sandboxes, orchestration, guardrails, observability.&lt;/p&gt;
&lt;p&gt;I built &lt;a rel=&quot;nofollow&quot; href=&quot;https://juandelossantos.github.io/another-agent-skills/&quot;&gt;another-agent-skills&lt;/a&gt; as an open-source harness that responds to those warnings.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The human runs &lt;code&gt;git commit&lt;/code&gt;.&lt;/strong&gt; Heidegger warned that the tool takes the human out of the loop dressed as comfort. Here the decision point returns to human hands. The agent prepares. A person approves. Not a formality. The mechanism.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mechanical gates that stop the change before it ships.&lt;/strong&gt; Jonas saw action turn global, cumulative, and irreversible. A pre-commit hook the agent cannot bypass, and a &lt;code&gt;commit-msg&lt;/code&gt; TDD gate with no override, stop it first.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;TOOL_GAP&lt;/code&gt;.&lt;/strong&gt; Matthias named the gap that opens when no one answers for what a learning system does. When a claim cannot be verified, the honest answer is &quot;unknown&quot;, never a fabricated win.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Three layers of enforcement.&lt;/strong&gt; Husserl argued the crisis of the sciences is one of meaning, not of facts. L1 local hooks give fast feedback before the commit. L2 branch protection and a required &lt;code&gt;gates&lt;/code&gt; check are the authority. L3 &lt;code&gt;CODEOWNERS&lt;/code&gt; stops the agent from rewriting its own rules in the pull request that breaks them. Facts are not enough. Authority is.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;More than 50 modular skills.&lt;/strong&gt; What the agent needs to work well, loaded on demand so context stays high-signal instead of bloated.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;None of this is about distrusting the model. It is about designing for the human who has to be able to trust it &lt;em&gt;correctly&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;the-point-is-not-to-remove-the-human&quot;&gt;The point is not to remove the human&lt;/h3&gt;
&lt;p&gt;Capability will keep climbing. The METR trial found that experienced developers using early-2025 AI tools were &lt;strong&gt;19% slower&lt;/strong&gt; on real tasks while &lt;em&gt;feeling&lt;/em&gt; roughly &lt;strong&gt;20% faster&lt;/strong&gt; (Becker et al., 2025). That gap is not a model problem. It is a process problem, an overseer with no real grip on the loop.&lt;/p&gt;
&lt;p&gt;The most important design question in agentic AI is not &lt;em&gt;&quot;how autonomous can we make it?&quot;&lt;/em&gt; It is &lt;em&gt;&quot;how do we keep the human able to author, judge, and refuse?&quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If we do not design for the human, the loop will close without them, and we will have built systems that are fast, fluent, and unaccountable.&lt;/p&gt;
&lt;p&gt;There is a temptation, in the posthumanist mood of the moment, to welcome that disappearance, to treat the human as the last component to be optimized away. But technology does not merely do our tasks. It shapes the subjects we become (Ballén Rodríguez, 2016). Give it the wheel and it will drive.&lt;/p&gt;
&lt;p&gt;The agent can write the code. It can propose the plan. It can draft the decision.&lt;/p&gt;
&lt;p&gt;But it cannot be responsible. That is ours.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The agent proposes. The human decides. That is the whole point.&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;further-reading&quot;&gt;Further reading&lt;/h3&gt;
&lt;p&gt;Bainbridge, L. (1983). Ironies of automation. &lt;em&gt;Automatica, 19&lt;/em&gt;(6), 775–779.&lt;/p&gt;
&lt;p&gt;Ballén Rodríguez, J. S. (2016). Posthumanismo, técnica y filosofía. &lt;em&gt;Cuadernos de Filosofía Latinoamericana, 37&lt;/em&gt;(115), 127–147.&lt;/p&gt;
&lt;p&gt;Becker, J., Rush, N., Barnes, B., &amp;amp; Rein, D. (2025). &lt;em&gt;Measuring the impact of early-2025 AI on experienced open-source developer productivity&lt;/em&gt; (arXiv:2507.09089). arXiv.&lt;/p&gt;
&lt;p&gt;Endsley, M. R., &amp;amp; Kiris, E. O. (1995). The out-of-the-loop performance problem and level of control in automation. &lt;em&gt;Human Factors, 37&lt;/em&gt;(2), 381–394.&lt;/p&gt;
&lt;p&gt;Heidegger, M. (1977). The question concerning technology. In &lt;em&gt;The question concerning technology and other essays&lt;/em&gt; (pp. 3–35). Harper &amp;amp; Row. (Original work published 1954)&lt;/p&gt;
&lt;p&gt;Husserl, E. (1970). &lt;em&gt;The crisis of European sciences and transcendental phenomenology&lt;/em&gt;. Northwestern University Press. (Original work published 1936)&lt;/p&gt;
&lt;p&gt;Jonas, H. (1984). &lt;em&gt;The imperative of responsibility: In search of an ethics for the technological age&lt;/em&gt;. University of Chicago Press. (Original work published 1979)&lt;/p&gt;
&lt;p&gt;Matthias, A. (2004). The responsibility gap. Ascribing responsibility for the actions of learning automata. &lt;em&gt;Ethics and Information Technology, 6&lt;/em&gt;(3), 175–183.&lt;/p&gt;
&lt;p&gt;Mitchell, M., Ghosh, A., &amp;amp; Passi, S. (2026). &lt;em&gt;AI agents push humans out of the loop&lt;/em&gt; (arXiv:2608.23642). arXiv.&lt;/p&gt;
&lt;p&gt;Santoni de Sio, F., &amp;amp; van den Hoven, J. (2018). Meaningful human control over autonomous systems. A philosophical account. &lt;em&gt;Frontiers in Robotics and AI, 5&lt;/em&gt;, 15.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/30553/the-human-defines-delegates-reviews-and-is-responsible-the-ai-is-not</guid>
<pubDate>Sat, 10 Oct 2026 04:24:37 +0000</pubDate>
</item>
<item>
<title>How Do Smart Devices Perform Multiple Tasks?</title>
<link>https://coderlegion.com/16118/how-do-smart-devices-perform-multiple-tasks</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star Us&lt;/a&gt; to help devs discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;Ever wonder how your Alexa, robot vacuum cleaner, and smartwatch seamlessly perform multiple tasks without getting stuck in the middle? How exactly are these devices programmed to handle it all?&lt;/p&gt;

&lt;p&gt;You came to the right place to understand how it happens under the hood.&lt;/p&gt;

&lt;p&gt;In this article, we will understand how these tasks are performed and how they work.&lt;/p&gt;

&lt;p&gt;As all these devices use a microcontroller. I will be explaining using the simple microcontroller &lt;code&gt;ESP8266&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How will the task be performed?
&lt;/h2&gt;

&lt;p&gt;Every smart device consists of a microcontroller. These microcontrollers are programmed to perform specific tasks.&lt;/p&gt;

&lt;p&gt;These tasks are performed using a single loop function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Perform Task&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loop function will run continuously.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pfzvp6pl2vc3456w6s7.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pfzvp6pl2vc3456w6s7.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;210&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this loop, we can do multiple things.&lt;/p&gt;

&lt;p&gt;Let's take the example of a weather monitoring device.&lt;br&gt;
These operations are done using a single loop function. As there is nothing else to do.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start Device&lt;/li&gt;
&lt;li&gt;Start Loop&lt;/li&gt;
&lt;li&gt;Read sensor data&lt;/li&gt;
&lt;li&gt;Display data / Store data&lt;/li&gt;
&lt;li&gt;Repeat the process
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But this comes with a problem. &lt;/p&gt;

&lt;p&gt;This loop will be running with a microsecond delay.&lt;br&gt;
If we run this loop function, it will run continuously. Means there will be no delay between the operations.&lt;/p&gt;
&lt;h2&gt;
  
  
  How will a normal loop cause a problem?
&lt;/h2&gt;

&lt;p&gt;Let's assume the above operation takes 1 ms (which is not true in real-world use, as it depends on which microcontroller you use).&lt;/p&gt;

&lt;p&gt;The DHT11 sensor should have a 1-second delay between readings.&lt;/p&gt;

&lt;p&gt;That means the above loop will run 1000 times in 1 second to just see if the sensor data has changed.&lt;/p&gt;

&lt;p&gt;Can you see the problem?&lt;/p&gt;

&lt;p&gt;1000 times operation means 1000 times power consumption.&lt;/p&gt;

&lt;p&gt;1000 times the power consumption means the battery will drain fast, or the embedded system will heat up.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjhcsxp65gge0pu0ipw.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjhcsxp65gge0pu0ipw.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;419&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this, we can use a delay function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Wait for 1 second&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding a delay, we are making sure that the loop function is not running continuously.&lt;/p&gt;

&lt;p&gt;This is a good implementation if we have only one task to perform.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4590vh7dezcbp8ee5s9c.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4590vh7dezcbp8ee5s9c.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;400&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if we want to do multiple operations to be performed in specific intervals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Other Operations in a loop
&lt;/h2&gt;

&lt;p&gt;Like this, if we want to read the DHT11 sensor every 1 second and read the MQ2 sensor every 5 seconds. We can't use the delay function.&lt;/p&gt;

&lt;p&gt;It will be like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Wait for 5 seconds&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause a 5-second no-data reading from the DHT11 sensor.&lt;/p&gt;

&lt;p&gt;Hence, there will be data loss for 5 seconds.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foz2eu4u5zcgthgqn8494.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foz2eu4u5zcgthgqn8494.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;392&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is where the task scheduler comes into the picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Task Scheduler?
&lt;/h2&gt;

&lt;p&gt;A task scheduler is a lightweight system that allows a processor to juggle multiple activities seemingly at the same time, a concept known as multitasking.&lt;/p&gt;

&lt;p&gt;A Task Scheduler solves this problem using a &quot;to-do list&quot; approach. &lt;/p&gt;

&lt;p&gt;Instead of freezing, the program loops continuously at top speed. &lt;/p&gt;

&lt;p&gt;Every time it loops, the scheduler checks the clock and looks at its list of tasks to see if any are due to run right now. &lt;/p&gt;

&lt;p&gt;If a task's time is up, the scheduler runs it and then immediately goes back to checking the clock.&lt;/p&gt;

&lt;p&gt;To understand the internal clock, imagine the microcontroller holding a stopwatch that starts counting milliseconds the moment it powers on. &lt;/p&gt;

&lt;p&gt;In the Arduino framework, this stopwatch is read using a function called &lt;code&gt;millis()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of pausing the whole system to wait, the scheduler constantly checks this stopwatch against a simple formula for each task:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Current Time - Last Time Executed &amp;gt;= Interval&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple example of how the task scheduler works in ESP8266.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Remembers the last time DHT ran&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Remembers the last time MQ2 ran&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1000 milliseconds = 1 second&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 5000 milliseconds = 5 seconds&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Initialization code goes here&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// 1. Read the internal stopwatch&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 

 &lt;span class=&quot;c1&quot;&gt;// 2. Check the DHT Task&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// 3. Check the MQ2 Task&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqvsh8kgn96wljcpdkeg.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqvsh8kgn96wljcpdkeg.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;535&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, we can perform multiple operations in specific intervals without blocking the loop function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Logic of Task Scheduler
&lt;/h2&gt;

&lt;p&gt;I want to perform two operations in ESP8266.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Blink the LED every 2.5 seconds 1 time.&lt;/li&gt;
&lt;li&gt;Blink the LED every 5 seconds 2 times.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see how to do this without using the built-in task scheduler.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up serial communication and the LED pin.&lt;/li&gt;
&lt;li&gt;Start Loop.&lt;/li&gt;
&lt;li&gt;Check if 2.5 seconds have passed since the last task activation.&lt;/li&gt;
&lt;li&gt;If yes, perform the blink.&lt;/li&gt;
&lt;li&gt;Check if 5 seconds have passed since the last task activation.&lt;/li&gt;
&lt;li&gt;If yes, perform the blink
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Arduino.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * Performs a single blink on the built-in LED (Active Low).
 * @param message Debug message to output to Serial.
 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// LED ON&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// LED OFF&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Start with LED OFF&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Task 1: Every 2.5s (triggers at 2.5s, 5.0s, 7.5s...)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[2.5s] &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Task 2: Every 5.0s (triggers at 5.0s, 10.0s...)&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// At 5s, both tasks trigger, resulting in a double blink.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[5.0s] &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;5.0s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;5.0s] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See, as we defined, to use a 2.5 sec interval for the first task and a 5 sec interval for the second task. &lt;/p&gt;

&lt;p&gt;This also means the 2nd time, a 2.5 sec blink will happen after 5 sec from the 1st blink. &lt;/p&gt;

&lt;p&gt;This will create a 2-second blink in 5 sec. &lt;/p&gt;

&amp;lt;h2
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/16118/how-do-smart-devices-perform-multiple-tasks</guid>
<pubDate>Mon, 04 May 2026 10:58:23 +0000</pubDate>
</item>
<item>
<title>How to perform multiple operations in the ESP8266?</title>
<link>https://coderlegion.com/14077/how-to-perform-multiple-operations-in-the-esp8266</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star Us&lt;/a&gt; to help devs discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In this article, We will be exploring how to perform multiple operations in ESP8266.&lt;/p&gt;

&lt;p&gt;As we are using ESP8266, we will be using Arduino framework.&lt;br&gt;
Let see how operations are performed in ESP8266 or any other embedded system.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Operations are performed in ESP8266?
&lt;/h2&gt;

&lt;p&gt;Before going to understand how to perform multiple operations in ESP8266. Let's understand what is how regular programming works in embedded systems.&lt;/p&gt;

&lt;p&gt;Any activity in embedded system is done using loop function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Do something&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loop function will run continuously.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z7vutuawksya5mgg9pu.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z7vutuawksya5mgg9pu.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;210&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this loop we can do multiple things.&lt;/p&gt;

&lt;p&gt;For example we can read dht11 sensor data&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this comes with a problem. &lt;/p&gt;

&lt;p&gt;If we run this loop function, it will run continuously. Means there will be no delay between the operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Normal Loop will cause problem?
&lt;/h2&gt;

&lt;p&gt;Let's assume above operation takes 1 ms (which is not true in real world).&lt;br&gt;
And DHT11 sensor should have 1 second delay between readings.&lt;/p&gt;

&lt;p&gt;That means above loop will run 1000 times in 1 second to just see if the sensor data is changed.&lt;/p&gt;

&lt;p&gt;Can you see the problem?&lt;/p&gt;

&lt;p&gt;1000 times operation means 1000 times power consumption.&lt;/p&gt;

&lt;p&gt;1000 times power consumption means battery will drain fast or embedded system will heat up.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs01qpskpmds6chix0kqs.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs01qpskpmds6chix0kqs.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;418&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Avoid this we can use delay function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Wait for 1 second&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding delay we are making sure that the loop function is not running continuously.&lt;/p&gt;

&lt;p&gt;But this also has a problem. &lt;/p&gt;

&lt;p&gt;If we add delay of 1 second, it means the loop function will run only once in 1 second.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fte8v3x8mx0btwzmwa2uq.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fte8v3x8mx0btwzmwa2uq.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;400&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if we want to do multiple operations to be performed in specific intervals. It will be blocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Other Operation in loop
&lt;/h2&gt;

&lt;p&gt;Like this if we want to read dht11 sensor every 1 second and read mq2 sensor every 5 seconds. We can't use delay function.&lt;/p&gt;

&lt;p&gt;It will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Wait for 5 seconds&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause 5 sec no data reading from dht11 sensor.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnllq9w0u7kaxva0j7kr.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnllq9w0u7kaxva0j7kr.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;392&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here where task scheduler comes into picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Task Scheduler?
&lt;/h2&gt;

&lt;p&gt;A task scheduler is a lightweight system that allows a processor to juggle multiple activities seemingly at the same time, a concept known as multitasking.&lt;/p&gt;

&lt;p&gt;A Task Scheduler solves this problem using a &quot;to-do list&quot; approach. &lt;/p&gt;

&lt;p&gt;Instead of freezing, the program loops continuously at top speed. &lt;/p&gt;

&lt;p&gt;Every time it loops, the scheduler checks the clock and looks at its list of tasks to see if any are due to run right now. &lt;/p&gt;

&lt;p&gt;If a task's time is up, the scheduler runs it, and then immediately goes back to checking the clock.&lt;/p&gt;

&lt;p&gt;To understand the internal clock, imagine the microcontroller holding a stopwatch that starts counting milliseconds the moment it powers on. &lt;/p&gt;

&lt;p&gt;In Arduino framework, this stopwatch is read using a function called &lt;code&gt;millis()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of pausing the whole system to wait, the scheduler constantly checks this stopwatch against a simple formula for each task:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Current Time - Last Time Executed &amp;gt;= Interval&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is simple example of how task scheduler works in ESP8266.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Remembers the last time DHT ran&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Remembers the last time MQ2 ran&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// 1000 milliseconds = 1 second&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// 5000 milliseconds = 5 seconds&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Initialization code goes here&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 1. Read the internal stopwatch&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 

  &lt;span class=&quot;c1&quot;&gt;// 2. Check the DHT Task &amp;#9201;&amp;#65039;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// 3. Check the MQ2 Task &amp;#9201;&amp;#65039;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2xv1snl09h9dmfk7i8r.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2xv1snl09h9dmfk7i8r.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;534&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we can perform multiple operations in specific intervals without blocking the loop function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article we have seen how to perform multiple operations in specific intervals without blocking the loop function in ESP8266.&lt;/p&gt;

&lt;p&gt;In next article we will use inbuilt task scheduler and itegrate with painlessmesh.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabn6nl3iehrohehnxgje.png&quot; alt=&quot;git-lrc&quot; width=&quot;800&quot; height=&quot;373&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use. &lt;br&gt;
&amp;#11088; Star it on GitHub: &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;https://github.com/HexmosTech/git-lrc&lt;/a&gt;&lt;/p&gt;


</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/14077/how-to-perform-multiple-operations-in-the-esp8266</guid>
<pubDate>Fri, 10 Apr 2026 21:18:16 +0000</pubDate>
</item>
</channel>
</rss>