Continously reading input from arduino pin

7 Ansichten (letzte 30 Tage)
Muhammad Haziq
Muhammad Haziq am 9 Jan. 2019
Beantwortet: Bálint Décsi am 28 Apr. 2019
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

Antworten (1)

Bálint Décsi
Bálint Décsi am 28 Apr. 2019
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.

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by