I am trying to program two arduinos using matlab and only one is working. I am not totally sure if it is a code issue.

8 views (last 30 days)
I am very new to matlab and am working on my senior thesis (as a non CS major). I am trying to program two arduinos to turn 5 stepper motors each in parallel using matlab and I am only getting motors to turn with one arduino. I need a second pair of eyes on my code to make sure it is not a code issue and is rather an issue with the circuitry.
function [currentAbIndex] = abCode(desiredIndex)
global currentAbIndex;
global currentFlexIndex
global abMatrix;
if currentFlexIndex > 1
flexCode(1)
end
a1 = arduino('COM4', 'Uno');
a2 = arduino('COM3', 'Uno');
pinDirA = "D12";
pinDirB = "D13";
pinDirCPosY = "D8";
pinDirCNegY = "D8";
pinDirCX = "D13";
pinDirD = "D12";
pinDirE = "D4";
pinDirFPosY = "D7";
pinDirFNegY = "D7";
pinDirFZ = "D4";
pinMovA = "D10";
pinMovB = "D11";
pinMovCPosY = "D9";
pinMovCNegY = "D9";
pinMovCX = "D11";
pinMovD = "D10";
pinMovE = "D5";
pinMovFPosY = "D6";
pinMovFNegY = "D6";
pinMovFZ = "D5";
configurePin(a2,pinDirA,'DigitalOutput')
configurePin(a2,pinDirB,'DigitalOutput')
configurePin(a2,pinDirCPosY,'DigitalOutput')
configurePin(a1,pinDirCNegY,'DigitalOutput')
configurePin(a1,pinDirCX,'DigitalOutput')
configurePin(a1,pinDirD,'DigitalOutput')
configurePin(a2,pinDirE,'DigitalOutput')
configurePin(a1,pinDirFZ,'DigitalOutput')
configurePin(a2,pinDirFPosY,'DigitalOutput')
configurePin(a1,pinDirFNegY,'DigitalOutput')
configurePin(a2,pinMovA,'PWM')
configurePin(a2,pinMovB,'PWM')
configurePin(a2,pinMovCPosY,'PWM')
configurePin(a1,pinMovCNegY,'PWM')
configurePin(a1,pinMovCX,'PWM')
configurePin(a1,pinMovD,'PWM')
configurePin(a2,pinMovE,'PWM')
configurePin(a1,pinMovFPosY,'PWM')
configurePin(a2,pinMovFNegY,'PWM')
configurePin(a1,pinMovFZ,'PWM')
directionPins1 = [pinDirFZ, pinDirCNegY, pinDirFNegY, pinDirD, pinDirCX];
directionPins2 = [pinDirA, pinDirB, pinDirCPosY, pinDirFPosY, pinDirE];
movementPins1 = [pinMovFZ, pinMovCNegY, pinMovFNegY, pinMovD, pinMovCX];
movementPins2 = [pinMovA, pinMovB, pinMovCPosY, pinMovFPosY, pinMovE];
i = 1;
if currentAbIndex < desiredIndex
while currentAbIndex < desiredIndex
while i<=5
deltaLength = abs(abMatrix(currentAbIndex,i)-abMatrix(currentAbIndex+1,i));
rotationDuration = deltaLength*116.886;
if abMatrix(currentAbIndex+1,i) > abMatrix(currentAbIndex,i)
writeDigitalPin(a1,directionPins1(i),1)
writeDigitalPin(a2,directionPins2(i),1)
else
writeDigitalPin(a1,directionPins1(i),0)
writeDigitalPin(a2,directionPins2(i),0)
end
writePWMDutyCycle(a1,movementPins1(i),0)
writePWMDutyCycle(a2,movementPins2(i),0)
minDutyCycle = 0.03;
maxDutyCycle = 0.12;
writePWMDutyCycle(a1,movementPins1(i),minDutyCycle)
writePWMDutyCycle(a2,movementPins2(i),minDutyCycle)
startTime = tic;
while toc(startTime) < rotationDuration
% Adjust the duty cycle gradually to simulate rotation
currentDutyCycle = minDutyCycle + (toc(startTime) / rotationDuration)...
* (maxDutyCycle - minDutyCycle);
writePWMDutyCycle(a1, movementPins1(i), currentDutyCycle);
writePWMDutyCycle(a2, movementPins2(i), currentDutyCycle);
% Add a small delay for smoother motion (adjust as needed)
pause(0.01);
end
% Stop the servo motor by setting the duty cycle to 0
writePWMDutyCycle(a1, movementPins1(i), 0);
writePWMDutyCycle(a2, movementPins2(i), 0);
i = i+1;
end
currentAbIndex = currentAbIndex + 1;
i = 1;
end
else
while currentAbIndex > desiredIndex
while i<=5
deltaLength = abs(abMatrix(currentAbIndex,i)-abMatrix(currentAbIndex-1,i));
rotationDuration = deltaLength*116.886;
if abMatrix(currentAbIndex-1,i) > abMatrix(currentAbIndex,i)
writeDigitalPin(a1,directionPins1(i),1)
writeDigitalPin(a2, directionPins2(i),1)
else
writeDigitalPin(a1,directionPins1(i),0)
writeDigitalPin(a2,directionPins2(i),0)
end
writePWMDutyCycle(a1,movementPins1(i),0)
writePWMDutyCycle(a2,movementPins2(i),0)
minDutyCycle = 0.03;
maxDutyCycle = 0.12;
writePWMDutyCycle(a1,movementPins1(i),minDutyCycle)
writePWMDutyCycle(a2,movementPins2(i),minDutyCycle)
startTime = tic;
while toc(startTime) < rotationDuration
% Adjust the duty cycle gradually to simulate rotation
currentDutyCycle = minDutyCycle + (toc(startTime) / rotationDuration)...
* (maxDutyCycle - minDutyCycle);
writePWMDutyCycle(a1, movementPins1(i), currentDutyCycle);
writePWMDutyCycle(a2, movementPins2(i), currentDutyCycle);
% Add a small delay for smoother motion (adjust as needed)
pause(0.01);
end
% Stop the servo motor by setting the duty cycle to 0
writePWMDutyCycle(a1, movementPins1(i), 0);
writePWMDutyCycle(a2, movementPins2(i), 0);
i = i+1;
end
currentAbIndex = currentAbIndex - 1;
i = 1;
end
end
clear a1
clear a2
end
Thank you so much to anyone who looks this over. It really helps a lot.
  1 Comment
Rob Purser
Rob Purser on 6 Mar 2024
I don't see anything obviously wrong, but I can't claim to have looked deeply. I probably would need to see a schematic to understand how you have it wired up. That said, I'd recommend pulling out all the code for one aduino, get that working, and then repeat for the second one. Once you've got them both working independently, merge them together. That'll isolate the two and simplify your code. Also consider using an object or functions to reduce the duplicate code for arduinos 1 and 2.
Also, which Arduino? Are one and two the same model?

Sign in to comment.

Answers (1)

MathWorks MATLAB Hardware Team
Hi,
Rob's suggested steps are a good starting point.
Additionally, you might want to explore the following methods:
1. The MATLAB Support Package for Arduino hardware does not directly support stepper motors. However, if you're using shields, the Adafruit shield API might be of interest:
https://www.mathworks.com/help/supportpkg/arduinoio/ref/arduinoioaddons.adafruit.stepper.html
2. You can also explore "Custom Arduino Libraries" approach for controlling two steppers motors at once. See the following link to know more, https://in.mathworks.com/help/supportpkg/arduinoio/custom-arduino-libraries.html
Please feel free to reach out to MathWorks Technical Support if you need any further assistance,
https://www.mathworks.com/support/contact_us.html
Thanks,
MATLAB Hardware Team
MathWorks

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!