Why does this code fail in this particular way?

So I have figured out my issue but I don't understand why the code failed in this particular way. So the error is that I was indexing an array in a loop without shifting it (i=4 I should be shifting minus 4 to be at variable[0]). Anyways, here is the code:

void loop()
{
  tCAN message;
  if (mcp2515_check_message())
  {
    if (mcp2515_get_message(&message))
    {
      if (message.id == 0x201) //uncomment when you want to filter
      {
        for (int i = 0; i < message.header.length; i++)
        {
          if (i < 2)               // only process first two bytes
          {
            rpm[i] = message.data[i];  //Store each byte
            Serial.print("Rpm byte");
            Serial.println(i);
            Serial.println(rpm[i]);
          }
          else if (i == 2 or i == 3)
          {
            speedkmh[i] = message.data[i];
            Serial.print("speed byte");
            Serial.println(i);
            Serial.println(speedkmh[i]);
          }
          else if (i == 4 or i == 5)
          {
            pos[i] = message.data[i];
            Serial.print("POS byte");
            Serial.println(i);
            Serial.println(pos[i]);
          }
        }
        newrpm = ((rpm[0] << 8) + rpm[1]) / 4;        //Take first byte and multiple it by 256 to shift bits by 8
        Serial.println(newrpm);
        speedmph = (((speedkmh[0] << 8) + speedkmh[1]) / 100) * .62;
        Serial.println(speedmph);
        throttle = ((pos[0] << 8) + pos[1]) / 640;
        Serial.println(throttle);
        rpmleds = map(newrpm, 0, 7000, 0, NUMPIXELS);
        for (int x = 0; x < NUMPIXELS; x++)
        {
          if (x < rpmleds)
          {
            strip.setPixelColor(x, strip.Color(0, 255, 50)); // Moderately bright green color.
          }
          else
          {
            strip.setPixelColor(x, strip.Color(0, 0, 0));
          }
        }
        strip.show();
      }

    }
  }
}

and here is the output:

Rpm byte0
48
Rpm byte1
11
speed byte2
0
speed byte3
0
POS byte4
18
POS byte5
0
1152
0
0

The neopixels I have attached later on were responding to throttle position instead of rpm which is what is confusing me. The code was originally to just pull RPMs from the CAN bus and display a neopixel graph. I had it running perfectly and started adding the code for the other stats (speed and throttle position). I did not change the neopixel code at all. Immediately after adding the speed and throttle code the neopixels started reacting to the throttle position and not rpm and the variables for speed and throttle were coming up as 0 (which they should because of the invalid array position). Anyways, in my head, the speed and throttle (pos) code should not have affected the rpm code (although obviously those variables would be wrong/unpredictable). Any idea why this behavior happened? After fixing the array positions it all worked as expected.

Btw, the output is in decimal but they are 2 separate bytes in decimal that I had to shift the bits to combine them, it may look like an rpm but it's definitely responding to Throttle position because it works with the car off :slight_smile:

Here is the output with the car off:

Rpm byte0
0
Rpm byte1
3
speed byte2
0
speed byte3
0
POS byte4
135
POS byte5
128
8672
0
0

So this could overwrite an entirely different variable?

laser411:
So this could overwrite an entirely different variable?

Of course.