1565Views7Replies

Author Options:

Matlab-Arduino Multi Servos "for" loop ? Answered

Hi!

I am new  to arduino. I am struggling with basic problem i guess. I need to rotate 2 servo slowly with for loop in Matlab. To rotate one servo from 90 to 120, I am using this code in Matlab:

for i=90:120
fwrite(arduino, i);
pause(0.03);
end

I found zoomkat sample multi servo code but it requires string command   like:
fprintf(arduino,"%c", "700a,");

And of course ( for i=90:120 fprintf(arduino,"%c", "ia,"); pause(0.03); end ) Not working.

I thought that first number of i may represent servo number. For example; 
fwrite(arduino, 1100) -> Servo number 1 to 100 degree
fwrite(arduino, 2050) -> Servo number 2 to 50 degree

Arduino Code:
#include <Servo.h>
Servo servo1, servo2; 

void setup() {
  Serial.begin(9600);
  servo1.attach(6); 
  servo2.attach(7); 
}

void loop() {

  if( Serial.available() )
    {
        int x = Serial.read();
        int servoNum=x/1000;
        int w=x-(servoNum*1000);
        if (servoNum==1){
          servo1.write(w);}
        if (servoNum==2){
          servo2.write(w);}
     }     
}

And when i try fwrite command in matlab 
arduino = serial('COM3','BaudRate',9600);
fopen(arduino);
pause(0.5)
for i=60:90
a=2000+i;
fwrite(arduino, a);  %Servo number 2  from 60 to 90 
pause(0.02)
end

But this one also not working... How can i do that? Please help..






9 Replies

user
mr61 (author)2014-03-15

For those who may have same problem just use num2str method:

fprintf(arduino, '%c', [num2str(position) 'a,']); %run servo a to required position

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2014-03-09

Fwrite is not the correct function. Try Fprintf instead.

Select as Best AnswerUndo Best Answer

user
mr61 (author)steveastrouk2014-03-09

Actually i do not know fwrite specifications completely. I've been using it for one servo only. When i try fprintf (for i=90:120 fprintf(arduino,"%c", "ia,"); pause(0.03); end) with zoomkat multiservo code, Arduino runs servo to opposite direction and irrelevant position. Generally it moves quickly to 30-40 instead of 120.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)mr612014-03-09

Are you actually trying to transmit the PROGRAM to the Arduino over serial like that ??

Select as Best AnswerUndo Best Answer

user
mr61 (author)steveastrouk2014-03-10

You said try fprintf and as far as i know position is sent with string command in fprintf. I'm aware of that fprintf(....'ia,') is not correct syntax. To increase i number step by step, it must be integer but how can i send integer command with fprintf? Can you be more specific? Thanks for your help.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)mr612014-03-10
user

To write two numbers

printf (arduino,'%d %d',i,j)

Select as Best AnswerUndo Best Answer

user

CHECK by connecting two PCs together with their serial ports.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2014-03-09

What is the output of an fwrite ? ASCII representations or binary ? "90" in ascii is "9" and "0", 90 as binary is a "Z", 91 is "[", 92 is "/" etc etc

How is "not working" ? Can you run the arduino using PuTTY or Hyperterminal ? Check that first.

Select as Best AnswerUndo Best Answer