split_string_2.ino: In function 'void loop()':
split_string_2:14: error: invalid conversion from 'int' to 'const char*'
split_string_2:14: error: initializing argument 1 of 'String& String::operator=(const char*)'
nehj:
how can i convert the Serial.read to a string?
A serial read will only net you one character of data, you can make this into a string but it will not help you. You must gather the bytes you read into a string and add a null terminator before calling your parsing routine.
nehj:
how can i convert the Serial.read to a string?
A serial read will only net you one character of data, you can make this into a string but it will not help you. You must gather the bytes you read into a string and add a null terminator before calling your parsing routine.
This is what you have been told in every other thread where you have asked the same question. If you don't like the answer, return the Arduino and ask for your money back.
nehj:
how can i convert the Serial.read to a string?
Simple test code that captures serial input and extracts the desired data from the captured string.
// 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="";
}
}
Grumpy_Mike:
Wow zoomkat, what a great teacher. I bet he learn a lot from that. Code that only works at certain baud rates. I am totally underwhelmed.
At least it works, compared to the non working code you have provided. The op seemed to want to see a method of capturing a string and extracting data from the string, not "snippits-R-Us.com" material. Post your working code, or perhaps it is time for your nap. 8)
zoomkat:
At least it works, compared to the non working code you have provided. The op seemed to want to see a method of capturing a string and extracting data from the string, not "snippits-R-Us.com" material. Post your working code, or perhaps it is time for your nap. 8)
Or we could just teach him to fish? You seem to be the only one who feels like giving him the fish, and a smelly one at that.
Or we could just teach him to fish? You seem to be the only one who feels like giving him the fish, and a smelly one at that.
Couldn't have put it better myself.
Zoomcat has an odd idea about education and helping, and seems to be increasingly fond of dishing out personal abuse.