Continously reading input from arduino pin

7 visualizaciones (últimos 30 días)
Muhammad Haziq
Muhammad Haziq el 9 de En. de 2019
Respondida: Bálint Décsi el 28 de Abr. de 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

Respuestas (1)

Bálint Décsi
Bálint Décsi el 28 de Abr. de 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.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by