Need help with arrays

Hello,

I try to make array from 3 analog sensors data converting it to binary array “mas” and compare it with my “case” arrays.

I do something wrong with arrays because I cant make array “mas” and compare it…

During compilation I get a lot of mistakes about “]” simbols…

I new in C so please help me to undestand array() reference and assign array values.

Can anyone correct my code to get it work or tell where I do mistakes?

Thank you.

P.S. sorry for my English:-)

compare_array_with_casies.ino (888 Bytes)

I’ll just post your sketch here, so you can get better help:

float ir1 = 0;
float ir2 = 0;
float buttom = 0;
int mas[2];                 //mas={..., ..., ...}
int case1[] = {0, 0, 0};
int case2[] = {1, 0, 0};
int case3[] = {1, 1, 0};
int case4[] = {1, 1, 1};
int case5[] = {0, 1, 1};
int case6[] = {0, 0, 1};
int case7[] = {0, 1, 0};

void setup()
  {
    Serial.begin(9600);
  }

void loop()
  {
    buttom = analogRead(0);
    ir1 = analogRead(1);
    ir2 = analogRead(2);

    if(analogRead(1) > 0){mas[0] = 0;} 
    else {mas[0] = 1;}

    if(analogRead(2) > 0){mas[1] = 0;} 
    else {mas[1] = 1;}

    if(analogRead(0) > 0){mas[2] = 0;} 
    else {mas[2] = 1;}

    if (mas[] == case2[] || mas[] == case3[] || mas[] == case4[] || mas[] == case5[] || mas[] == case6[] || mas[] == case7[])
      {
        while (mas[] |= case1[])
        {
        Serial.println(mas[]);
        Serial.println("OK");
        delay(500);
        }
      }
  }

To mee it seems like you are wasting a lot of space and compute power on something quite simple. If the only values you are wanting to store are 0 and 1 then it is much simpler to use bit manipulation:

//Constants used to compare bit-flags
const byte case1 = 0; //000
const byte case2 = 4; //100
const byte case3 = 6; //110
const byte case4 = 7; //111
const byte case5 = 3; //011
const byte case6 = 1; //001

//Your "mas"
byte mas = 0;

void testPin(int pin, byte bit) {

  //Convenient function to test a pin and set or clear a bit accordingly
  if (analogRead(pin) > 0) mas |= (1 << bit); //Set bit
  else mas &= ~(1 << bit); //Clear bit

}

void loop() {

  testPin(1, 0);
  testPin(2, 1);
  testPin(0, 2);

  //From here on you can compare like so:
  if (mas == case1) {
    //Do something
  } else {
    //Do something else
  }
}
if (mas[] == case2[]

You can't compare a whole array at once like that. There is a memcmp function you can look up that can do it. But I think this code could be simplified a lot otherwise and may not need that in the end.

When you refer to arrays in your code like here:

Serial.println(mas[]);

You must always tell the compiler which member of the array you want to refer to. You must always have something inside the . The only place where an empty is OK is when you first create the array IF AND ONLY IF you also supply a list of initial values in { and } so the compiler can count and figure out the number.

BTW: this comment is wrong.

int mas[2];                 //mas={..., ..., ...}

If mas only has 2 members then it would only be {... , ... } and they would be numbered 0 and 1. I think you wanted a 3 in there so you would have mas[0], mas[1], and mas[2].

Delta_G:
When you refer to arrays in your code like here:

Serial.println(mas[]);

You must always tell the compiler which member of the array you want to refer to. You must always have something inside the ..

How about char arrays?

BulldogLowell:
How about char arrays?

Go ahead and try it:

char array[] = "This is my array.";

Serial.println(array[]);

It will error I'm sure. Don't have Arduino handy to try. But I'm betting it errors. The syntax there is to pass a pointer to the first character. And that's why you have to have the null to mark the end, because the print function doesn't know how long the string is that's being passed.

So normally you would write:

char array[] = "This is my array.";

Serial.println(array);