To keep things short & sweet...
- Motive: I want to control the rotational speed of my stepper motor using MATLAB. Why? Cause there's also some stuff I want to plot while it runs in MATLAB.
- Hardware: stepper motor + driver + Arduino
- Problem: MATLAB has only a few controls for Arduino PWM: writePWMVoltage, writePWMDutyCycle
- Solution Needed: I need a way to control the shape of the square wave to control the stepper speed--i.e., frequency, duty cycle, and even peak would be great
The wave needs to bounce between high (5V) or low (0V) with a 50% duty cycle, but user-control over the frequency (e.g., freq = 1/pulseDelay).
So what are some clever ways to do this?
Approach 1: One is to set up a while loop in which you manually send a pulse every pulseDelay seconds. However, you reach a limit in communication speed between MATLAB and the Arduino (e.g., no speed change between pulseDelay = 1E-3 and pulseDelay = 1E-6). It also moves way too slow.
delay = 1E-6;
Approach 2: Create a square wave function and try to get writePWMVoltage to write it. This doesn't work, as this function only takes scalar double between 0 and 5.
time = 0:0.1:60;
sqrWave = 2.5*(square(time)+1);
for count = 1:length(time)
Approach 3: The only thing that works smoothly: writePWMDutyCycle, but I have no control over speed which is what I really want. OR at least to know what speed I'm going at while this line of code executes...
writePWMDutyCycle(a, 'D10', 0.5);
Suggestions are very welcome! I'd love to use MATLAB for this task, but if the support, functions, documentation, etc. are not there for this project that's fine--I'll switch back to Arduino.