Let's Make Robots!

Accessing an array's size in C? (Arduino)

Hey,

I'm doing a little series of flashing LEDS, sort of like the knight rider examples you see everywhere.

 

Anyway, I'm trying to make sure I've got clean code - and from what I understand of C, this is valid. However also from what I understand, the Arduino uses it's own form of C, so certain things are bound to be a bit different.

Here's the code I'm having trouble with.


#include<vector>
#include<string> 
 
int leds[] = { 12, 11, 10, 9, 8 };
int tempo = 300;

void setup() {
  for (int i = 0; i <= leds.size(); i++ {
    pinMode(leds[i], OUTPUT);
  }
}

 
And the error...
 
17: error: vector: No such file or directory In function 'void setup()':
Bad error line: -3
  
In order for leds.size() to work, I need to include the vector and string libraries I've read. However, those aren't found when I compile the code. I'm not exactly sure where to go from here as C is relatively new to me.
 
Any thoughts would be greatly appreciated!
 
Thanks. 
 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

That code is exactly what you would want if you were working in actual C (although you forgot to close you ")" in your for loop) however arduino C is different. It does not use the standard C libraries so take out the include statements.

The arduino uses a command called Sizeof to do this sort of thing:

 http://arduino.cc/en/Reference/Sizeof 

 Also, you will need a void loop(){}. All arduino programs require one even if you dont do anything in them. 

Thanks Slick.

I didn't post the loop because I had that working fine. The code I had originally worked, I just wanted to make it as clean as I knew how to. I hate hardcoding things when simple loops will do them for me - makes it much easier to add things later on so there's not so much copy/paste. Plus I get more room for other things that way.

Thanks again for lending a hand!