new on this

hello Arduino community im new in this forum i have this program thats makes move a 6 dof arm whit a simulator, and i want to know if some one found an error because when i start the simulator all goes ok until the Arduino collapse. like 20 second then of started. is like the tx rx lock and they lock the arduino i dont know if the problem is in this code or in the simulator!! also can be a plugin on ubuntu ? thanks a lot.

agus.

//////////////////////////////////////////////////////////////////////////////////
// Receive a message via serial port.
// Interprets the message.
// Send orders for servos.
//////////////////////////////////////////////////////////////////////////////////

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
Servo myservo8;

char buffer[39]; //Array for the message

int pos=0;

int b1=0;

int ini=0;

void setup(){
Serial.begin(9600); //opening the serial port
Serial.begin(9600);
Serial.flush(); //Clean the serial port

pinMode(13, OUTPUT);

//Pins of the servos

myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(6);
myservo4.attach(9);
myservo5.attach(10);
myservo6.attach(11);
myservo7.attach(12);
myservo8.attach(13);

}

void loop(){

if (ini == 0){

myservo1.write(90);
myservo3.write(90);
myservo5.write(90);
myservo7.write(90);

myservo2.write(20);
myservo4.write(20);
myservo6.write(20);
myservo8.write(20);

ini = 2;
}

if (Serial.available() > 0){

int index=0;
delay(40);
int numChar = Serial.available();

if (numChar>39) {
numChar=39;
}

while (numChar–) {
buffer[index++] = Serial.read();
//delay(10);
}

Serial.println(buffer);
AnalizaLectura(buffer);

}
}

void AnalizaLectura(char* data){

char* parameter;
parameter = strtok(data,".");

while (parameter != NULL) {
setAction(parameter);
parameter = strtok(NULL,".");
}

// Clear the text and serial buffers
for (int k=0; k<40; k++) {
buffer[k]=’\0’;
}

Serial.flush();

}

void setAction(char* data) {

if (data[0] == ‘A’) {

pos=strtol(data+1, NULL, 10);

myservo1.write(pos);

pos=0;
}

if (data[0] == ‘B’) {

pos=strtol(data+1, NULL, 10);

myservo3.write(pos);

pos=0;
}

if (data[0] == ‘C’) {

pos=strtol(data+1, NULL, 10);

myservo5.write(pos);

pos=0;
}

if (data[0] == ‘D’) {

pos=strtol(data+1, NULL, 10);

myservo7.write(pos);

pos=0;
}

if (data[0] == ‘E’) {

pos=strtol(data+1, NULL, 10);

myservo2.write(pos);

pos=0;
}

if (data[0] == ‘F’) {

pos=strtol(data+1, NULL, 10);

myservo4.write(pos);

pos=0;
}

if (data[0] == ‘G’) {

pos=strtol(data+1, NULL, 10);

myservo6.write(pos);

pos=0;
}

if (data[0] == ‘H’) {

pos=strtol(data+1, NULL, 10);

myservo8.write(pos);

pos=0;
}

}

robotic_arm_arduino1.ino (2.91 KB)

  for (int k=0; k<40; k++)

Write to all 40 elements of a 39 element array?

Please remember to use code tags when posting code.

Allow me to do the honors. Eh em... Please put your code in the "Insert Code" brackets. Like this:

void loop () {
Serial.println("Hello World!");
}

)

Serial.flush();        //Clean the serial port

You haven't sent anything yet, so that's just a waste of time.

Can you see how the following code repeats with only minor changes?

 if (data[0] == 'A') {
   
     pos=strtol(data+1, NULL, 10);
       
     myservo1.write(pos);  
            
     pos=0; 
  }

This screams "array" - it'll shorten your code- I also don't see why the initial servo positioning can't be done in "setup"

thanks for the answers !!! AWOL can you explain me about what to do on the Serial.flush ? about of the servo position its done like that because of the simulator 3d program.

//////////////////////////////////////////////////////////////////////////////////
// Receive a message via serial port. 
// Interprets the message. 
// Send orders for servos.
//////////////////////////////////////////////////////////////////////////////////

#include <Servo.h> 
 
 
Servo myservo1;      
Servo myservo2;      
Servo myservo3;      
Servo myservo4;      
Servo myservo5;      
Servo myservo6;     
Servo myservo7;     
Servo myservo8;     
 


char buffer[39];  //Array for the message

int pos=0;        

int b1=0;         

int ini=0;        

void setup(){
  Serial.begin(9600);    //opening the serial port
  Serial.begin(9600);
  Serial.flush();        //Clean the serial port
  
    pinMode(13, OUTPUT);  

 //Pins of the servos
  
  myservo1.attach(3);  
  myservo2.attach(5); 
  myservo3.attach(6);
  myservo4.attach(9);
  myservo5.attach(10);
  myservo6.attach(11);
  myservo7.attach(12);
  myservo8.attach(13);
  
}


void loop(){
  
 if (ini == 0){
   
     myservo1.write(90);
     myservo3.write(90);
     myservo5.write(90);
     myservo7.write(90);
     
     myservo2.write(20);
     myservo4.write(20);
     myservo6.write(20);
     myservo8.write(20);
     
     ini = 2;
 }
 
 
  
  if (Serial.available() > 0){    
   
    int index=0;
    delay(40); 
    int numChar = Serial.available();    
    
    if (numChar>39) {
          numChar=39;
    }

    while (numChar--) {
        buffer[index++] = Serial.read();
        //delay(10);
    }
    


    Serial.println(buffer);
    AnalizaLectura(buffer);


  }  
}    
     
  

void AnalizaLectura(char* data){  

   char* parameter;              
   parameter = strtok(data,"."); 

   while (parameter != NULL) {
       setAction(parameter);  
       parameter = strtok(NULL,"."); 
   }

  // Clear the text and serial buffers
  for (int k=0; k<40; k++) {
      buffer[k]='\0';
   }

      Serial.flush(); 
      


}




void setAction(char* data) {
  
   if (data[0] == 'A') {
   
     pos=strtol(data+1, NULL, 10);
       
     myservo1.write(pos);  
            
     pos=0; 
  }
  
  if (data[0] == 'B') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo3.write(pos);  
  
     pos=0; 
  }
  
  if (data[0] == 'C') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo5.write(pos);
            
     pos=0; 
  }
  
  if (data[0] == 'D') {
    
     pos=strtol(data+1, NULL, 10);
       
     myservo7.write(pos);  
     
     pos=0; 
  }
  
  if (data[0] == 'E') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo2.write(pos);  
            
     pos=0; 
  }
  
  if (data[0] == 'F') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo4.write(pos);  
     
     pos=0; 
  }
  
  if (data[0] == 'G') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo6.write(pos);  
     
     pos=0; 
  }
  
  if (data[0] == 'H') { 
   
     pos=strtol(data+1, NULL, 10);
       
     myservo8.write(pos);  
     
     pos=0; 
  }
    
  
 

  
}

How are you powering all these servos, and the arduino? You have them on different power supplies I assume? You have a few bypass capacitors spread around to cut down on noise?

hey jack wp! thanks for the interesting. em the servos are with a external power supply and yes with capacitors. here the problem is the comunication!!! passing the 20 seconds of runing the simulator with the arduino the serial comunication stops.

Increase the size of the buffer array by one.

Append a null terminator after every character appended to it.

You can clean up the serial processing substantially - you only need to know the value of index and that available() > 0, you don’t need to keep a separate count of the number of characters remaining to be read.

Your serial handling code seems to assume that every incoming message is complete at the point you start receiving it, which does not seem a safe assumption. It would be much safer to explicitly recognise the end of the message, for example by looking for an end-of-line marker.

The parsing code looks as if it would work when you get the expected message format but does not look very robust - what would happen if you get a partial or corrupted message ending with a ‘.’?

Here is a sample demonstrating how to buffer characters received from the serial port and only process the message when a complete line has been received:

// incomplete
void handleSerial()
{
    const int BUFF_SIZE = 32; // make it big enough to hold your longest command
    static char buffer[BUFF_SIZE+1]; // +1 allows space for the null terminator
    static int length = 0; // number of characters currently in the buffer

    if(Serial.available())
    {
        char c = Serial.read();
        if((c == '\r') || (c == '\n'))
        {
            // end-of-line received
            if(length > 0)
            {
                handleReceivedMessage(buffer);
            }
            length = 0;
        }
        else
        {
            if(length < BUFF_SIZE)
            {
                buffer[length++] = c; // append the received character to the array
                buffer[length] = 0; // append the null terminator
            }
            else
            {
                // buffer full - discard the received character
            }
        }
    }
}

void handleReceivedMessage(char *msg)
{
	if(strcmp(msg, "on") == 0)
	{
		// handle the command "on"
	}
	else
	{
		// etc
	}
}
  Serial.flush();        //Clean the serial port

It does not!. Get rid of this useless call. Unless you've got a really good reason for blocking until all of the serial data you have sent (none) gets shuffled out.

Some servo test code that might be similar to what you may be looking for. I don’t have servos connected to test, so YMMV.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

passing the 20 seconds of runing the simulator with the arduino the serial comunication stops.

Does just the serial communication stop, or does the arduino stop executing properly? Do you have a blinking light in the main loop to indicate that the loop is still running? If so, does it stop blinking? If not, can you add that to the sketch?

Does just the serial communication stop, or does the arduino stop executing properly? Do you have a blinking light in the main loop to indicate that the loop is still running? If so, does it stop blinking? If not, can you add that to the sketch?/quote]

THANKS zoomkat !! i will trie with parts of your code.

well.... em with these... the comunication stops " the tx rx leds " ( sometimes sometimes remain lit, and sometimes off depending what was doing the simulator )the leds of comunication are the ones who give me the indication that its running all okey. in some moment " about 20 seconds "