For the life of me, I can't figure this out! I'm simply trying to find ALL the numbers in a string and put them in an int. So, like, say the string was, " blakalskjdfj10" it would put the value 10 in an int. I've tried atol and tostring and I I've tried one or two others that I can't remember now. The tostring only checks the first "thing" in the string and if it's a number it keeps going, otherwise it just stops and returns a zero. The atol gives a compiler error saying "cannot convert 'String' to 'const char*' for argument '1' to 'long int atol(const char*)'". Thanks in advance.
So, get it to find the first thing in the string that is a digit, and work from there.
Don't use String; that's just wasteful
You could do it this way:
char test[] = "gsgsgasff12hhbgt3zz44ikki55pmm0";
unsigned long allNumbers(const char* ptr) {
unsigned long result = 0;
while (*ptr) {
if (isdigit(*ptr)) {
result *= 10;
result += *ptr - '0';
}
ptr++;
}
return result;
}
void setup() {
Serial.begin(115200);
Serial.println(allNumbers(test));
}
void loop() {}
12344550
If you were using a char string, atol() would have worked. Don't use the String class on Arduino, it has memory management issues.
Thanks for your replies guys. When I tried using chars it gave me a harder time than with strings. I'll give the characters another shot. Though, the more I look into it the more confusing it gets. Could you please tell me how your code works, Whandall? It would be really appreciated.
No big deal. It is like scanning a number from serial.
- initialize result to zero
- while there are characters, inspect each of them
- if it is a digit, multiply result by ten and add value corresponding to digit
- if end of string reached, return result
Yeah, the standard functions won't help you a lot, because what you are trying to do isn't one of those standard things. I mean, for instance - you you want to pick out all the digits in your string, or do you just want to pick out the first run of digits? If your string is 'xxxx123xxx345xxx', do you want 123, or 123345? The standard functions simply go "if the string is not a number, barf" because if they didn't then the whole world would be full of little "gotcha!" clauses.
I'm simply trying to find ALL the numbers in a string and put them in an int.
Very simple test code that converts a captured String into an integer. You need to provide the example of character string that will be captured to decide how to parse out the desired data. Do you have control over how the data is being sent?
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
Serial.println(n); //so you can see the integer
myservo.write(n);
readString="";
}
}
Thanks for all your guy's help! It finally works! I never would've gotten it by my self. Here's the ending code if anyone's curious.
#include <OneSheeld.h>
#define CUSTOM_SETTINGS
#define INCLUDE_TEXT_TO_SPEECH_SHIELD
#define INCLUDE_VOICE_RECOGNIZER_SHIELD
#include <BioloidController.h>
BioloidController bioloid = BioloidController(1000000);
String lastcommand;
unsigned long lastnumbers = 0;
void setup() {
OneSheeld.begin();
}
void loop() {
if(VoiceRecognition.isNewCommandReceived()){
lastnumbers = allNumbers(VoiceRecognition.getLastCommand());
lastcommand = VoiceRecognition.getCommandAsString();
if(compare(lastcommand, "move servo left")){
SetPosition(13, ax12GetRegister(13, 36, 2)+lastnumbers/.29);
delay(3000);
}
else if(compare(lastcommand, "move servo right")||compare(lastcommand, "move servo wright")||compare(lastcommand, "move servo write")){
SetPosition(13, ax12GetRegister(13, 36, 2)-lastnumbers/.29);
delay(3000);
}
}
}
unsigned long allNumbers(const char* ptr) {//Thanks Whandall!!!
unsigned long result = 0;
while (*ptr) {
if (isdigit(*ptr)) {
result *= 10;
result += *ptr - '0';
}
ptr++;
}
return result;
}
boolean compare(String x, String y){
if(x.indexOf(y)>= 0)
return true;
else
return false;
}