More efficient code

How do you write more efficient code for:

  #define RED1 10
  #define BLUE 13
  #define RED2 9  

  pinMode(RED1, OUTPUT); 
  pinMode(BLUE, OUTPUT);  
  pinMode(RED2, OUTPUT);

Would this work? Can you put defined variables into arrays?

  #define RED1 10
  #define BLUE 13
  #define RED2 9  
  
  int myOutputs[] = {RED1,BLUE,RED2};
  
  for (int i=0; i<3; i++) {
    pinMode(myOutputs[i], OUTPUT); 
  }

If

#define RED2 9

gives you trouble with the array, use

const byte RED2 = 9; // think this is the correct format

instead.

A #define basically say 'wherever this text appears in the source, replace it with this text at compile time' so it should work OK, but the code output by the compiler will not be more efficient. It would, however, improve readability of the source.

Thanks guys!

command_z:
Would this work? Can you put defined variables into arrays?

You've written the code, why don't you try it?

How do you write more efficient code for:

Can you explain why you think the second code is more "efficient", by any definition of efficient, than the first code? You replaced 6 lines of code with 7 lines.

Good point. I was wondering that before I posted this. I was trying to put my new knowledge of arrays to work, but I guess in this case, simpler is better.

Thank you.

Ideally, you would define all the pins as declare them as inputs or outputs, so you've got a good start on that.

command_z:
How do you write more efficient code for:

  #define RED1 10

#define BLUE 13
 #define RED2 9

pinMode(RED1, OUTPUT);
 pinMode(BLUE, OUTPUT);  
 pinMode(RED2, OUTPUT);




Would this work? Can you put defined variables into arrays?



#define RED1 10
 #define BLUE 13
 #define RED2 9  
 
 int myOutputs[] = {RED1,BLUE,RED2};
 
 for (int i=0; i<3; i++) {
   pinMode(myOutputs[i], OUTPUT);
 }

Now why would anyone prefer the second method over the first? Just because it uses more 'sexy' stuff (for loop, an array) doesn't mean it's better, if it is indeed faster or uses less memory, which it probably doesn't.

The first example is simple to understand and six months later when you look at it you you won't say "what the *&%^ was I thinking here?" :wink:

Also good code has good comments added. Try adding readable comments to that second example

Lefty

retrolefty:
Now why would anyone prefer the second method over the first?

I would prefer it if it made the code more maintainable or reduced duplication. In the current scheme, where pins are named and also included in the array, that duplication offsets the improvement that would have come from doing the initialisation in a loop. If the LED pins are always accessed individually, identifying them in an array gives no benefit and the first approach would make more sense. However, if there are pieces of code that do things to all LEDs then the second approach would result in more compact and maintainable code for that. In other words, you need to know how the data is being used before you can design the best way to store it.

Or just do it both ways - named & arrayed- and leave how it open for use later on. The compiler will get it all reduced down nicely, yes?

A lot depends on the compiler--and also, you could help the array version along by making your array constant. Will it unroll your loop? If so, and especially if you make the array an array of const int, it might not make any difference.

I'd be tempted to go with the array, anticipating possible changes; maybe someday you'll want to add an element or elements to it. OTOH, the Arduino is very constrained in resources, so if it's important, test and see which is better in the context of your entire program--or rather, "sketch".

  #define RED1 10
  #define BLUE 13
  #define RED2 9

Personally I would make constants, well, constant:

const byte RED1 = 10;
const byte BLUE = 13;
const byte RED2 = 9;

Now why would anyone prefer the second method over the first?

Yes, I prefer it, as it is extensible, like if you have 20 of them. It looks neater.