logo elektroda
logo elektroda
X
logo elektroda

Conversion of data from char to int array after reading from UART in Arduino

Maryush 927 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17241759
    Maryush
    Level 22  
    Welcome,

    I have this difficulty, I read the characters coming from the UART and write them to an array of characters defined as char value [] , the array stores values such as voltage, current and power. There are always 9 characters in the array, 3 for each of the given quantities. I want to take 3 characters out of this array and convert them to an int variable in a way I don't know yet. How can I do this? I am writing the program in arduino.
  • ADVERTISEMENT
  • #3 17242995
    Maryush
    Level 22  
    This has been successfully done, I am at the stage of converting a string variable to a float. And now after the conversion I have the variable A=27.500, I would like the variable A to have the form and be displayed as A=27.5, that is without these two unnecessary zeros. How can they be removed?
  • ADVERTISEMENT
  • #5 17243045
    Maryush
    Level 22  
    I only want to display two decimal places.
  • ADVERTISEMENT
  • #6 17243050
    witoldwitoldowicz
    Level 28  
    So
    String text= String(A, 2);
  • #7 17243055
    Maryush
    Level 22  
    And what would the function look like if I just wanted to remove those two zeros at the end?
  • ADVERTISEMENT
  • #8 17243103
    witoldwitoldowicz
    Level 28  
    Code: C / C++
    Log in, to see the code
    .
  • #9 17243176
    Maryush
    Level 22  
    Thank you very much for the code. I've switched from bascom to arduino and somehow can't shift my thinking. Apparently the syntax of this function is simple, but I can't quite understand the mechanism in which it works and in effect removes those unnecessary zeros. Could I still ask for an explanation of how this code works? So briefly.
  • Helpful post
    #10 17243203
    witoldwitoldowicz
    Level 28  
    if( if it has something after the comma
    while( as long as the last character is '0'
    remove remove last character
  • #11 17243218
    Maryush
    Level 22  
    That's right, thank you for your help.

    One more question, why does this function subtract a one from the length of the string variable if the last character is to be removed?
  • #12 17243933
    krzbor
    Level 27  
    Maryush wrote:
    .
    Still a question, why is a one subtracted from the length of the string variable in this function if the last character is to be removed?
    .
    Because "length" gives the length of the string, and the elements are indexed from 0. Example: for the string abc, "a" has index 0, "b" - 1, "c"-2, so the last character has index 2, but the length of the text is 3.

Topic summary

The discussion addresses converting a subset of characters read from UART into integer variables in Arduino. The initial solution involves copying three characters into a String object and using the String.toInt() function for conversion. Further elaboration covers converting the resulting string to a float and formatting the output to display a specific number of decimal places. To remove trailing zeros after the decimal point, a method using String manipulation is proposed: converting the float to a String, checking for a decimal point, and iteratively removing trailing '0' characters from the end of the string. The explanation clarifies that string indices start at zero, so removing the last character involves subtracting one from the string length to access the correct index. The discussion references Arduino String class functions such as toInt() and String(val, decimalPlaces) for formatting numeric output.
Summary generated by the language model.
ADVERTISEMENT