stepper motor controling using matlab GUI, speed very slow

hi everyone, i got a problem by stepper motor controlling using matlab GUI--the motor rotates slowly compared to using same code on arduino IDE.
there is the arduino code i used

#define stepPin 5
#define dirPin 6

int pauseje2Schritte = 400;  // pause between 2 steps
int zahlderSchritte = 1000;  // how many steps the motor rotates
  
void setup() { 
  Serial.begin(9600); 
  Serial.println("Starting stepper exerciser."); 

  pinMode(stepPin, OUTPUT); 
  pinMode(dirPin, OUTPUT); 

  digitalWrite(dirPin, HIGH); 
  digitalWrite(stepPin, LOW); 
  

} 

    void loop() { 
 
      digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction 
      // Makes 200 pulses for making one full cycle rotation 
      for(int x = 0; x < zahlderSchritte; x++) { 
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(pauseje2Schritte); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(pauseje2Schritte); 
      } 
      delay(1000); // One second delay 
      
      digitalWrite(dirPin,LOW); //Changes the rotations direction 
      // Makes 400 pulses for making two full cycle rotation 
      for(int x = 0; x < zahlderSchritte; x++) { 
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(pauseje2Schritte); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(pauseje2Schritte); 
      } 
      delay(1000); 
      
      digitalWrite(stepPin,LOW);
      delay(3000);
      
    }

there is a part of matlab GUI code

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
global pauseje2Schritte; 
writeDigitalPin(a,'D6',1);
% pauseje2Schritte = 0.0000001;
zahlderSchritte= 300;

for x = 1:zahlderSchritte
    writeDigitalPin(a,'D5',1);
        
    pause(pauseje2Schritte);
        
    writeDigitalPin(a,'D5',0);
        
    pause(pauseje2Schritte);
       
end

I read a lot and the reason may that the communication between matlabGUI and arduino board needs much more time. But i don't find any solution. Does anyone have an idea how to improve the speed?? I really appreciate

I am not familiar with Matlab but I can say with certainty that it is completely impractical to send individual step instructions from a PC program to an Arduino. The USB system is very slow for small amounts of data and also it has unpredictable latency.

My suggestion would be to send the Arduino a message telling it how many steps to take and the interval between steps and then let it get on with it - for example <400, 20> for 400 steps and 20 millisecs between steps. That is how I control the motors on a small lathe.

...R
Stepper Motor Basics
Simple Stepper Code

Matlab IS very slow - it's interpreted - but you can buy compilers which speed
it up loads once it's debugged

regards

Allan

The speed of Matlab is not the problem. It is the slow speed and uncertain timing of communication with the Arduino that needs to be dealt with.

...R

Thanks for reply.

According to the suggestion, i should write a code in arduino IDE and uoload it to arduino board, in this code there should be several parameters which i can vary with matlab GUI code. But here is the problem: In oder to connect the arduino board to the Matlab GUI, i need upload .PDE code to arduino board.
I still tried to connect GUI and arduino board without .PDE code, but i failed.

so i think this method may also not work. What do u think?

Am I correct to think you are assuming that the only way to communicate between Matlab and an Arduino is with a specific Arduino program and you don't see how another Arduino program could then be used to control the motor?

AFAIK any Arduino program can include code to take data from a Matlab program. You need to write a program that can control the motor and which can also communicate with Matlab.

As I said earlier, I don't know Matlab so I can't be more specific.

...R

Robin2:
Am I correct to think you are assuming that the only way to communicate between Matlab and an Arduino is with a specific Arduino program and you don't see how another Arduino program could then be used to control the motor?

AFAIK any Arduino program can include code to take data from a Matlab program. You need to write a program that can control the motor and which can also communicate with Matlab.

As I said earlier, I don't know Matlab so I can't be more specific.

...R

Thanks for reply.
I tested the code on a brand new PC which runs much faster and the stepper motor rotated much faster too. The problem was fixed. So happy:)
And i continue my project now....

jameyao:
I tested the code on a brand new PC which runs much faster

I'm curious. What was the clock speed of the old PC and what is the clock speed of the new PC?

1.6GHz PCs have been around for a long time and you would need to go to 3.2GHz to get double the performance. I assumed that when you said "speed very slow" that it would have needed a lot more than a doubling of the speed.

...R

Robin2:
I'm curious. What was the clock speed of the old PC and what is the clock speed of the new PC?

1.6GHz PCs have been around for a long time and you would need to go to 3.2GHz to get double the performance. I assumed that when you said "speed very slow" that it would have needed a lot more than a doubling of the speed.

...R

Actually i told my supervisor my problem and he thought that the PC runs slowly would lead to the motor rotate slowly. So he bought several new PCs which have 3.40GHz 3.41GHz processor.
Then i tested the same code on two PCs, the old one and the new one. The fact is that the motor runs on new PC >10 times faster. The speed is almost same as the motor controlled by arduino IDE.

So if anyone does same thing as i do and has similar problem, my advice is that try it again on a NEW PC!!

jameyao:
So if anyone does same thing as i do and has similar problem, my advice is that try it again on a NEW PC!!

You still have not told us the specification of the PC that was too slow. My suspicion is that the Operating System was clogged up and if that had been sorted it would have been perfectly fast enough.

In time the new PC may slow down, and then what will you do?

...R

Yeah true...... matlab has those compilers that gets your script into a machine code form.... which can speed things up, instead of doing the line-by-line script interpretation, which can slow things down a lot.

Southpark:
Yeah true...... matlab has those compilers that gets your script into a machine code form.... which can speed things up, instead of doing the line-by-line script interpretation, which can slow things down a lot.

What has that got to do with NEW vs OLD computers?

It would be funny if the new PCs "accidentally" default to compiling whereas the old ones were using interpreted mode and all that expense could have been avoided by changing the mode.

...R