No appropriate method, property, or field 'pinMode' for class 'arduino'.

Hello,
I make a project with arduino and matlab. I have connected everything, now I try to write arduino code in matlab. if I print on run, this following announcement always comes out:
No appropriate method, property, or field 'pinMode' for class 'arduino'.
can somebody help please me?
thanks
thus matlab code looks:
a = arduino('COM12', 'uno');
clkPin=2;
dtPin=3;
swPin=4;
a.pinMode(2,'input')
a.pinMode(3,'input');
a.pinMode(4,'input');
a.digitalWrite(4,1);
oldA = 1;
oldB = 1;
result = 0;
encoderVal = 0;
newA = a.digitalRead(clkPin);
newB = a.digitalRead(dtPin);
if (newA ~= oldA) | (newB ~= oldB)
if (oldA == 1 & newA == 0)
result = (oldB * 2 - 1);
end
end
oldA = newA;
oldB = newB;
change = result;
encoderVal = encoderVal + change;
if digitalRead(swPin) == 0
encoderVal = 0;
end
Serial.println(encoderVal);
i use matlab R2016a

2 Comments

i have the same problem i am trying to communicate arduino ith matlab gui and the error appears in the last line
delete(instrfind({'Port'},{'COM5'}))
clear a;
global a;
a=arduino('COM5');
a.pinMode(8,'output');
homam mohamad alramadan: which MATLAB release are you using?

Sign in to comment.

Answers (1)

pinmode is the older "legacy" interface. It has been replaced. See https://www.mathworks.com/help/supportpkg/arduinoio/ref/configurepin.html and readDigitalPin and readVoltage and writeDigitalPin

9 Comments

Hello Walter Roberson, thanks for your help. now everything is OK. But I have the new following error message:
Undefined variable "Serial" or class "Serial. println".
Error in Lol (line 40)
Serial. println (encoderVal);
Thus my new matlab code looks:
clear all; close all; clc
a = arduino('COM12', 'uno');
clkPin=2;
dtPin=3;
swPin=4;
configurePin(a,'D2')
configurePin(a,'D3');
configurePin(a,'D4');
writeDigitalPin(a,'D4',1);
oldA = 1;
oldB = 1;
result = 0;
encoderVal = 0;
newA = readDigitalPin(a,'D2');
newB = readDigitalPin(a,'D3');
if (newA ~= oldA) | (newB ~= oldB)
if (oldA == 1 & newA == 0)
result = (oldB * 2 - 1);
end
end
oldA = newA;
oldB = newB;
change = result;
encoderVal = encoderVal + change;
if configurePin(a,'D4') == 0
encoderVal = 0;
end
Serial.println(encoderVal);
The Serial class is for C++ on the Arduino side. On the MATLAB side, if you want to display to the screen, you can disp() the value. If you want to send to the Arduino as text you would need to fprintf(a, '%d\n', encoderVal) or similar.
Hello Walter Roberson,
I thank you once more.
I have one more question. I have written with arduino code thus:
static int oldA = HIGH;
static int oldB = HIGH;
Now I want to circumscribe in Matlab code.
Can you help me please over again?
Thanks. . .
persistent oldA oldB
oldA = int32(HIGH);
oldB = int32(HIGH);
Hello Walter Roberson,
this is the complete arduino code:
const int clkPin= 2;
const int dtPin= 3;
const int swPin= 4 ;
int encoderVal = 0;
void setup()
{
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600);
}
void loop()
{
int change = getEncoderTurn();
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)
{
encoderVal = 0;
}
Serial.println(encoderVal);
}
int getEncoderTurn(void)
{
static int oldA = HIGH;
static int oldB = HIGH;
int result = 0;
int newA = digitalRead(clkPin);
int newB = digitalRead(dtPin);
if (newA != oldA || newB != oldB)
{
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
This arduino code is for encoder. if I turn encoder, it counts pay high and if I turn in other direction, it counts pay under it. now I would like to circumscribe this arduino code in Matlab code and this is what I have written:
clear all; close all; clc
a = arduino('COM12', 'uno');
configurePin(a,'D2','DigitalInput');
configurePin(a,'D3','DigitalInput');
configurePin(a,'D4','Unset');
writeDigitalPin(a,'D4',1);
encoderVal = 0;
while 1
oldA = 1;
oldB = 1;
result = 0;
newA = readDigitalPin(a,'D2');
newB = readDigitalPin(a,'D3');
if (newA ~= oldA) || (newB ~= oldB)
if (oldA == 1 && newA == 0)
result = (oldB * 2 - 1);
end
end
oldA = newA;
oldB = newB;
change = result;
encoderVal = encoderVal + change
end
but it does not work and I do not know what I make wrong or how I should continue. Please, can you help me?
I thank you for her understanding.
Thanks
Move
oldA = 1;
oldB = 1;
to before the while loop.
Hello Walter Roberson, thank you one more time for your reponse. I have made as you have said me, but it does not function so properly. when i turn to encode the opposite sense of the needle of a watch to have numbers negative, i gets numbers rather positive. when I write Befehl a = arduino (' COM12 ', ' Uno '); to assure me that there is a communication between Matlab and the board Arduino, i get the following text: Failed to open serial harbour COM12 to communicate with board Uno. Make sure there simple percentage no other MATLAB ARDUINO OBJECT FOR this board. For troubleshooting, see Arduino Hardware Troubleshooting. I do not know if it is because of it that I do not get numbers negative to encode it. Please are you possible to help me. thank you
Try
delete(instrfind())
to get rid of any lingering connections.
Hello Walter Roberson,
when I run the programme by putting delete (instrfind ()) at this level:
clear all; close all; clc
a = arduino('COM12', 'uno');
delete(instrfind())
configurePin(a,'D2','DigitalInput');
configurePin(a,'D3','DigitalInput');
configurePin(a,'D4','Unset');
writeDigitalPin(a,'D4',1);
encoderVal = 0;
oldA = 1;
oldB = 1;
while 1
result = 0;
newA = readDigitalPin(a,'D2');
newB = readDigitalPin(a,'D3');
if (newA ~= oldA) || (newB ~= oldB)
if (oldA == 1 && newA == 0)
result = (oldB * 2 - 1);
end
end
oldA = newA;
oldB = newB;
change = result;
encoderVal = encoderVal + change
end
I get the text suivant:
Error using to Encode (line 4)
Instrument object OBJ simple percentage year invalid object.
but when I put delete (instrfind ()) at the end of the programme:
clear all; close all; clc
a = arduino('COM12', 'uno');
configurePin(a,'D2','DigitalInput');
configurePin(a,'D3','DigitalInput');
configurePin(a,'D4','Unset');
writeDigitalPin(a,'D4',1);
encoderVal = 0;
oldA = 1;
oldB = 1;
while 1
result = 0;
newA = readDigitalPin(a,'D2');
newB = readDigitalPin(a,'D3');
if (newA ~= oldA) || (newB ~= oldB)
if (oldA == 1 && newA == 0)
result = (oldB * 2 - 1);
end
end
oldA = newA;
oldB = newB;
change = result;
encoderVal = encoderVal + change
end
delete(instrfind())
I still do not get numbers negative. I always get numbers positive, all the same which direction of encoder I turn .
where exactly in the programme I must delete(instrfind()) write?
good day to you and thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!