Actuator controls - it works, but it doesn't

Hi all.

So we’re setting up control of a range of actuators controlled by a Mega board. And luckily it works. But when we add serial communication it stops working. When i manually set the goals of the actuators by setting the goals array and commenting out the holdGoals part, it works. But when i use the data read from the serial port and store it in holdGoals and use it, the actuators doesn’t move or goes all the way to the top or bottom. Why? Has anyone got some experience with this?

The setup is as follows:

  1. The board listens for serial communication.
  2. It receives two values seperated by semi-colon and is terminated by a line-feed command (ie. 25;75;\n)
  3. The values are stored in an array
  4. The board reads the potentiometers on the analog ports, maps it and stores it in a array.
  5. If the read values are different from the goals, it sets the right pins to HIGH and LOW.
// Variables for serial communication
#define BUFFSIZE  12    // buffer one command at a time, 12 bytes is longer than the max length
char buffer[BUFFSIZE];  // this is the double buffer
uint8_t bufferidx = 0;
char *parseptr;
char buffidx;
int counter = 0;

// Control variable, used instead of return
boolean doInc = true;

//ACTUATOR VARIABLES//
int motorPinIn[] = {9,7};
int motorPinOut[] = {10,8};
int adcPin[] = {0,2};
int val[] = {0,0};
int sensorMin[] = {10,0};
int sensorMax[] = {926,915};
int goal[] = {50,50};
int holdGoals[] = {0,0};
//////////////////


void setup()   {                
  
  //Begin serial communication
  Serial.begin(19200);
  
  // ACTUATOR INITIALIZATION
  for(int i =0 ; i< sizeof(motorPinIn);i++){  
    pinMode(motorPinIn[i], OUTPUT);
    pinMode(motorPinOut[i], OUTPUT);
  }
  //////////////////////////
  
}


void loop()
{
 
  
  char c;    // holds one character from the serial port

  while (Serial.available()) {

    c = Serial.read();      // read one character
    
    buffer[bufferidx] = c;  // add to buffer
    
    if (c == ';') {  // Semi-colon seperates the goals for each of the actuators
      
      holdGoals[counter] = parsedecimal(buffer);    // offload the buffer into temp variable
      counter++;
      bufferidx = 0;      // reset the buffer for the next read
      
     doInc = false;        //return so that we don't trigger the index increment below

    }
    
    if (c == '\n') { 
      
      bufferidx = 0;
      
      counter = 0;
     
      for(int i =0 ; i< sizeof(holdGoals);i++){ 
        
        goal[i] = holdGoals[i];
        
      }
      
      // This part is used to test that, the board gets the goal values from the serial communication. 25 and 75 er just test values.
      if(goal[0]==25 && goal[1]==75){
          digitalWrite(13,HIGH);
      }else{
          digitalWrite(13,LOW);
      }
      
      doInc = false; //return so that we don't trigger the index increment below
      
    }
   
    if(doInc){
      bufferidx++;    // increment the index for the next character
      
    }else{
      doInc = true; 
    }
    
    if (bufferidx == BUFFSIZE-1) {  //if we get to the end of the buffer reset for safety
      bufferidx = 0;
    }
    
    
  }
  
  
  //ACTUATOR CONTROL//
  for(int i =0 ; i< sizeof(motorPinIn);i++){  
    //Read analog pin actuators
    val[i] = analogRead(adcPin[i]);
    //Map values from sensor into the min and max milimeter distance of travel length for the actuator
    val[i] = map(val[i], sensorMin[i], sensorMax[i], 0, 100);
    
    //If statement, if value read by poententiometer is more than goal, activate motor In and deactivate motor Out
    if (val[i] > goal[i]){
    digitalWrite(motorPinIn[i],HIGH);
    digitalWrite(motorPinOut[i],LOW);
    }
    //Else if statement, if value read by poententiometer is equal to goal, deactivate motor In and motor Out
    else if (val[i] == goal[i]){
    digitalWrite(motorPinIn[i],LOW);
    digitalWrite(motorPinOut[i],LOW);
    }
    //Else if statement, if value read by poententiometer is less than goal, deactivate motor In and activate motor Out
    else if (val[i] < goal[i]){
    digitalWrite(motorPinOut[i],HIGH);
    digitalWrite(motorPinIn[i],LOW);
    }
  }
  //////////////////  

}

//Function to parse string to int
int parsedecimal(char *str)
{
  int d = 0;

  while (str[0] != 0) {

    if ((str[0] > '9') || (str[0] < '0'))
      return d;
      d *= 10;
      d += str[0] - '0';
      str++;
    }

    return d;

 }

Thanks in advance
Thomas

Where is the serial data coming from?

It receives two values seperated by semi-colon and is terminated by a line-feed command (ie. 25;75;\n)

If the data is coming from the Serial Monitor, this statement is not true.

The data is sent from Rhino (3d-modelling program) to the Arduino board using serial communication (the com port). From Rhino it is formatted valueX;valueY;\n.

I've even checked that the values received are correct, by turning on a LED when the correct goal is set (25;75;\n).