hi
i wrote this code a little while ago
it gets updated positions through serial and i was controlling it off the computer
you can probabily make it a hell of a lot better very easily but anyway thought it might help:
#include <string.h>
#include <ctype.h>
//servo rotation bits
int servnum[11] = {3, 5, 6, 9, 10, 11, 14, 15, 16, 17, 18};
int rotation[5] = {90, 90, 90, 90, 90};
int pw = 0;
//converting shit
int inputbyte =-1;
char linea[300] = "";
char servopin[7] = "$SERVO";
int conta = 0;
int cont = 0;
int indices[5];
int match=0;
void setup(){
for (int i=0;i<11;i++){
pinMode(servnum[i], OUTPUT);
}
Serial.begin(9600);
for (int i=0;i<300;i++){ // Initialize a buffer for received data
linea[i]=' ';
}
}
void loop(){
inputbyte=Serial.read();
if(inputbyte ==-1){
servo();
} else {
input();
}
}
void servo(){
for(int t=0; t<11; t++){
pw = ((rotation[t] * 10) + 600); // determines delay
digitalWrite(servnum[t], HIGH); // set servo high
delayMicroseconds(pw); // micro pause
digitalWrite(servnum[t], LOW); // set servo low
}
delay(10); // refresh cycle
}
void input(){
cont=0;
conta=0;
match=0;
for(int h=0; h<=60; h++){
linea[h]=inputbyte; // If there is serial port data, it is put in the buffer
inputbyte=Serial.read();
delay(2);
}
//serial Checker (prints linea)
Serial.println("linea =");
for(int y=0; y<60; y++){
Serial.print(linea[y]);
}
Serial.println("");
Serial.println("");
//prints the servopin 2 serial 2 check it
Serial.println("servopin = ");
for (int i=0;i<7;i++){
Serial.print(servopin[i]);
}
Serial.println("");
Serial.println("");
//Checks the start = servopin
for (int i=0;i<7;i++){
if (linea[i]==servopin[i]){
match++;
Serial.println("positave");
} else {
Serial.println("negitave");
}
}
Serial.println("");
Serial.println("");
if 5 or more characters match
if(match==6){
Serial.println("got to here!");
Serial.println("");
Serial.println("");
//find the , seperators and the * end
for (int i=0;i<300;i++){
if (linea[i]==','){ // check for the position of the "," separator
indices[cont]=i;
cont++;
}
if (linea[i]=='*'){ // ... and the "*"
indices[5]=i;
cont++;
}
}
for(int j=0; j<5; j++){
Serial.print("indices ");
Serial.print(j);
Serial.print(" = ");
Serial.println(indices[j]);
}
for(int g=0; g<5; g++){
if((indices[g+1] - indices[g])-1 == 1){
Serial.println("1 spacing");
rotation[g] = (linea[indices[g]+1] - '0');
}
if((indices[g+1] - indices[g])-1 == 2){
Serial.println("2 spacing");
rotation[g] = (linea[indices[g]+1] - '0') * 10 + (linea[indices[g]+2] - '0');
}
if((indices[g+1] - indices[g])-1 == 3){
Serial.println("3 spacing");
rotation[g] = (linea[indices[g]+1] - '0') * 100 + (linea[indices[g]+2] - '0') * 10 + (linea[indices[g]+3] - '0');
}
}
//Serial.println("");
//Serial.println("");
Serial.println("Rotations");
Serial.println(rotation[0]);
Serial.println(rotation[1]);
Serial.println(rotation[2]);
Serial.println(rotation[3]);
Serial.println(rotation[4]);
}
}
good luck with it
callum