0

New to Arduino and programming so forgive me if this is trivial but I've searched and can't find an answer...

Goal: I want to convert decimal values to their equivalent fraction... eg: 0.125=1/8. I think I came up with a simple solution, having one array of decimal values and one array of fraction values and when the input value matches a decimal value return the corresponding fraction value and display it on a tft screen...

Problem: well my simple solution doesn't work for some reason and as stated previously I searched and can't figure out why... I tried declaring the arrays in many ways but with the same result, here is the relevant code but it's only a small fraction of the complete code... the line that is commented out is the one giving me the problem...

//Serial.print("decimal test result: ");Serial.print(fracarray[i]);Serial.println(" match");

when I use that line a random smiley face character appears on the tft screen and keeps the code from running properly.

int checkformatting(char *input, int button){
  char *decarray[]={"0.015625", "0.03125", "0.046875", "0.0625", "0.078125", "0.09375", "0.109375", "0.125", "0.140625", "0.15625", "0.171875", "0.1875", "0.203125", "0.21875",
                  "0.234375", "0.250", "0.265625", "0.28125", "0.296875", "0.3125", "0.328125", "0.34375", "0.359375", "0.375", "0.390625", "0.40625", "0.421875", "0.4375", "0.453125",
                  "0.46875", "0.484375", "0.5", "0.515625", "0.53125", "0.546875", "0.5625", "0.578125", "0.59375", "0.609375", "0.625", "0.640625", "0.65625", "0.671875", "0.6875",
                  "0.703125", "0.71875", "0.734375", "0.75", "0.765625", "0.78125", "0.796875", "0.8125", "0.828125", "0.84375", "0.859375", "0.875", "0.890625", "0.90625", "0.921875",
                  "0.9375", "0.953125", "0.96875", "0.984375"};
  char *fracarray[]={"1/64", "1/32", "3/64", "1/16", "5/64", "3/32", "7/64", "1/8", "9/64", "5/32", "11/64", "3/16", "13/64", "7/32",
                  "15/64", "1/4", "17/64", "9/32", "19/64", "5/16", "21/64", "11/32", "23/64", "3/8", "25/64", "13/32", "27/64", "7/16", "29/64",
                  "15/32", "31/64", "1/2", "33/64", "17/32", "35/64", "9/16", "37/64", "19/32", "39/64", "5/8", "41/64", "21/32", "43/64", "11/16",
                  "45/64", "23/32", "47/64", "3/4", "49/64", "25/32", "51/64", "13/16", "53/64", "27/32", "55/64", "7/8", "57/64", "29/32", "59/64",
                  "15/16", "61/64", "32/32", "63/64"};
    for (int i=0; i<63; i++){
      if (strcmp(decarray[i], input)==0){
        //Serial.print("decimal test fracarray: ");Serial.print(fracarray[i]);Serial.println(" match");
       Serial.print("decimal test result: ");Serial.println(decarray[i]);Serial.println(" match");
      }
    }

    Serial.print(("result mm= ")); Serial.println(conversionresult, 6);
  }
}

Thanks in advance for any help...

  • This is not the reason for the error, but your arrays should be declared as static const char *const decarray[] = { .... (Even better: relocate the string literals to PROGMEM). As for the actual problem... Apparently something else happens in the rest of the code. You said this is just a fraction of it. BTW, what is conversionresult? Where is it initialized? – AnT May 7 at 0:15
  • @AnT thanks, I'll change the array declaration and look into what relocating to PROGMEM means... like I said very new at this. conversionresult is a global variable float conversionresult; I use it to return the results when converting inches to mm. – JoMojo May 7 at 20:03
  • Otherwise there seems to be nothing wrong with the code you posted. – AnT May 7 at 20:07
  • @Ant just had time to look into your suggestion that something else was causing the problem... and found it, it was a global float causing the problem... can't actually explain why but it works without that line of code that I originally thought was the problem and doesn't with it.... thanks for the help – JoMojo May 8 at 23:35
  • It is highly unlikely that "a global float" by itself would cause such problem. Most likely that somewhere else in your code you have undefined behavior. It can be temporarily "spooked"/"swept under the carpet" by making random unrelated changes in the code (like your "global float"), but it will reappear eventually in some other form. – AnT May 8 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.