does anyone know the syntax for calling more than one specified position in an array, not using a for loop? for instance, I only want to call position one and two, is there something along the lines of
ledArray[1,2]
that will work?
does anyone know the syntax for calling more than one specified position in an array, not using a for loop? for instance, I only want to call position one and two, is there something along the lines of
ledArray[1,2]
that will work?
Is there a problem with just calling them seperatly, one after the other? What context are you working in?
/me
it's not a problem. I know how to use for loops, etc. I was just curious, is there a way to do it other than using a for loop, or
for a 7 position array
digitalWrite(ledArray[1], HIGH)
digitalWrite(ledArray[2], HIGH)
it seems like something you should be able to do, but i can't figure out the syntax. tried stuff like
ledArray[1,2]
ledArray[(1,2)]
you know. just wondering
I don't think this is possible as it involves more than one int calling the array at once and causing digitalRead() or any functions to process more than one thing at once which is not possible/they cannot do.
Sorry
/me
Nope. All iteration is done "aboveboard." Not like scripting languages or vector-heavy platforms like Matlab or Perl or Haskell. If you want something done twice, there's got to be a loop somewhere you can see it.
Why not write a function?
void digitalWrite(int first, int last, int value)
Then just code your for loop in the function.
Sidenote: Why are HIGH and LOW defined as int's. Wouldn't it make more sense if they were bools? Does this have anything to do with avr compiling in c vs c++?
HIGH and LOW are just macros for 1 and 0 - they have no type at all. If you want a function to only use one byte on the stack, declare the appropriate argument or return value that way.
Sidenote: Why are HIGH and LOW defined as int's. Wouldn't it make more sense if they were bools?
There are a lot of wierdness going on when it comes to arduino and datatype selection, if you ask me.
Not exactly good practice or the most efficient solution. But it might be, or seem, user friendly. :
I expected them to be uint8_t (basically a byte), in case on wanted to add more 'states' such as HIGHZ for instance.
[edit]...it would seem that they are bytes, and one could add HIGHZ :)[/edit]
Halley: My question should have been "Why does digitalRead return an int"
Fusive: if you start down that path, you will end up asking a lot of questions, as I have done. I've actually made a thread on reforming datatype pracitces. (It kind of took on a life of it's own, and now those I teach arduino to are using a superset of arduino functions + some classes and structs I've created.)
I've never started to change all ints to bytes where suitable, just because it will be a lot of wasted work when time comes and the developers does not want to add the changes to the release. It does not seem to be something that the arduino core worry about. And I do actually kind of see why.
Arduino is about user experience, after all.
I was, however, pleased to see that the examples in the 'Getting started with Arduino' book had changed all ints to #defines, which I understand is more intuitive than const byte, for instance.
Thanks for the respone AlphaBeta.
One of the biggest questions I had when i first witnessed arduino code was why ints were used when a #define is perfectly suited for the job.
What bothers me most is that that the tutorials use ints. http://arduino.cc/en/Tutorial/Blink