Hello, sorry for my question, I’m still at very beginner of Arduino, and I’m try to understand how it work the loop function.
Suppose I have a 2 function one that move forward and another one that move backward 2 dc motor for my robot.
If i put this 2 func in the void loop {} of Arduino IDE like this:
void loop {
moveforward();
movebackward();
}
For how long Arduino is going to move forward before to step to the other function move back? (inside my move forward func i don't have any timing, just switch on the motor and move forward) is it going forever forward and never step on the backward?
For how long Arduino is going to do the single instruction inside the loop if I don’t set any timer or delay.
dm1886:
Hello, sorry for my question, I’m still at very beginner of Arduino, and I’m try to understand how it work the loop function.
Suppose I have a 2 function one that move forward and another one that move backward 2 dc motor for my robot.
If i put this 2 func in the void loop {} of Arduino IDE like this:
void loop {
moveforward();
movebackward();
}
For how long Arduino is going to move forward before to step to the other function move back? (inside my move forward func i don't have any timing, just switch on the motor and move forward) is it going forever forward and never step on the backward?
Show your moveforward() and movebackward() functions.
Keep in mind that a "single instruction" is not the same as a function. A call to "digitalWrite(...)" may involve a hundred or more machine instructions. Machine instructions, such as "nop" (an assembler mnemonic) may only only take a single clock cycle or 62.5nS to complete but if your function consists of a hundred such instructions -- when translated from C++ to actual machine code -- your function may take 100x 62.5nS to run.
For how long Arduino is going to do the single instruction inside the loop if I don’t set any timer or delay.
You won't really know without looking at the code in assembler an counting clock cycles or getting an idea by putting a pin high when a function starts and then low when it finishes and scoping that pin as the program runs.
Generally speaking these are speedy little machines but you can code them "badly" so functions take way longer than they need to. The function delay(...) for example, is often badly implemented by new programmers because it really slows program execution.
What problem are you having and can you post your whole code?
first of all you are asking good questions. Your question asks for a specific thing.
You can ask as many questions as you like. If the other users can see that you make some progress to walk up the learning-curve they will answer again and again and aigain.
the loop does what its name says loop-ing.
it startets with the first command below the functions name "loop"
down to the the very last and then jumps up immetiately to the top again.
How long this takes depends on the number of commands and in case of functions how long the function takes to "run through" and this can vary from a few microseconds up to infinity long.
Really infinity long. If you program a loop that never stops again your program will loop as long as the powersupply is switched on.
So there is no "standard"-time how long this takes.
As you are learning I suggest that you take a look into this tutorial
Take a look into this tutorial: Arduino Programming Course
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try if you find this kind of explaining enjoyable.
Here is a code that does measure how long it takes count up a single variable inside of a while-loop which does in pricniple the same thing as function loop(). Only difference:
loop() is the most outer loop inside Arduino-IDE.
unsigned long myCount = 1;
unsigned long StartTime;
unsigned long StopTime;
unsigned long Cnt_target = 1000000;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
}
void loop() {
myCount = 0;
StartTime = micros();
Serial.print("counting up myCount starts at ");
Serial.println(myCount);
while (myCount < Cnt_target) {
myCount++;
}
Serial.print("counting up myCount stopped at ");
Serial.println(myCount);
StopTime = micros();
Serial.print("StartTime");
Serial.println (StartTime);
Serial.print("StopTime");
Serial.println (StopTime);
Serial.print("counting up from 0 to ");
Serial.print (Cnt_target);
Serial.print( " needed ");
Serial.print(StopTime - StartTime);
Serial.println(" microseconds");
Serial.print("This means a single loop needs ");
Serial.print( (StopTime - StartTime) * 1000 / Cnt_target);
Serial.println(" nanoseconds");
Serial.println();
Serial.println();
}
You got loop() and in loop() there are 2 functions called doA() and doB(). What will happen is that doA() will run, when the code in doA() is done then doB() will be ran. When the code in doB() is ran, then doA() will run, when the code in doA() is done then doB() will be ran. When the code in doB() is done, then doA() will run, when the code in doA() is done then doB() will be ran. When the code in doB() is done, then doA() will run, when the code in doA() is done then doB() will be ran. When the code in doB() is done, doA() will run, when the code in doA() is done then doB() will be ran. When the code in doB() is done, then doA() will run and will continue to run like such until acted upon by an disturbing event. It's a loop and does the loopy thing.