Hey,
I would have expected this to be easy, but no surprises - I'm having a lot of trouble with it.
I'm need to use .substring() to fetch 2 charcters (numbers) from a long string.
I need to then save them as an integer for mathematical operations later.
I've tried to use atoi() and atol(), but they never compile because they don't like the dynamic fetching of the characters.
Could anyone please point me in the right direction?
Thanks!
void setup()
{
Serial.begin(9600);
String inputString = "this is a example of the string openHour: 08 and the rest of the string goes here";
int openHour;
openHour = inputString.indexOf("openHour:")+10; //collect '08' position from inputString
Serial.println(inputString.substring(openHour, openHour + 2)); //print '08' from inputString
openHour = inputString.substring(openHour, openHour + 2); // I want to convert the dynamically fetched 2 characters to an int [PROBLEM]
Serial.println(openHour);
}
void loop()
{
}
Not sure what you are trying here, but your second openhour operation will return a string not an int, so no surprise you get an error trying to save it back to open hour, since it cannot automatically convert it.
I know there is no automatic string to int conversion, but of the attempted work arounds I've tried - none of them were successful. So I didn't see the point of including them. So yeah, clearly this code doesn't work, but converting the string to integer is what I'm trying to do.
Some servo test code that has a method of converting a String to an integer.
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see parsed results
Serial.println(servo2);
int n1 = servo1.toInt();
int n2 = servo2.toInt();
Serial.println("the numbers are :");
Serial.println(n1); //print to serial monitor to see number results
Serial.println(n2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
servo1="";
servo2="";
}
}
Thanks for the .toInt() - of all the examples to convert strings to int, that one I never found.
I didn't know it existed after checking String() - Arduino Reference , because it isn't listed there either.
Here's my modified code, I've had to use a secondary string to store the contents. I don't think there is a way around this?
void setup()
{
Serial.begin(9600);
String inputString = "this is a example of the string openHour: 08 and the rest of the string goes here";
int openHour;
String extract;
openHour = inputString.indexOf("openHour:")+10; //collect '08' position from inputString
Serial.println(inputString.substring(openHour, openHour + 2)); //print '08' from inputString
extract = inputString.substring(openHour, openHour + 2); // saves characters found with .substring()
openHour = extract.toInt(); //converts simple string object to integer
Serial.println(openHour); //print integer
}
void loop()
{
}
Another method using an array. Note that I always do some cleanup of setting the Strings to "" when done.
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
servo1="";
servo2="";
}