Program not indexing an array correctly

hi there

I am coding a recent project of mine and have run into a problem where my code won't index an array correctly.

The code has been designed to sort values into an array to be used later.
I have checked the incoming values and they are what I am expecting and the communication code seems to be working fine but when I print the array index value to see what's going on I get crazy values as seen in the photo linked below. I want it to count up sequenchly till the array is full and then sent back to 0.

I have also linked the code below. (this includes the whole code but I have blocked out a lot of it as I was trying to narrow the possible faults points coursing this problem)

// slave node

#include <SoftwareSerial.h>

#define RS_RO    10
#define RS_DI    11
#define RS_DE_RE 12

SoftwareSerial RS_Slave(RS_RO , RS_DI); // TX , RX

// sorting and reseving incuming darta
                   //index 0 1 2
int controle_camandes [3] {3,3,3};
byte cc_index = 0;
int in_data;


// sorting and sending out going darta
                  //index 0  1 2 3  4
int senser_readings [5] {001,2,2,2,002};
byte sr_index = 0;


void setup() {
  Serial.begin(9600);
  RS_Slave.begin(9600);
  pinMode(RS_DE_RE, OUTPUT);
  // enabeal pin low to actvate reseve fucion
  digitalWrite(RS_DE_RE,LOW);
  Serial.println("slave node is ready");
}

void loop() {
  /* // sending darta
    digitalWrite(RS_DE_RE,HIGH);
     for (sr_index = 0; sr_index < 5; sr_index ++)
     {
      RS_Slave.write(senser_readings[sr_index]);
     }
    digitalWrite(RS_DE_RE,LOW);
  */  
  //reseving and sorting darta
  //001 = new transmicion started
  //002 = end of transmicion
    while (RS_Slave.available() && in_data != 2 )
    {
      in_data = RS_Slave.read();
      //Serial.println (in_data);
     
      if (in_data == 1)
      {
        //cc_index = 0;
       // Serial.println ("reseving data");
      }
      if (in_data == 2)
      {
       // Serial.println ("all data reseved");
      }
      if (in_data != 1 || 2)
      {
        controle_camandes [cc_index] = in_data;
        cc_index ++;
      }
    }
    Serial.println (cc_index);
    /*
     if (cc_index > 5)
    {
      Serial.println ("dater format not exspected");
      for (cc_index = 0; cc_index < 3; cc_index ++)
      {
       controle_camandes [cc_index] = 0; 
      } 
    }
   */ in_data = 0;
    
  
    //end of comuncacions code
  /*
    for (cc_index = 0; cc_index < 3; cc_index ++)
    {
      Serial.println ( controle_camandes [cc_index] );
    }
  */
  //Serial.println ( controle_camandes [0] );
    //other code
    delay(1000);
  }

What is your expected output. It looks to me that you are displaying the index. First "2" (0, 1, 2) then "+5" (=7) then "+5" (=12)...

This doesn't do what you think it does... maybe you meant the following?

if (in_data != 1 && in_data != 2)

Or just use an if/else-if/else for the 3 conditions.

Yes it does appear to be displaying that but thats not what i was intending. It should be 1,2,3,4,5,6 and so on.

And to mack it even more confusing the error changes from counting in 5s to 10s to 8s every time i reflash the board

Ok i will try changing that line of code and see if it fixes whatever bugs are going on.

You are printing the index. Did you intend: Serial.println(controle_camandes[cc_index]);?

Yes that is in one if the skipped over codes. That was the first thing i looked at and it wasn't changing the second and third values in the array withch is way i looked futher.

Remove code that is not working, or at least, paste it out of the way. Give yourself a clean sketch that works, and add parts, testing at each change. I must go. See you soon. Good luck.

Ok thanks. I will have a go trying all that and see how it goes. Thanks for your help

does this mean you receive random values but want to insert them into an array in sorted order?

what's wrong with the values? what makes them "crazy"? they look like a sort set of non-sequential values

They are ment to be sequenchel values as this is the index number of the array that is getting printed.

Ive taken a bit of a break from it recently but i will do some more testing tomorrow and see what i can diagnose and get back to you.

Thanks for all the help

When cc_index is more than 2 than you are overwriting memory. Once that happens all bets are off. You can get all sorts of wierdness.

To emphasize what @red_car said I would change the following:

      if (in_data == 1)
      {
        //cc_index = 0;
       // Serial.println ("reseving data");
      }
      if (in_data == 2)
      {
       // Serial.println ("all data reseved");
      }
      if (in_data != 1 || 2)
      {
        controle_camandes [cc_index] = in_data;
        cc_index ++;
      }

to this:

      if (in_data == 1)
      {
        //cc_index = 0;
       // Serial.println ("reseving data");
      }
      else if (in_data == 2)
      {
       // Serial.println ("all data reseved");
      }
      else
      {
        controle_camandes [cc_index] = in_data;
        cc_index ++;
      }