필터 지우기
필터 지우기

All pins reserved by servo. I cant use a DC motor at the same time.

조회 수: 7 (최근 30일)
Hi everyone, I have just started to use MATLAB with arduino for a university project.
I'm trying to use both a servo and a DC motor with an arduino, but when I initialise the servo motor, all the pins (or at least the PWM ones) become reserved to the servo.
a = arduino('COM4', 'Uno', 'Libraries', 'Servo')
s = servo(a, 'D11', 'MinPulseDuration', 0.00055, 'MaxPulseDuration', 0.0024)
writePWMDutyCycle(a, "D9", 1)
The error I get is:
"Digital pin D9 is reserved by Servo in 'Reserved' mode. To change the pin configuration, clear all variables holding onto this resource."
I have tried other pins, but they are all reserved. If i don't initialise the servo, it works.
How can I reserve just one pin for the servo and not all of them?
Thanks

채택된 답변

Madhu Govindarajan
Madhu Govindarajan 2018년 11월 13일
Can you try configurepin command to set it as Digital Output or Digital Input depending on your application?
Or does that also error out?
  댓글 수: 4
Brian O'Connell
Brian O'Connell 2021년 4월 16일
Why is this an accepted answer when it doesn't answer the question?
I have students with this same question. It's not a consistent issue, only showing up for some students. I have not noticed a trend in operating system or version yet. Has there been any work to solve or address this issue?
Eric Davishahl
Eric Davishahl 2021년 10월 28일
Bumping Brian's question. I also have a few students (not all) experienceing this issue and the configurePin approach does not resolve it.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Madhu Govindarajan
Madhu Govindarajan 2018년 11월 13일
I only have an MKR1000 at my disposal right now and here is what I tried with no error messages. See if a similar approach works for you on Uno.
a = arduino
configurePin(a,'D9','Servo')
configurePin(a,'D10','DigitalOutput')
s = servo(a, 'D9', 'MinPulseDuration', 0.00055, 'MaxPulseDuration', 0.0024)
writePWMDutyCycle(a, 'D10', 1)
This did not give me any errors, but I do not have actual devices connected. So I cannot confirm that the hardware is doing what it is supposed to. But there are no reasons why it should not.
HTH
  댓글 수: 1
Levi Montgomery
Levi Montgomery 2020년 11월 8일
Doesn't work either. I still get the error any time I want to write to a digital pin (not the one I am using for my servo) . Marked your suggestion with %%%%%%%%%%
clear
a = arduino();
% below is adapted from MiniProjectCollectData by Neil Moore
writeDigitalPin(a, 'D9', 1); % on
pause(0.1);
red = readVoltage(a, 'A0');
pause(0.1);
writeDigitalPin(a, 'D9', 0); % off
%
s1 = servo(a, 'D6', 'MinPulseDuration', 700*10^-6, 'MaxPulseDuration', 2300*10^-6);
while 0==0
if red < 0.0195506
material = "mousepad";
writePosition(s1, 1)
else
if red < 0.058651
material = "calculator";
writePosition(s1,.75)
else
material = "folder";
writePosition(s1, .5)
end
end
fprintf("Your material is %s!\n ", material)%%%%%%%%%%%%%%%%%
configurePin(a,'D9','DigitalOutput') %%%%%%%%%%%%%%%%%
writeDigitalPin(a, 'D9', 1); % on
pause(0.1);
red = readVoltage(a, 'A0');
pause(0.1);
writeDigitalPin(a, 'D9', 0); % off
end

댓글을 달려면 로그인하십시오.


Daniel
Daniel 2024년 6월 5일
This issue is caused by the way the Arduino Servo library handles pins. Their documentation states that "use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."
So, the fix for this issue is to not use the pins that the Servo Library disables while using the Servo Library, which worked in this similar situation:
https://www.mathworks.com/matlabcentral/answers/1698815-all-pins-reserved-by-arduino-servo

커뮤니티

더 많은 답변 보기:  Power Electronics Community

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by