Hello all,
I am quite new both with programming C++ and Arduino. I have a question regarding processing efficiency and relations with programming techniques. For the codes that I wrote, the processing efficiency was not an issue. But my next proyect will require more careful decisions and I want to make sure I understand the logic, I cannot find any clear directions on this.
Does it make any difference the way of programming the following basic things in terms of processor efficiency after compiling , or is only a matter of readibility on the code before compiling ?
Maybe to formulate this first question without examples, I would ask ... do we really need to use variables even if they never change their value in our program flow, and assign the value of digitalRead to a variable instead of using it directly in a logic statement ? ... I ask because I believe to see this done quite often when reading sketches online.
Example A (the way I see most used)
#define pinIn A0
int pinRead ;
int var = 1023 ;
void setup() {
pinMode (pinIn, INPUT) ;
}
void loop() {
pinRead = analogRead (pinIn) ;
if ( pinRead == var) { // do whatever
}
}
Example B
#define pinIn A0
void setup() {
pinMode (pinIn, INPUT) ;
}
void loop() {
if ( analogRead (pinIn) == 1023) { // do whatever
}
}
Now the second doubt ... same doubt BUT with functions. Does it really make sense to make a very short (one two or three lines) function and call it just a few times in a sketch, instead of typing directly the instructions when needed ? I am not talking about a function that is going to be called 20 times, so it is not about typing economy and sketch simplicity ... sometimes I see sketches with 20 functions and each function is called only once or twice, where it would be simpler to just put the instructions directly where the functions are called.
As an example, here goes example C (replacing what is done directly in the loop with a function)
#define pinIn A0
int pinRead ;
int var = 1023 ;
void myFunct() {
// do whatever
}
void setup() {
pinMode (pinIn, INPUT) ;
}
void loop() {
pinRead = analogRead (pinIn) ;
if ( pinRead == var) {
myFunct();
}
}
All additional input and directioning will be appreciated. Thanks a lot !!