I am attempting to read a string of data in the form of (YD###PI###) which will move two different servos in different directions. I think I know what I have to do, but I am getting erros. Here is my code
#include <Servo.h>
#include <string.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo ud;
Servo lr;
int pos = 0; // variable to store the servo position
int udpos;
int lrpos;
String incoming;
char in[2];
char * pch;
void setup()
{
ud.attach(9); // attaches the servo on pin 9 to the servo object
lr.attach(10);
}
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
//incoming = Serial.read();
incoming=Serial.readString();
}
pch = strtok (incoming,"YDPI");
while (pch != NULL)
{
for(int i=0;i<2;i++)
{
in[i]=pch;
pch = strtok (NULL, "YDPI");
}
}
lrpos=(0.44)*int(in[1])+70;
lr.write(lrpos);
udpos=(0.33)*int(in[0])+75;
ud.write(udpos);
}
Here are my errors. Now sure how to fix?
I guess maybe I dont understand the difference between a string and a char array
servo.ino: In function 'void loop()':
servo:32: error: cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'
servo:38: error: invalid conversion from 'char*' to 'char'
It's really not clear to me what you're trying to do, but I assume what you really want to extract is the two fields shows as #'s in (YD###PI###)? strtok will stop parsing as soon as it finds the first character match, so:
if incoming = "(YD123PI456)"
strtok(incoming, "YDPI") finds the delimiter "Y", and returns "("
strtok(NULL, "YDPI") finds the delimiter "D", and returns an empty string
strtok(NULL, "YDPI") finds the delimiter "P", and "123"
strtok(NULL, "YDPI") finds the delimiter "I", and returns an empty string
strtok(NULL, "YDPI") finds no delimiter, and returns "456)"
Below is some test code for controlling several servos.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}