Set Multiple Variables in a FOR LOOP

I need to set about 15 different variables all with sequential numbering can I do this in a for loop? Something like...

for (i=1; i<=15; i++) {
  const int buttonPin_{i} = {i};
  const int outputPin_{i} = {i};

  boolean thisOutputState_{i} = LOW;
  boolean prevButtonState_{i} = LOW;
  boolean thisButtonState_{i} = LOW;
}

Yes. Open your book, or wherever you get you learning fix and get ready.

You want to use array variables. You made up some syntax, the correct syntax is just as straightforward.

You will learn that in C , arrays have N elements that you get to say what N is, and the array elements are at indexes that will range from 0 to (N - 1).

It looks like you are also winging it on how to declare, define and the use variables of any type, so I will suggest you slow your role a bit and learn about basic variable types and how to use them in C++.

Are you using any book or youtube series or websites in particular?

Anything can turn up dozens of hits. If you add Arduino, you can get some focused results.

  arduino variable types

or like

  arduino array variables

HTH

a7

At least you don't use blocking delays, but the buttons in real do "bounce" which when your loop is fast enough turns 1 press into many.

Some good get you started tutorials on buttons, timing and state machines:

Nick Gammon complete on switches/buttons.

Nick's Do Multiple Things At The Same Time --- intro to non-blocking and millis timing.

Nick complete again. The most important part is the State Machine section about halfway down.

With these and practice you can code automation that doesn't require interrupts, about 98 or 99% without misusing interrupts.

I run button handling as a task function that watches button pins over time and stores those as bits read twice a ms. Every new read pushes the pin state bits up and the last is lost but I only care about the latest 8 reads as pin state history. In binary with button down == zero and up == one, when history is 10000000, that is button up to button down for 7 reads taken as transition and stable state for 3.5 ms in a single value. I use 2 byte vars per button to do that. One function to update pin histories and the code to Process only reads history byte to know the button from the noise.
Your version should be what you can understand that works and you evolve that as you grow in coding. Yes, I care that you improve. The links above should give you solid grounding to build on and Nick is a master at common-sense explanations.

Ahh, so it's better to use it as an array rather than distinct variables?

I'm a backend PHP developer and Arduino strikes me as a more frontend javascript'esque language which I'm not too up with.

I was using Paul McWhorter on YouTube - https://www.youtube.com/playlist?list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP but I can't find anywhere about using the for loop in the setup

Arduino uses C/C++. It may be obscure to some.
The big difference to a PC is the hardware that makes me nostalgic for the early 80's.

OK, here's a secret I can tell you.

Arduino is programmed in C++. Period.

So learn about C, and add as much C++ as you are up for or need.

As I said, to focus any searching for C++ that will be talked about mre in the exact circumstances of "embedded system programming", Arduino, for example, add "arduino".

A for statement can appear in any function.

  arduino for statement

and

arduino functions

a7

some comments

  • variable defined (i.e. int varName) within a block (i.e. the braces) are local to that block and don't exist outside of it. you presumably want the variable defined globally, at the top of the program outside of any function definitions
  • variables are indexed with brackets (e.g. varName [i])
  • the value , your "{i}" should just be "i" (no brace nor brackets

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.