Basically there is a void loop() which receives the incoming data but in my servo code that was used with pots there is another void loop() to control the servos. I attempted to use the schedule library which arduino recognises successfully but am getting an error saying,
Rec_servo.ino: In function 'void setup()':
Rec_servo:36: error: expected unqualified-id before '.' token
expected unqualified-id before '.' token
Is there a way to get around this without using two separate loops?
Here is the code. I know its a bit messy atm with renaming variable but will clean it all up once it works.
#include <VarSpeedServo.h>
#include <Scheduler.h>
VarSpeedServo myservo1;
VarSpeedServo myservo2;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int Data_1 = 0;
int Data_2 = 0;
int Data_3 = 0;
int val1; // variable to read the value from the analog pin
int val2;
int val3;
int val4;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
Scheduler.startLoop(loop1);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Data_1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); //this continues where the previous call left off
Data_2 = atoi(strtokIndx); // convert this part to a int
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Data_3 = atoi(strtokIndx); // convert this part to an integer
}
void showParsedData() {
Serial.print("Data 1 ");
Serial.println(Data_1);
Serial.print("Data 2");
Serial.println(Data_2);
Serial.print("Data 3 ");
Serial.println(Data_3);
}
////////////////////////////////////////////////////////servos///////
void loop2()
{
/////////servospeed//////////
val4 = Data_3;
val4 = (val4/100);
//////Servo 1////////////////
val1 = Data_1;
myservo1.slowmove(val1, val4); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
//////Servo 2////////////////
val2 = Data_2;
myservo2.slowmove(val2, val4);
delay(1);
yield();
}