How to convert int to string on Arduino?

前端 未结 9 1341
-上瘾入骨i
-上瘾入骨i 2021-01-31 06:32

How do I convert an int, n, to a string so that when I send it over the serial, it is sent as a string?

This is what I have so far:

int ledP         


        
相关标签:
9条回答
  • 2021-01-31 07:30

    You just need to wrap it around a String object like this:

    String numberString = String(n);
    

    You can also do:

    String stringOne = "Hello String";                     // using a constant String
    String stringOne =  String('a');                       // converting a constant char into a String
    String stringTwo =  String("This is a string");        // converting a constant string into a String object
    String stringOne =  String(stringTwo + " with more");  // concatenating two strings
    String stringOne =  String(13);                        // using a constant integer
    String stringOne =  String(analogRead(0), DEC);        // using an int and a base
    String stringOne =  String(45, HEX);                   // using an int and a base (hexadecimal)
    String stringOne =  String(255, BIN);                  // using an int and a base (binary)
    String stringOne =  String(millis(), DEC);             // using a long and a base
    
    0 讨论(0)
  • 2021-01-31 07:35

    This simply work for me:

    int bpm = 60;
    char text[256];
    sprintf(text, "Pulso: %d     ", bpm);
    //now use text as string
    
    0 讨论(0)
  • 2021-01-31 07:38
    Serial.println(val) 
    Serial.println(val, format)
    

    for more you can visit to the site of arduino https://www.arduino.cc/en/Serial/Println

    wish this will help you. thanks!

    0 讨论(0)
提交回复
热议问题