Continously reading input from arduino pin

조회 수: 7 (최근 30일)
Muhammad Haziq
Muhammad Haziq 2019년 1월 9일
답변: Bálint Décsi 2019년 4월 28일
Hi,
I am using matlab 2018b. I have a problem, actually I want to read input from pin 02 of arduino and when ever the D2 pin =0 it should print a=0 otherwise it should print a=1. I write this code but it only run for one time and then it gives error.
Undefined function 'readDigitalPin' for input arguments of type 'double'.
Error:
Error in servo_example (line 17)
status = readDigitalPin(a,'D2');
status = readDigitalPin(a,'D2');
Code:
a = arduino('COM5', 'Uno', 'Libraries', 'Servo');
configurePin(a,'D2','DigitalInput');
while (1)
status = readDigitalPin(a,'D2');
if (status ==0)
a=0
pause(3);
else
a=1
end
end

답변 (1개)

Bálint Décsi
Bálint Décsi 2019년 4월 28일
Hi,
I guess your case is solved or not relevant anymore, but anyway I leave an answer here.
The problem with your code is that you gave the same name to your arduino instance that you gave to the variable to print (a). So during the first iteration of the while cycle, your arduino instance a is overwritten, hence won't exist anymore and you won't be able to call its methods. I suggest to change the name of the arduino, e.g.:
ard = arduino('COM5', 'Uno', 'Libraries', 'Servo');
and use this name in later funcions.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by