How do you convert a String to a float or int?

后端 未结 5 1584
醉酒成梦
醉酒成梦 2021-02-19 03:56

In an Arduino program I\'m working on the GPS sends the coordinates to the arduino through USB. Because of this, the incoming coordinates are stored as Strings. Is there any way

5条回答
  •  天命终不由人
    2021-02-19 04:27

    String stringOne, stringTwo, stringThree;
    int a;
    
    void setup() {
      // initialize serial and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
    
      stringOne = 12; //String("You added ");
      stringTwo = String("this string");
      stringThree = String();
      // send an intro:
      Serial.println("\n\nAdding Strings together (concatenation):");
      Serial.println();enter code here
    }
    
    void loop() {
      // adding a constant integer to a String:
      stringThree =  stringOne + 123;
      int gpslong =(stringThree.toInt());
      a=gpslong+8;
      //Serial.println(stringThree);    // prints "You added 123"
      Serial.println(a);    // prints "You added 123"
    }
    

提交回复
热议问题