Arduino, matlab and servo control

I'm trying to interface my mega 2560 with a matlab. I just have to move two servo motors based on values sent by matlab. Unfortunately I have had zero luck and am unable to debug. Could someone look at the code and maybe lead me towards the problem?
[Arduino][ard - Pastebin.com]
[Matlab][servo - Pastebin.com]

Thank you in advance

twoservo.m (677 Bytes)

UWFLO.ino (13 KB)

interfacing matlab has been discussed several times on the forum. Have you tried to search in the upper right corner?

The .ino file looks rather complex, can you explain the meaning of the states?

You can compare val like this

if (val < 'a') { ....

makes it a bit more readable imho

hi there!

i am also searching for how to set up my arduino with matlab.

i want to use matlab to controll a servo, connected to a arduino uno.

i read every post i could find in this forum about "matlab servo", but nothing i found was helpful.

i also searched the internet, and this is what i get so far:

MATLAB CODE:

%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);
fprintf(arduino,'%s','120'); %Write 120 to ComPort
fwrite(arduino,120);
pause(0.02)


fclose(arduino);

delete(arduino)
clear arduino

fprintf(arduino,'%s','120');
fwrite(arduino,120);
those are two ways i found to send data, but both of them dont work....

ARDUINO CODE:

/*
Matlab-Arduino Servo Controller
*/


#include <Servo.h> 


//DECLARATION--------------------- 
Servo servo1;



//SETUP----------------------------
void setup() {
  
  Serial.begin(9600);
    
  servo1.attach(6);
   
   
}


//LOOP--------------------------------
void loop() {

 if( Serial.available() ) 
    {
        int x = Serial.read();
        servo1.write(x);
    }
  
}

It seems Matlab isnt sending the right data.
If is send data, arduino RX Led is blinking, but nothing happens.
SO it gets something, but not the angle (ie. 120) to controll the servo.

i hope somebody can help me with that problem.

thank you!

---moe

EDIT:

ok, i found the error!!!!

before u send data to serial with matlab you have to give arduino a little break!
just insert a:

pause(0.5) before the write commando

like this!

%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);
pause(0.5) % this is very important! arduino needs a little time to initialise the fopen command!

for i = 0:179  % Runs your servo from 0-179 degree
fwrite(arduino,i);
pause(0.02)
end

fclose(arduino);

delete(arduino)
clear arduino

That's because the Arduino resets when the serial port is opened.

ok, now i got another problem.

i want to sent not only numbers to arduino with matlab.

i want to send following code:

p7m45g

this means: servo on pin 7 with 45 degree

how can i send this with matlab?

my code is:

%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);

pause(0.1) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!

fprintf(arduino,'%s','p7m20g')

fclose(arduino);
delete(arduino)
clear arduino

but it doesnt work, any ideo?

---moe

edit:

ok, solved it.
you have to set the pause after fopen longer.
a pause of 1.7 seconds worked
is there any other way to solve this?

%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',115200); % Set ComPort

fopen(arduino);

pause(1.7) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!

fprintf(arduino,'%s','p7m45g') %servo on pin 7 to 45 degree
fprintf(arduino,'%s','p6m135g') % servo on pin 6 to 135 degree

fclose(arduino);
delete(arduino)
clear arduino

Yes. You can disable the auto-reset - something else to search for.

thank you.

it seems to disable auto reset on a arduino uno, you have to use a 10uF capacitor (Resetpin- 10uF - Groundpin).
i will give it a try, when im home again.

---moe

Hi,
Im new about the arduino language. I have tried control the LED by using the matlab through Arduino, it work well to me, but cannot control even move the servo motor as i refer to the arduino forum the previous forum about arduino matlab servo.
There are my steps and code in order to control and interface.
1.dowload the arduino i/o support package and installed it.
2.connect the arduino to PC via ubs port
3.dowload and install the arduino software and 'upload' the adiosrv into arduino duemilanove (also try in romeo) board
4.type arduino in the matlab and it connect successful.
5.and then type "a=arduino('COM8')", "a.servoAttach(1)"and "a.servoWrite(1,50)" to move the servo motor 1(pin 9) from 0 to 50 degree.

but it seem does not work at all.
my question are:
1.is there any steps that i miss out?

2.any software is required others than what i mention above?

3.the romeo or the duemilanove that assign to "a.servoAttach(1)" is represented pin 9 and "a.servoAttach(2)"

help needed,this code below is for one servo ,suppose if i have 5 servo how do i change the void loop?

 Serial.begin(9600);
    
  servo1.attach(6);
   
   
}


//LOOP--------------------------------
void loop() {

 if( Serial.available() ) 
    {
        int x = Serial.read();
        servo1.write(x);
    }
  
}

suppose if i have 5 servo how do i change the void loop?

You need to send more data. You need to send which servo the data is for as well as the data for the servo.

Then, you read two bytes of data - servo # and servo val - and move the corresponding servo. And, hope like hell no serial data ever gets lost. Don't forget that you need to wait for two or more bytes, not one or more, too.

Multi servo test code.

//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
    }
  }
}

Friends for matlab and arduino interfacing my article written at i have provided steps and images to reduce difficulty I hope you all will be benifited

thanks: