Arduino and servo motor

조회 수: 4 (최근 30일)
Dekel Mashiach
Dekel Mashiach 2022년 5월 26일
답변: arushi 2024년 9월 3일
Hey;
I have this code to active servo motor, I want regulate the of the motor with binary values between 0-255. someone can help me please?
clear a;clear s;
a = arduino('COM10', 'Mega2560', 'Libraries', 'Servo');
s = servo(a, 'D4', 'MinPulseDuration', 700*10^-6, 'MaxPulseDuration', 2300*10^-6)
while(1)
writePosition(s);
current_pos = readPosition(s);
current_pos = current_pos*180;
fprintf('Current motor position is %d degrees\n', current_pos);
% pause(2);
end

답변 (1개)

arushi
arushi 2024년 9월 3일
Hi Dekel,
From what I understand you are trying to adjust the position of the servo with input ranging from 0 to 255.
angle = 50 %You can take any value from 0-255
angle = angle/255 %This changes the value to the range of 0-1
writePostition(s,angle);
Now if you want to read the value in range of 0-255 just multiply 255 to current_pos instead of 180
current_pos = readPosition(s);
current_pos = current_pos*255;
For more details refer to the Documentation

카테고리

Help CenterFile Exchange에서 Embedded Coder Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by