Executing a function

Hello,

I’m an amateur when it comes down to writing code but currently I’m working on a project and need some advice. I’ve built a robot with wheels, a webcam for my image processing software(RoboRealm), and I also built an arm for it. The goal is for the robot to find the object, center itself to the object, move a certain distance away (serialread from roborealm), and than execute my function arm to grab the object. Everything works great till I have to execute the arm.

I’m reading the values from roborealm to turn left or right, where ever the object is. Than I have the robot move forward or backwards, I would like it to grab the object here after it completes centering. It should only execute onces unless it needs to re center. I am not sure how or where to write this on my code. I’m asking to understand how to write it and possibly a short example. Thank you, any help is useful.

Here is my line of the void loop where I would like my function called out.

void loop()
{
  if (Serial.available()){              // Do stuff if serial available
  
//============================================================================
// Data Parsing Algorithm
// RoboRealm serial send sequence:  P[COG_X];D[COG_BOX_SIZE];
//============================================================================

    char ch = Serial.read();
    if(ch >= '0' && ch <= '9'){	        // Check if character is a number
        pos = pos * 10 + ch - '0';	// If yes, accumulate the value
        }
         
    else if(ch != ';'){                 // Check for delimiter
	command[count] = ch;
	pos = 0;
        }
    
    if( ch == ';' ){ 
        var[count] = pos;
	  if(command[0] == 'P'){        //Pan variable
            x = var[0];
            }

          if(command[1] == 'D'){        //Distance(BoxSize) variable
            d = var[1];
            }
           
        count++;
        pos = 0;   
        }      
     
//============================================================================
// Servo/Motor Contol Algorithms
//============================================================================
// Print back parsed data

    if(count == 3){     
          for (int i = 0; i < 3; i++){
              Serial.print(command[i]);Serial.print(var[i]);Serial.print(' ');
              }
              
// Pan servo algorithm
          if(x < (imageX/2 - tolerance) && x != 0){        // If object x is to the left of the y-axis move right
              LW.writeMicroseconds(1600);  
              RW.writeMicroseconds(1600);  
              delay(100);
              LW.writeMicroseconds(1500);  
              RW.writeMicroseconds(1500);
              }
        
          else if(x > (imageX/2 + tolerance) && x != 0){   // If object x is to the right of the y-axis move left
              LW.writeMicroseconds(1400);  
              RW.writeMicroseconds(1400);
              delay(100);
              LW.writeMicroseconds(1500); 
              RW.writeMicroseconds(1500);
              }
        
          else if(x == 0){                                 // If object x is not in sight, search by rotating clockwise continuously
              LW.writeMicroseconds(1600);  
              RW.writeMicroseconds(1600);  
              }
       
          else if (d > 90  && d > 110){                    // If distance of object x is greater than 12 inches, move backwards
              LW.writeMicroseconds(1600);  
              RW.writeMicroseconds(1400);
              }
       
          else if (d < 90  && d < 110 ) {                 // If distance of object x is less than 12 inches, move forward
              LW.writeMicroseconds(1400);  
              RW.writeMicroseconds(1600);
              }
              
          else if (d == 0 ) {                 
              LW.writeMicroseconds(1500);  
              RW.writeMicroseconds(1500);
              }                                     
          count = 0;                                       // Reset count to retrieve next data packet          
        }          
        }
}

Your code would be easier to deal with if you created some functions:

void readNextByte()
{
   if (Serial.available())
   { 
//============================================================================
// Data Parsing Algorithm
// RoboRealm serial send sequence:  P[COG_X];D[COG_BOX_SIZE];
//============================================================================

      char ch = Serial.read();
      if(ch >= '0' && ch <= '9')
      {	        // Check if character is a number
        pos = pos * 10 + ch - '0';	// If yes, accumulate the value
      }
      else if(ch != ';')
      {                 // Check for delimiter
        command[count] = ch;
        pos = 0;
      }
      
      if( ch == ';' )
      { 
        var[count] = pos;
        if(command[0] == 'P')
        {        //Pan variable
            x = var[0];
        }
        else if(command[1] == 'D')
        {        //Distance(BoxSize) variable
           d = var[1];
        }

        count++;
        pos = 0;   
    }      
}
void pan()
{
   if(x < (imageX/2 - tolerance) && x != 0)
   {        // If object x is to the left of the y-axis move right
      LW.writeMicroseconds(1600);  
      RW.writeMicroseconds(1600);  
      delay(100);
      LW.writeMicroseconds(1500);  
      RW.writeMicroseconds(1500);
   }
   else if(x > (imageX/2 + tolerance) && x != 0)
   {   // If object x is to the right of the y-axis move left
      LW.writeMicroseconds(1400);  
      RW.writeMicroseconds(1400);
      delay(100);
      LW.writeMicroseconds(1500); 
      RW.writeMicroseconds(1500);
   }
   else if(x == 0)
   {                                 // If object x is not in sight, search by rotating clockwise continuously
       LW.writeMicroseconds(1600);  
       RW.writeMicroseconds(1600);  
   }
}
void move()
{
   if (d > 90  && d > 110)
   {  // If distance of object x is greater than 12 inches, move backwards
      LW.writeMicroseconds(1600);  
      RW.writeMicroseconds(1400);
   }
   else if (d < 90  && d < 110 )
   {  // If distance of object x is less than 12 inches, move forward
      LW.writeMicroseconds(1400);  
      RW.writeMicroseconds(1600);
   }
   else if (d == 0 )
   {                 
      LW.writeMicroseconds(1500);  
      RW.writeMicroseconds(1500);
   }
}

Then, loop can be much simpler;

void loop()
{
   readNextByte();
     
//============================================================================
// Servo/Motor Control Algorithms
//============================================================================
// Print back parsed data

    if(count == 3)
    {     
       for (int i = 0; i < 3; i++)
       {
          Serial.print(command[i]);Serial.print(var[i]);Serial.print(' ');
       }
              
// Pan servo algorithm
      pan();
      move();
   }
}

Now, you can add a (conditional) call to arm(), to grab the object. Either before, so the call is conditional, or in, so the call is unconditional, you need to determine if the object is in range, so it can be grabbed, or not. If it is in range, grab it and set a flag to show that you have the object. Don’t call the arm function, or don’t do anything in that function, if the flag (a boolean variable) is true.

Of course, at some point, you’ll want to release the object, and set the flag back to false.

Wow Paul! I don't know what to say, other than Thank You for simplifying my code. It's much better than what I have, but it comes with my amateur title lol.