writePosition does not work inside of a function

So i'm trying to make a function that i can input a number from 0 to 1 and the function turns the servo how i specify. Problem is, the servo just never turns. I don't get an error, it acknowledges the arduino and servo connections, but it just refuses to turn the servo. When i do this in a script, it works just fine. I need help, please.
function servocontrol(theta)
a=arduino();
s=servo(a,'D9');
writePosition(s,theta)
end

3 件のコメント

Walter Roberson
Walter Roberson 2018 年 3 月 1 日
I do not recommend creating a connection to arduino each time. I recommend creating it once and storing the connection (a) somewhere that can be accessed. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Yousef Gaffary
Yousef Gaffary 2018 年 3 月 1 日
Thank you for the reply my follow up question is, even if i do create the connection every time, how come the servo still won't rotate? Even if i store the connection, i feel like the result will be the same. Is there a reason why writePosition does not work in a function?
Walter Roberson
Walter Roberson 2018 年 3 月 1 日
I am not aware of any reason why writePosition would not work in a function. I am, however, concerned about the possibility that there might already be a connection open to the arduino and so the arduino() call inside the function might not be able to communicate properly.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

質問済み:

2018 年 3 月 1 日

コメント済み:

2018 年 3 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by