0

I am using TinkerCad as Arduino UNO R3 simulator. My aim is to convert int to string but the following code prints 188 instead of 180.

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


void loop()
{
  Serial.println(String(180));
}

Serial monitor output

Output is 188 as you can see in the screenshot from TinkerCad serial monitor above. Why is there a weird problem with String?

The Exile
  • 644
  • 8
  • 14

3 Answers3

1

Seems a tinkercad problem. Your weird sketch works well on a real Arduino.

datafiddler
  • 1,755
  • 3
  • 17
  • 30
1

Had the same issue with tinkercad.
String (intval) or (String) intval was not working.

Kludge: using lcd.write(int); instead if possible (i.e. use int where you can and send int directly to write).

DaveL17
  • 1,673
  • 7
  • 24
  • 38
gologozo
  • 11
  • 1
0

That's really weird. Try this:

int intValue = 180;
string stringValue;

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


void loop()
  {
    stringValue = String(intValue);
    Serial.println(stringValue);
  
}
Slushee
  • 26
  • 6