I am sending the commands out Arduino for servo motor positions. I am using the following code for Arduino:
int nextServo = 0;
int servosPos[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Skipping some setup code that is not involved with this problem.
void loop() {
if(Serial.available()){
int servoPos = Serial.read(); //set input position to number coming in from processing
servosPos[nextServo] = servoPos;
nextServo++;
if(nextServo > 13)
{
nextServo = 0;
}
Serial1.write(0xFF); //short protocol command
Serial1.write(1); //servo channel number
Serial1.write(servosPos[0]); //send the input position
Serial1.write(0xFF); //short protocol command
Serial1.write(2); //servo channel number
Serial1.write(servosPos[1]); //send the input position
Serial1.write(0xFF); //short protocol command
Serial1.write(3); //servo channel number
Serial1.write(servosPos[2]); //send the input position
and this continues up till servosPos[13]...so there are 14 total. I was thinking that the array of data was stored each time and I could call a particular data by using servosPos#
My code works when I change the Arduino side for only writing out to one servomotor. Could this be a speed issue? I have a 15ms delay for each run on the Processing side to try not to overload Arduino.
When I run the code with all the ports active it looks like the data is getting all out of order. I basically built this code off of some examples that were using an array of 2 numbers.
Thanks for the response! I tried the while loop method and it is almost working. I am using processing to track a person. The output of my servomotor mechanism appears to be "fighting" itself to track a person. If I move left, it follows left. If I move right, it follows right. However, it stutters constantly like it is getting a very quick short command to move to a different position. I am fairly confident that it is not on the Processing side of things because I am printing the output position commands and they appear correct and consistent.
Arduino Code before void setup() (These are resting positions that I want the motors to start on):
void loop() {
int counter = 0;
while (Serial.available())
{
servosPos[counter] = Serial.read();
counter++;
if (counter == 13)
{
break; //exit the loop
}
}
Serial1.write(0xFF); //short protocol command
Serial1.write(1); //servo channel number
Serial1.write(servosPos[0]); //send the input position
Serial1.write(0xFF); //short protocol command
Serial1.write(2); //servo channel number
Serial1.write(servosPos[1]); //send the input position
Serial1.write(0xFF); //short protocol command
Serial1.write(3); //servo channel number
Serial1.write(servosPos[2]); //send the input position
Serial1.write(0xFF); //short protocol command
Serial1.write(4); //servo channel number
Serial1.write(servosPos[3]); //send the input position
//etcetera..... for all 14 servo motors.
}
The processing side of things are still the same. The numbers being sent are from 0-255.
I'm kinda new to c++ and hope I don't derail the topic.. but wouldn't this code work and be more efficient?? Or is Serial and Serial1 not available at the same time??
void loop() {
int counter = 0;
while (Serial.available())
{
int channel = counter +1;
servosPos[counter] = Serial.read();
Serial1.write(0xFF); //short protocol command
Serial1.write(channel); //servo channel number
Serial1.write(servosPos[counter]); //send the input position
counter++;
if (counter == 13)
{
break; //exit the loop
}
}
}