I have been working on a project that requires me to edit code that was written by someone else. I am very new to the Arduino language (I have had one college class on MatLab) and the person who wrote this code is much more skilled than I am with the language.
Currently I am struggling with the below problem.
The program begins a void loop(){
has some code in it
and then the loop ends
Then the program continues on with another void function
I am confused as to how the program gets out of the loop and continues moving forward with the program. I will copy and paste the code I am confused with below.
void loop(){
//RESET COUNTER
unsigned int luint_rechargeCount = 0;
// CHECK TIMER
if (timerMetro.check() == 1) {
collectData(true,true); // Collect Data (UpdateScreen, RecordData)
// INCREMENT RECHARGE COUNT
if (gflt_opticalSensorValue < guint_rechargeTrigger) ++luint_rechargeCount;
}
}
// TEST IF NEEDS TO BE RECHARGED (less than 3 then do the do loop again c.s.)
while(luint_rechargeCount<3); //
}
Sorry if my question is at all confusing. I am still getting used to the program and how to talk about it. Unfortunately I really haven't had anyone to talk about this coding with.
The program does not get out of loop. Loop runs forever.
The subroutine named void collectData is called from within loop - so as loop runs, it jumps out to the subroutine, does that code, then goes back into loop and keeps running.
So you could copy/paste the code in collectData into loop and not jump back & forth at all if you wanted. That is called "inline code" and is how I write mine - everytime you jump back & forth to a subroutine it takes a little time, which can be seen as a small loss of performance.
Okay, I see how it uses the values of updateScreen and recordData later in the code. Thank you again. This really helped me understand the coding a lot better.
// DISPLAY DATA
if (updateScreen) {
updateGraph();
updateText();
}
cstepien:
Okay, I see how it uses the values of updateScreen and recordData later in the code. Thank you again. This really helped me understand the coding a lot better.
// DISPLAY DATA
if (updateScreen) {
updateGraph();
updateText();
}
// RECORD DATA TO SD CARD
if (recordData) {
....
Right! When collectData(boolean updateScreen, boolean recordData) is called, with (true,true), those values are placed into the local variables called updateScreen and recordData.
And it turn, when updateGraph(); or updateText(); are called, those functions are jumped to, execute, and return to the collectData() function. So, your sequence happens as follows:
loop()
collectData(true, false)
updateGraph()
back to next statement in collectData()
updateText()
back to end of collectData()
back to next statement in loop()
@cstepien - you need to start using code tags. Please read the "sticky" at the start of the programming questions section.
Second, it doesn't "jump" it "calls". That is, a function call results in control transferring to that function, and then back again when the function exits.
It's conceptually very similar to using Arduino's built-in functions, except that the built-in functions are, well, built-in 8), and don't have to be defined in your own code.
the values "myPin" and "HIGH" are passed to the built-in function digitalWrite, which is processed accordingly. Then when digitalWrite is finished, the next line, an analogWrite in my example is processed.
The obvious advantage to using your own functions is that if you do the same sequence of code more than once it saves you re-typing or copying the code. A less obvious advantage, even if you use a sequence of code once, is to give your code more clarity. Keep the calculation out of the way, so to speak, in a function and just have a calling line in the body of the code.