Continously reading input from arduino pin

5 次查看(过去 30 天)
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