Newbie question - Index looping

Although I started off with machine code, then assembler many, many moons ago, I never really got into C/C++. Actually I skipped across to scripting (with a smidgin of Fortran that I don't remember), but I do have a fair mount of experience at that even at low levels. So please have patience with an old dude trying to push back the frontiers of ignorance on an hourly basis :-[

I've only started with the Arduino this week but am having great success building a control system for an aircraft cabin simulator (just a tiny one), so I'd like to get as proficient as possible. The Arduino is fast as compared to a PLC, which I need !!

My problem is that I can't work out how to do (what I would call) index looping.

The code I have works fine but is long due to repeating chunks of code that I would normally place inside an indexed loop. Meaning, I have similar code that must get processed but with, say, different inputs, outputs and maybe variables.

In scripting I would normally use an index loop such as;

//Declare the variables int foo1; int foo2; int foo3; int mVar1; myVar2; myVar3; int myOutput1; myOutput2, myOuput3;

//do the indexed loop. this is just a silly sample. for i = 1 to 3; put input (foo & i) into bar; bar = (bar + (myVar & i); put bar into (myOutput & i); next i

The closest thing I have found is to set up arrays, but that seems to be a complex solution to what I would have thought is a simple problem.

But, is there a simple solution? :-/

Can anybody help please ?

Many thanks, Bob...

//Declare the variables
int foo1; int foo2; int foo3;
int mVar1; myVar2; myVar3;
int myOutput1; myOutput2, myOuput3;

//do the indexed loop. this is just a silly sample.
for i = 1 to 3;
put input (foo & i) into bar;
bar = (bar + (myVar & i);
put bar into (myOutput & i);
next i

In c++:

//Declare the variables
int foo[3] = {2,4,6};
int myVar[3] = {1,2,3};
int myOutput[3] = {0}; // all null

//do the indexed loop. this is just a silly sample.
for (byte i=0; i<3; i++){ 
  //put input (foo & i) into bar
  int bar = foo[i];

  //bar = (bar + (myVar & i)
  bar += myVar[i];

  //put bar into (myOutput & i);
  myOutput[i] = bar;
  //Serial.println( myOutput[i] );//will print 3, 6 and 9
}

Wowzy, many thanks for such a quick reply !! It's simple when you know how, but I still prefer scripting ;)

Thanks again.... Bob...

Wowzy, many thanks for such a quick reply !! It's simple when you know how, but I still prefer scripting ;)

Thanks again.... Bob...

C++ takes some 'getting used to' , but I've fallen in love with it, and I bet you will too ;)

And by the way: Welcome to the community!

It's nice to be welcomed, thanks and I'm sure I will get to love it as much as you !! BTW, is it acceptable to this forum to ask if there are folks in my area (I'm close to Vancouver, BC) that use Arduino's or the equivalent, and if so what area would I post such in ?

You can use the Bar Sport section.

And, if you end up finding a group of people (or want to create one) that starts to arrange 'get togethers' / workshops, you can post info in the Workshops section. :)

Tnx…

Do you guys test loops by using a separate compiler and then after perfecting the loop you add it to the Arduino code? I would like to be able to test loops and non related C++ code without using the arduino so I can debug it faster. I need a Freeware compiler I guess but which one should I use?

I have a Windows PC

Arduino uses winavr which is avr-gcc and it is as free as the air you breathe. Atmel's AVR Studio also uses it to compile code.