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)