if-statement condition question

Hi,

Longtime listener, first time poster.

I have a question regarding an if statement conditions. Here is my test Code:

int TestArray[6];
int ArraySize = 6;
int LoopCount = 0;
 
void setup(){

    Serial.begin(9600);
    Serial.println("-------------|||   START   |||-------------");
    
    randomSeed(analogRead(A0));
}
 
void loop() {

    LoopCount++;

    for (byte i = 0; i < ArraySize ; i++) {
        int ran = random(100);
        TestArray[i] = ran;
    }

    for (byte j = 0; j < ArraySize-3 ; j++) {

        if ((TestArray[j] > 50) && (TestArray[j+1] > 50) && (TestArray[j+2] > 50) && (TestArray[j+3] > 50)) {
            
            Serial.print("Loop: ");Serial.println(LoopCount);
            Serial.print(j); Serial.print(" - ");Serial.println(TestArray[j]);
            Serial.print(j+1); Serial.print(" - ");Serial.println(TestArray[j+1]);
            Serial.print(j+2); Serial.print(" - ");Serial.println(TestArray[j+2]);
            Serial.print(j+3); Serial.print(" - ");Serial.println(TestArray[j+3]); Serial.println();
        }
    } 
}

Now, there has to be a more sophisticated way of writing the if statement in the for loop?
Something along the lines of:

if ( check all/certain amount of array members for the same condition) {}

or

if ( (for (j=0; j < ArraySize; j++)) TestArray[j] > 50) {}

This would come in particularly handy if the Array is big, or if the array size varies.
I hope this makes sense. Thank for your time and help.

Simon

Why not

boolean actionNeeded = false;
for (byte i = 0; i < ArraySize; i++)
{
  if (TestArray[i] > 50)
  {
    actionNeeded = true;
  }
}

if (actionNeeded)
{
  //take action
}

By the way, I assume that you know that there are errors in your code

UKHeliBob,

Thanks so much for your quick reply.

I think this still doesn't solve my initial question. Your code flips the "Action needed" Switch if ANY of the Array members fulfills the if statement.
I'm looking for a simpler way to write if ALL of them do.

Also, could you please point out the errors in my code. It compiles fine for me, but I'm always appreciating an opportunity to learn. Thanks again!

int TestArray[6];

An array of 6 elements, all set to 0

    for (byte i=0; i < ArraySize; i++) {
        pinMode(TestArray[i], OUTPUT);
    }

Code setting pin 0 to OUTPUT 6 times, which seems unlikely

Is that what you meant to do ?

Oh I guess setting the pins to OUTPUTS is kind of meaningless in this code... It's a leftover from the more complicated one, where the Array contains Arduino Pins. I want to set them all to OUTPUTs

I think the for loop does accomplish this, no? At i=0 the first member of the Array is set to OUTPUT, at i=1, the 2nd member is set to OUTPUT and so on...

Thanks again for your time. Really appreciate it.

at i=1, the 2nd member is set to OUTPUT and so on...

The code does that, but at the moment all elements of the array are initialised to zero by the global declaration of the array

Yep, your right. Thanks for catching that. I will update the initial post.

I will update the initial post.

Please don't do that, at least not without adding a note that it has been changed and why otherwise it makes a nonsense of subsequent comments

seemoo:
I think this still doesn't solve my initial question. Your code flips the "Action needed" Switch if ANY of the Array members fulfills the if statement.
I'm looking for a simpler way to write if ALL of them do.

Just turn the logic on its head - like this

boolean actionNeeded = true;
for (byte i = 0; i < ArraySize; i++)
{
  if (TestArray[i] > 50)
  {
    actionNeeded = false;
  }
}

...R

Thank you Robin2 for your reply.

I think the problem still persists.

boolean actionNeeded = true;
for (byte i = 0; i < ArraySize; i++)
{
  if (TestArray[i] > 50)
  {
    actionNeeded = false;
  }
}

This turns actionNeeded to false if ANY of the elements is bigger than 50. It could be one of them or all of them. Correct?
I'm looking for a statement that turns actionNeeded to false when ALL of the Array members fulfill the if condition.
Thanks again

I think I might have found a solution. Will have to play around with it more a bit, But wanted to see what you think

boolean actionNeeded = true;
int isTrue = 0;

for (byte i = 0; i < ArraySize; i++)
{
  if (TestArray[i] > 50)
  {
   isTrue++;
  }
}

if (isTrue == ArraySize)
{
  actionNeeded = false;
  isTrue = 0;               // Reset isTrue variable
}

Hopefully this tests ALL the array members and flips the switch if ALL of them fulfill the condition. Thoughts?

byte isTrue = 0;
for (byte i = 0; i < ArraySize; i++)
{
  isTrue += (TestArray[i] > 50);
}

seemoo:
I'm looking for a statement that turns actionNeeded to false when ALL of the Array members fulfill the if condition.

I think the test in my code in Reply #8 should have been

if (TestArray[i] <= 50)

that way it shows false if any one of them fails the test.

...R

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