Array Application

So I am creating a password system using Neopixels and an IR remote. Lighting up each pixels once a button is pressed. I created 4 array variables with list of numbers inside to be used later. I also initialised z variable to be use later on the last part to check if the passwords are correct. Correct = all pixel blink green 3 times. Wrong all light up as red for 2 seconds. So my question is, for every if statements on the first part, do the digits actually get stores on the y[z] array? And, am I right to use if( z =8) to check if the number of digits inside y[z] is already 8 to start checking the password? Because, it does not seem to work, it does not enter in the if statements when checking if the password is correct or not. Also, the serial monitor is only showing 0s instead of the number of the button I am pressing :// is my application of array wrong? Thank you!

//INFRARED LIBRARY
#include <IRremote.h>
//ADAFRUIT PIXEL LIBRARY
#include <Adafruit_NeoPixel.h>

#define numpixels 8

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numpixels, 12);

const int receiver = 11;


int bryce[8] = {1,1,8,1,1,2,7,7,}; //passwords
int franchell[8] = {1,1,8,1,2,3,6,2}; 
int justyn[8] = {1,1,8,1,0,6,5,3};
int extra[8] = {1,1,9,8,7,6,5,4};

int x;
int y[8];//will be used to assign numbers
int z; //to activate correct or not

//receiver oject
IRrecv irrecv(receiver);
//decoder object
decode_results results;

void setup()
{ 
  Serial.begin(9600);
  irrecv.enableIRIn();//receiving process
  irrecv.blink13(true);
  pixels.begin();
}

void loop()
{  
  if(irrecv.decode(&results)) //this checks to see if a code has been received
  {
    if(results.value == 0xFD30CF) //if the button press equals the hex value 0xC284
    {
      //maps 0 button
      pixels.setPixelColor(x,pixels.Color(20,30,40));//GRAYISH black since black is not visible
      pixels.show();
      y[z]=0;
    }
    else if(results.value == 0xFD08F7) //if the button press equals the hex value 0xC284
    {
      //maps 1 button
      pixels.setPixelColor(x,pixels.Color(0,0,255));//blue
      pixels.show();
      y[z]=1;
    }
    else if(results.value == 0xFD8877) //if the button press equals the hex value 0xC284
    {
      //maps 2 button
      pixels.setPixelColor(x,pixels.Color(255,165,0));//orange
      pixels.show();
      y[z]=2;
    }
    else if(results.value == 0xFD48B7) //if the button press equals the hex value 0xC284
    {
      //maps 3 button
      pixels.setPixelColor(x,pixels.Color(255,0,0));//red
      pixels.show();
      y[z]=3;
    }
    else if(results.value == 0xFD28D7) //if the button press equals the hex value 0xC284
    {
      //maps 4 button
      pixels.setPixelColor(x,pixels.Color(0,255,0));//green
      pixels.show();
      y[z]=4;
    }
    else if(results.value == 0xFDA857) //if the button press equals the hex value 0xC284
    {
      //maps 5 button
      pixels.setPixelColor(x,pixels.Color(0,255,255));//cyan
      pixels.show();
      y[z]=5;
    }
    else if(results.value == 0xFD6897) //if the button press equals the hex value 0xC284
    {
      //maps 6 button
      pixels.setPixelColor(x,pixels.Color(128,0,128));//purple
      pixels.show();
      y[z]=6;
    }
    else if(results.value == 0xFD18E7) //if the button press equals the hex value 0xC284
    {
      //maps 7 button
      pixels.setPixelColor(x,pixels.Color(255,255,0));//yellow
      pixels.show();
      y[z]=7;
    }
    else if(results.value == 0xFD9867) //if the button press equals the hex value 0xC284
    {
      //maps 8 button
      pixels.setPixelColor(x,pixels.Color(255,0,255));//magenta
      pixels.show();
      y[z]=8;
    }
    else if(results.value == 0xFD58A7) //if the button press equals the hex value 0xC284
    {
      //maps 9 button
      pixels.setPixelColor(x,pixels.Color(255,255,255));//white
      pixels.show();
      y[z]=9;
    }
    x++;
    delay(1000);
    irrecv.resume(); //receive the next value
    Serial.print(y[8]);
  }
  if (z==8){
  if((y[8]==bryce[8]) or (y[8]==franchell[8]) or (y[8]==justyn[8]) or (y[8]==extra[8]))
     {
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,255,0));
         pixels.show();
       } delay(500); x=0;
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,0,0));
         pixels.show();
       } 
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,255,0));
         pixels.show();
       } delay(500);
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,0,0));
         pixels.show();
       } 
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,255,0));
         pixels.show();
       } delay(500);
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,0,0));
         pixels.show();
       }
     }
  else
  {
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(255,0,0));
         pixels.show();
       } delay(2000); x=0;
       for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,0,0));
         pixels.show();
       }
  }
  }
}

//https://learn.sparkfun.com/tutorials/ir-communication/receiving-ir-example

You never increment z :wink:

And a tip, use way more discriptive variable names. I would even say that is you named 'x' something more descriptive as 'passwordPosition' you would not have made this error :wink:

Also

y[8]==bryce[8]

does not work. You can't directly compare two array's against each other. This is just notation to compare element 8 of 'y' with element 8 of 'bryce'. Moreover, both 'y' and 'bryce' are just 8 in size and this the highest available element is 'y[7]' and 'bryce[7]'. So the 8th element does not exist! You have to compare each element individually.

And another tip

//instead of
int bryce[8] = {1,1,8,1,1,2,7,7,}; //passwords
int franchell[8] = {1,1,8,1,2,3,6,2};
int justyn[8] = {1,1,8,1,0,6,5,3};
int extra[8] = {1,1,9,8,7,6,5,4};

//make it a two dimentional array
const bytes Passwords[][8] = {{1,1,8,1,1,2,7,7},
                              {1,1,8,1,2,3,6,2},
                              {1,1,8,1,0,6,5,3},
                              {1,1,9,8,7,6,5,4}};

Makes it easier to loop over every "user" without the need to add (duplicated) code for that user. Also makes it super easy to extend it to 5, 6 etc users.

Ahhhh I see! Thank you for that insight! I thought by doing this y[8]==bryce[8], it would compare the data inside the array. So is it impossible to compare the data inside an array to an array?

erenkirstein:
So is it impossible to compare the data inside an array to an array?[/color]

not in one go. You have to compare each value against the matching one. because data in an array is guaranteed to be in contiguous memory addresses, you can use the memcmp() standard function to avoid writing your own for/while compare loop. just make sure you pass the size of the array in bytes (not the number of elements) as your elements use up more than 1 byte each.

I see! Thank you! I will study the memcmp() function!

is it impossible to compare the data inside an array to an array?

If you want to know that the arrays are different, what the different values are and their position(s) in the array then you will need to iterate through them. However, if you just want to know that they are different in some way then a single call to memcmp() will tell you

If you are receiving values one by one then you do not really need to put them in an array as they are received because you can compare each of them with the corresponding array entry as they are received. You don't have to act on a failure to match until all of the inputs have been received so as not to give clues as to which entry was wrong

so something like

if (!memcmp(y, bryce, sizeof(bryce)) {
  // match found
}

Sorry but last question! Whenever I do this, does data actually get stored on y[z]? So will it come out as y[z] = {1,3,2,1,3,4,5,4} whenever it iterates 8 times after pressing buttons on ir remote? If not, how do I actually store the data incoming as a list like this y[z] = {1,3,2,1,3,4,5,4} so that I can compare it with the correct passwords? Thank you!

 if(results.value == 0xFD30CF) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 0 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(20,30,40));//GRAYISH black since black is not visible[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=0;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD08F7) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 1 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(0,0,255));//blue[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=1;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD8877) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 2 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(255,165,0));//orange[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=2;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD48B7) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 3 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(255,0,0));//red[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=3;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD28D7) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 4 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(0,255,0));//green[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=4;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFDA857) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 5 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(0,255,255));//cyan[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=5;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD6897) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 6 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(128,0,128));//purple[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=6;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD18E7) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 7 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(255,255,0));//yellow[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=7;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD9867) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 8 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(255,0,255));//magenta[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=8;[color=#222222][/color]
    }[color=#222222][/color]
    else if(results.value == 0xFD58A7) //if the button press equals the hex value 0xC284[color=#222222][/color]
    {[color=#222222][/color]
      //maps 9 button[color=#222222][/color]
      pixels.setPixelColor(x,pixels.Color(255,255,255));//white[color=#222222][/color]
      pixels.show();[color=#222222][/color]
      y[z]=9;[color=#222222][/color]
    }[color=#222222][/color]
    x++;

to your question, if you increment z so that at every press you store the information in a new index, then yes, after 8 key presses you'll have an array filled with data (I'm not sure what's the x++ is doing at the end)

:o :o the comment feels weirdif(results.value == 0xFD30CF) //if the button press equals the hex value 0xC284

I see, thank you once again! Oh x++ is just for the pixels to light up one by one each button press. Anyway, Thank you!

@erenkirstein You missed the first part of reply #1 :wink: Aka, you don't need z and x and with a proper name you probably would have noticed.

ahhh I see, you are right! I should also change my variable huh so that it is more understandable haha! Thank you! I Changed my code, totally removed the z and changed the x, and tried to see if a list of array is really stored on y[ ] array. But, It does not seem to save the previous data, it just

//INFRARED LIBRARY
#include <IRremote.h>
//ADAFRUIT PIXEL LIBRARY
#include <Adafruit_NeoPixel.h>

#define numpixels 8

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numpixels, 12);

const int receiver = 11;


int bryce[] = {1,1,8,1,1,2,7,7,};
int franchell[] = {1,1,8,1,2,3,6,2}; 
int justyn[] = {11810653};
int extra[] = {1,1,9,8,7,6,5,4};

int a;
int position;
int y[8];//will be used to assign numbers

//receiver oject
IRrecv irrecv(receiver);
//decoder object
decode_results results;

void setup()
{ 
  Serial.begin(9600);
  irrecv.enableIRIn();//receiving process
  irrecv.blink13(true);
  pixels.begin();
}

void loop()
{
  //code to know which button corresponds to its HEXAdecimal code
  /*if(irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }*/
  
  if(irrecv.decode(&results)) //this checks to see if a code has been received
  {
    if(results.value == 0xFD30CF) //if the button press equals the hex value 0xC284
    {
      //maps 0 button
      pixels.setPixelColor(position,pixels.Color(20,30,40));//GRAYISH black since black is not visible
      pixels.show();
      y[8]=0;
    }
    else if(results.value == 0xFD08F7) //if the button press equals the hex value 0xC284
    {
      //maps 1 button
      pixels.setPixelColor(position,pixels.Color(0,0,255));//blue
      pixels.show();
      y[8]=1;
    }
    else if(results.value == 0xFD8877) //if the button press equals the hex value 0xC284
    {
      //maps 2 button
      pixels.setPixelColor(position,pixels.Color(255,165,0));//orange
      pixels.show();
      y[8]=2;
    }
    else if(results.value == 0xFD48B7) //if the button press equals the hex value 0xC284
    {
      //maps 3 button
      pixels.setPixelColor(position,pixels.Color(255,0,0));//red
      pixels.show();
      y[8]=3;
    }
    else if(results.value == 0xFD28D7) //if the button press equals the hex value 0xC284
    {
      //maps 4 button
      pixels.setPixelColor(position,pixels.Color(0,255,0));//green
      pixels.show();
      y[8]=4;
    }
    else if(results.value == 0xFDA857) //if the button press equals the hex value 0xC284
    {
      //maps 5 button
      pixels.setPixelColor(position,pixels.Color(0,255,255));//cyan
      pixels.show();
      y[8]=5;
    }
    else if(results.value == 0xFD6897) //if the button press equals the hex value 0xC284
    {
      //maps 6 button
      pixels.setPixelColor(position,pixels.Color(128,0,128));//purple
      pixels.show();
      y[8]=6;
    }
    else if(results.value == 0xFD18E7) //if the button press equals the hex value 0xC284
    {
      //maps 7 button
      pixels.setPixelColor(position,pixels.Color(255,255,0));//yellow
      pixels.show();
      y[8]=7;
    }
    else if(results.value == 0xFD9867) //if the button press equals the hex value 0xC284
    {
      //maps 8 button
      pixels.setPixelColor(position,pixels.Color(255,0,255));//magenta
      pixels.show();
      y[8]=8;
    }
    else if(results.value == 0xFD58A7) //if the button press equals the hex value 0xC284
    {
      //maps 9 button
      pixels.setPixelColor(position,pixels.Color(255,255,255));//white
      pixels.show();
      y[8]=9;
    }
    position++;
    a++;
    delay(1000);
    irrecv.resume(); //receive the next value
    Serial.print(y[8]);
  }
  if(a==8){
  if (memcmp(justyn,y,sizeof(justyn))==0)
  {
    Serial.print("equal");
      for(int i =0; i<=8;i++)
       {
         pixels.setPixelColor(i,pixels.Color(0,255,0));
         pixels.show();
       } delay(500); position=0;
      }
  else
  {Serial.print(y[8]);}}

prints out the latest data that I have stored. Am i doing something wrong here? Sorry for the barrage of questions :confused: so my serial monitor only prints out as 3, instead of 14234343

something feels wrong for Justyn?

int bryce[] = {1,1,8,1,1,2,7,7,};
int franchell[] = {1,1,8,1,2,3,6,2}; 
int justyn[] = {11810653};
int extra[] = {1,1,9,8,7,6,5,4};

Oh sorry, I tested both with or without comma, and pressed the button that correspond to the password for justyn and compared it using memcmp, but it still says it does not match the array inside y[8 ]. So, I tried inputting serial.print on the else statement to check why the codes did not match. And it seems to print the latest data inputted on y[8 ]. Which therefore I concluded that it did not store a list in the array, only the latest data :confused:

Edit:
I did create y[8] so that the data will be stored there..

y[8] does not exist... you need an index that is incremented at every press

position does not change

what's this doing in the code

    x++;
    a++;

why is there a delay ?

Oh wait, on the first part if statements (mapping for remote buttons): when I put the y[8]=0. So it does not input the data inside the index??? Oh I changed x to position. Position is inside the pixel code pixels.setPixelColor(position,pixels.Color(255,255,255));
position increments only after I pressed a button so that one pixel only lights up from left to right one by one per button pressed.
variable a on the other hand is used for the ending if statement which means once a reaches the 8th increment, an 8 length long code is already (supposedly) inside the y[8] array. Which means, it's comparing time to check if it matches the passwords.

Also, there is a delay so that rapidly pushing a button will only record one input per second.

here is a better structure for the core part of your code (typed here based on your code, so mind typos - not sure it even compiles). You should get the idea

#include <IRremote.h>
const byte receiverPin = 11;
IRrecv irrecv(receiverPin);
decode_results results;

const byte secretCodeSize = 8;
const byte users[][secretCodeSize] = {
  {1, 1, 8, 1, 1, 2, 7, 7}, // bryce
  {1, 1, 8, 1, 2, 3, 6, 2}, // franchell
  {1, 1, 8, 1, 0, 6, 5, 3}, // justyn
  {1, 1, 9, 8, 7, 6, 5, 4}  // extra
};
byte numberOfKnownUsers = sizeof users / sizeof users[0];

byte position;
byte codeReceived[secretCodeSize];  //will be used to assign numbers

void setup()
{
  irrecv.enableIRIn();//receiving process
  irrecv.blink13(true);
  position = 0;
}

void loop()
{
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0xFD30CF: codeReceived[position++] = 0; break;     // key 0
      case 0xFD08F7: codeReceived[position++] = 1; break;     // key 1
      case 0xFD8877: codeReceived[position++] = 2; break;     // key 2
      case 0xFD48B7: codeReceived[position++] = 3; break;     // key 3
      case 0xFD28D7: codeReceived[position++] = 4; break;     // key 4
      case 0xFDA857: codeReceived[position++] = 5; break;     // key 5
      case 0xFD6897: codeReceived[position++] = 6; break;     // key 6
      case 0xFD18E7: codeReceived[position++] = 7; break;     // key 7
      case 0xFD9867: codeReceived[position++] = 8; break;     // key 8
      case 0xFD58A7: codeReceived[position++] = 9; break;     // key 9
    }
    irrecv.resume(); //receive the next value

    if (position >= secretCodeSize) { // we got our full input
      bool foundUser = false;
      byte userId;
      for (userId = 0; userId < numberOfKnownUsers; userId++) {
        foundUser = !memcmp(codeReceived, users[userId], sizeof codeReceived);
        if (foundUser) break; // no need to continue, we got our user
      }

      if (foundUser) {
        // do something, the user we found is at index userId
      } else {
        // do something else, wrong code
      }
      position = 0; // get ready for next time
    }
  }
}

That doeeees look way cleaner than the if else! Tried combining it with neopixel color activation for each case as well though I'm not sure it works XD. Anyway thanks for the help guys!

post your resulting code :slight_smile:

ohhhh I'll try to do it soon, been burn out lately from the bombardment of projects so I dropped this project first and do the other projects first. Thanks though, you really helped me a lot in understanding the basics in arduino XD. I'm not used to c++ syntax XD