I have some code executing quite well with orders from a serial input. Though, till now I am using single chars like 1,2,3,a,b,c. To each input are established specific orders, this reveal to work as expected. Now I would like to built a more complex protocol in order to bypass some useless intermediate actions.
For instance, in order to get a servo to move from one position to another I would just type 'a' and the servo would increase or decrease from the current position to one degree up or down. This method is not the most efficient one and by far not the fastest since I had to have a delay associated to each degree in order to be sure that the servo manage to get to the right position before I could order it to go to a new one.
In this case, I would need the arduino to read from starting char to a termination one to get the full string, only than to execute the rest of the code expecting to be able to read the full string. Instead of establish a number of bits to read, the arduino could wait for a '!' to start reading and a '?' to terminate. Like this I could get, for instance, '!SERVO1 45?' and the code would get that the information is to be address to the servo 1 and to move to an equivalent position 45.
I made some research before writing down this post, though the examples I found only mention a predicted size of a string or to read everything till a termination char that I wasn't successfully adapting. Neither of each mention a starting char to read from.
//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
}
}
}
In this case I continue to use one char to communicate with the most of actuators and sensors. Though to the servos the code above is an example of the serial protocol I may write.
So, if I type "TP" it just gives me the current position, "T50!" will adjust the servo throttle to 50%.