do this to any variable that equals y, do that to any variable that equals x

I have a general question.

Lets say i have four variables

a = 1
b = 1
c = 1
d = 2

what i want to do is

if (any variable == 1 ) {
do this;
}

else if (any variable == 2){
do the other thing;
}

it needs to check all variables.

If you only have four variables, maybe something like:

if (a == 1 || b == 1 || c == 1 || d == 1){
  // do your thing
}

if (a == 2 || b == 2 || c == 2 || d == 2){
  // do some other thing
}

But, I almost guarantee there's a better way to do that - I'm learning myself. As I said, if you have a small, definite number of variables you want to check, the above could work...but if you have say 10 variables that you want to check, then I bet there's a better way to code that.

Maybe using Array variables? This may not work exactly like you're thinking, but it does work. I set an array variable with 4 values. Then, I have two FOR loops checking to see if anything is either 1 or 2. If 1, light my blue LED. If 2, light the red.

const int blueledPin = 8;
const int redledPin = 9;
int myVariables[4] = {1,2,1,4};

void setup(){
  pinMode(blueledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
}

void loop(){
  for (int i = 0; i < 3; i++){
    if (myVariables[i] == 1){
      digitalWrite(blueledPin, HIGH);
    }
    if (myVariables[i] == 2){
      digitalWrite(redledPin, HIGH);
    }
  }
}

That works for simple code, but I think there's likely a better solution if someone else has any ideas.

hmm. those might work.

i think what i really need is another variable.

Right now, I've got four variables that have three states:

blinking, on, or off.

if they are blinking, they are in "edit" mode
if they are on, led strips are on
if they are off, led strips are off

the edit mode times out, and if no changes are made, I'd like the led state to off or on, depending on what it was before it entered setup mode. I probably just need a second variable to keep track what state it was in before it entered edit mode.

but even then, it would be nice not to have to create if then statements for each LED light

Qdeathstar:
hmm. I guess i need to be more specfic. Ill update my original post.

No need, the proper answer has already been posted. You have to check if each of them equal the specified value, using the or operator.

Arrch:

Qdeathstar:
hmm. I guess i need to be more specfic. Ill update my original post.

No need, the proper answer has already been posted. You have to check if each of them equal the specified value, using the or operator.

Would using the OR operator be preferable to using an array variable? I can see how the OR works, but what if you have a relatively large program with many variables - it could get 'tiring' typing out like 10 OR statements. Is there something equivalent to the following psuedo code:

int number1 = 1;
int number2 = 9;
int number3 = 102;
int number4 = 1;
int number5 = 1;
int number6 = 100; // and so on

if ( [ number1, number2, number3, number4, number5, number6 ] == 1){
// run something
}

instead of having to do
if ( number1 == 1 || number2 == 1 || number3 == 1 || number5 == 1 || number6 == 1){
// run something
}

Arrch:

Qdeathstar:
hmm. I guess i need to be more specfic. Ill update my original post.

No need, the proper answer has already been posted. You have to check if each of them equal the specified value, using the or operator.

i wasn't very clear in what i wanted to do, i already that and it will turn all my LEDs off. I didnt clearly stated the problem. The answer was right, but the question was wrong.

I think the array thing might work

Piethon:
Would using the OR operator be preferable to using an array variable?

With two variables, the OR operator is preferable. As the number increases, arrays start to become more preferable.

Piethon:
Is there something equivalent to the following psuedo code:

No

Here's food for thought: Perhaps think of using some kind of state flag?

Somewhere in the code, there must be lines that change the values of those variables, number1 can be set to 476 now, then 1 maybe, and 21 next time, say. Have flags for all of the states you would be looking for later on, and set those flags every time a variable is set to one of those target states. So if you are interested in the values 1 and 2, if number5 is set to 1, set the flag weHaveAOne as true; if number83 is set to 2, set the flag weHaveATwo. Any variable that goes to a value of 1 or 2, would set the appropriate flag.

Then when it's time to make decisions, you just check if weHaveAOne == true; it will be true if one or more variables were 1.

You'll need to think through how when to clear the flags though.....

There is no simple way to check the values of several separately named variables. One workaround would be to put the variables into an array

byte myArray = [12, 0, 7, 11]  // just rubbish values

and then have 4 variables that represent the locations in the array

byte firstVal = 0; // obviously use meaningful names in your code
byte secondVal = 1;
//etc

Then you can access the values in two different ways.

If you want a specific item you can say

myArray[firstVal] = 23;

or if you want to test them all you can do

for (byte n = 0; n < 4; n++) {
    if (myArray[n] == 7) {
       // do whatever
   }
}

...R

I have a few questions:

byte myArray = [12, 0, 7, 11]  // just rubbish values

byte firstVal = 0; // obviously use meaningful names in your code
byte secondVal = 1;
byte thirdVal = 2;
byte fourthVal = 3;

if (myArray[fourthVal] == 11) {
  Serial.println("true");
}

else {
Serial.println("false");
}

That would print true, correct?

Qdeathstar:
That would print true, correct?

It wouldn't even compile.

JimboZA:
Here's food for thought: Perhaps think of using some kind of state flag?

like Florida's or Alabama's? :blush:

Qdeathstar:
That would print true, correct?

yes, with a whole lotta tweaking...

byte myArray[] = {12, 0, 7, 11};  // just rubbish values

byte firstVal = 0; // obviously use meaningful names in your code
byte secondVal = 1;
byte thirdVal = 2;
byte fourthVal = 3;
void setup()
{
  Serial.begin(9600);
  if (myArray[fourthVal] == 11) 
  {
    Serial.println("true");
  }
  else 
  {
    Serial.println("false");
  }
}

void loop()
{

}

Robin2:

byte myArray = [12, 0, 7, 11]  // just rubbish values

...R

you said it. :blush:

BulldogLowell:

JimboZA:
Here's food for thought: Perhaps think of using some kind of state flag?

like Florida's or Alabama's? :blush:

Or the Republic of Scotland's....

JimboZA:

BulldogLowell:

JimboZA:
Here's food for thought: Perhaps think of using some kind of state flag?

like Florida's or Alabama's? :blush:

Or the Republic of Scotland's....

:stuck_out_tongue:

JimboZA:
Or the Republic of Scotland's....

!! XD!!

Yes but Alex fish has said he wants to keep the Queen as the head of state so that rules out a republic dosn't it.

BulldogLowell:

Robin2:

byte myArray = [12, 0, 7, 11]  // just rubbish values

...R

you said it. :blush:

I said it so you wouldn't feel the need to do so - but I see you couldn't resist.

...R

Qdeathstar:
That would print true, correct?

YES

Unlike the others, I had assumed you would be aware that the code would need to be included in a complete program.

This a great example of where it would be much quicker to try it out than to ask a question about it. The Arduino is a wonderful tool for learning by doing.

...R

Robin2:

BulldogLowell:

Robin2:

byte myArray = [12, 0, 7, 11]  // just rubbish values

...R

you said it. :blush:

I said it so you wouldn't feel the need to do so - but I see you couldn't resist.

...R

just noting the obvious that you noted as obvious... it was transcribed verbatim...

karma++ for your good sense of humor!