Trouble understanding code

Hey guys, brand new to Arduino, just started to look at programming and I find the easiest way for me to learn some of the new concepts is by dissecting and reverse engineering existing code. I came across this one, a program to convert resistor values, and while there are parts like the loops that make sense there are others I’m just not understanding. Was wondering if anyone could help me understand what’s going?

 /*

*/

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

void loop()
{
  int id[10];
  int index = 0;

  while (Serial.available())
  {
    id[index] = Serial.read() - 48;
    if (id[index] < 0) {
      id[index] = 0;
    }
    else
    {
      index = index + 1;
    }
  }
  if (index > 0)
  {
    for (int i = 0; i < index ; i++)
      Serial.print(id[i]);
    Serial.print(" Ohm resistor is\t");

    switch (id[0]) {
      case 0:
        Serial.print("Black");
        break;
      case 1:
        Serial.print("Brown");
        break;
      case 2:
        Serial.print("Red");
        break;
      case 3:
        Serial.print("Orange");
        break;
      case 4:
        Serial.print("Yellow");
        break;
      case 5:
        Serial.print("Green");
        break;
      case 6:
        Serial.print("Blue");
        break;
      case 7:
        Serial.print("Violet");
        break;
      case 8:
        Serial.print("Grey");
        break;
      case 9:
        Serial.print("White");
        break;
    }

    Serial.print("\t");

    switch (id[1]) {
      case 0:
        Serial.print("Black");
        break;
      case 1:
        Serial.print("Brown");
        break;
      case 2:
        Serial.print("Red");
        break;
      case 3:
        Serial.print("Orange");
        break;
      case 4:
        Serial.print("Yellow");
        break;
      case 5:
        Serial.print("Green");
        break;
      case 6:
        Serial.print("Blue");
        break;
      case 7:
        Serial.print("Violet");
        break;
      case 8:
        Serial.print("Grey");
        break;
      case 9:
        Serial.print("White");
        break;
    }

    Serial.print("\t");

    switch (index - 2) {
      case 0:
        Serial.print("Black");
        break;
      case 1:
        Serial.print("Brown");
        break;
      case 2:
        Serial.print("Red");
        break;
      case 3:
        Serial.print("Orange");
        break;
      case 4:
        Serial.print("Yellow");
        break;
      case 5:
        Serial.print("Green");
        break;
      case 6:
        Serial.print("Blue");
        break;
      case 7:
        Serial.print("Violet");
        break;
      case 8:
        Serial.print("Grey");
        break;
      case 9:
        Serial.print("White");
        break;
    }
    Serial.println("");
    index = 0;
    delay(1000);
  }
  delay(1000);
}

I doubt that anyone will try to explain that code line by line. If you have specific questions it would be better than the general “explain this code”.

I'm also pretty sure it's got a timing-dependent bug in it - if a value is in the process of being sent while that 1000 ms delay ticks over, it'll interpret the part that it's gotten. Needs a delimiter to signify the end of the value to be converted.

Apologies I should have been more specific. I'm trying to understand all the index stuff. Why is there a [10] after the int index? The code works exactly as intended, I'm just trying to figure out how. I guess the best way to explain it would be, with this code you enter a resistor value, it then returns the band color. How is it able to denote color values to specific parts of the entered resistor value. You can enter 100 or 1200 or 90000 and it's able to sort through and know that the first digit is a color, second digit a different color and the rest of the value is a multiplier. I'm just trying to figure out how it knows. Again my apologies for such a general question, I'm sure you all have better things to do then walk a new guy through code. So I appreciate all the input.

I my humble opinion , you are better off playing with the IDE examples and going from there. These are tested and work , and are a great tool for learning- as are some of the books you can buy or on line tutorials .
Down loading someone else’s code is asking for trouble :

  1. It probably doesn’t work
  2. It may not be very well written.
  3. Downloading leads , IMO to bodging and nailing together bits of code from here and there- not the way to go!

total index of 10 because that's the longest value they expect. (you can break the code by sending a 11 digit number - there's not even a test against this, so it will produce undefined behavior.

Then they use the first 2 digits to figure the first two colors in the first two switch-case statements, and then for the last one, they count the remaining digits and subtract 2 (which is unnecessary, as they could also have just added 2 to all the values they're comparing it to), then put it through another switch-case.

My overall impression is that the author of that code was a beginner himself. This is often a problem with tutorials and guides on the internet.

Thanks for the replies. It is greatly appreciated. It all makes sense now. Also a big thanks for the thoughtful suggestions. I’ll be going through the example book tonight and perhaps picking up programming Arduino for dummies like book. Now that I know what’s what and what I’m looking at it all makes a lot more sense and I have a better understanding of what’s happening. So thanks again. It’s greatly appreciated.

Good luck , hope you enjoy it