Unable to send data (1) from MATLAB to Arduino - matlab

I am sending 1 from MATLAB to Arduino. My Arduino code works perfectly when I send 1 from teraterm terminal software. But when I send 1 from MATLAB by using this code arduino TX lite blinks but cannot get the 1 output.
s = serial('COM7','BaudRate',9600);
fopen(s)
fprintf(s,'1');
fclose(s)

Problem with this code is that you are not giving Delay to process the code.
According to my calculation : fopen(string) command needs 0.8754 Seconds of Processing time.
So add 1 second delay, it will work.
..
Your Modified Code :
clear all
clc
s=serial('COM7','BaudRate',9600);
fopen(s);
pause(1);
fprintf(s,1);
fclose(s);

Related

Matlab code runs in the command window but not in the script file

I have a very simple code in MATLAB for a laser device which I need to use.
Here's the code:
% creating a serial port object
s = serial('COM3');
% opening the port
fopen(s);
% enabling the port
fprintf(s, 'e');
% sending the power to the laser
fprintf(s, 'a738.8889');
% disabling the port
fprintf(s, 'd');
fprintf(s, 'z');
fclose(s);
delete(s);
The code works perfectly fine when I write them one by one from the command window, but it's not working when I put the whole code in either a script file or a function file.
I don't get any errors or any other messages from MATLAB when I run the script or the function. Any ideas why?
Thank you.
So I tried delaying the commands and the script works now. Apparently when the commands are being executed in the script, it is too fast for the device to read them all and that is why it wasn't working.

Serial Comunnication between matlab and arduino

I am trying to send data from MATLAB to ARDUINO using the following code for arduino and the second one for the matlab. Both code work fine and when i press 1 led lights up and when press 2 led become off. But actually what i am trying to do is when matlab run code it automatically send 1 to arduino and the led become on. i have tried may changes but cant able to do. when iam trying to run the third code (given below) the arduino status led blink that show it receive some thing but my actual led which is connected to pin 13 still off.
int ledPin=13;
int matlabData;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin,LOW); // turn light off
}
}
(MATLAB)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.end
13.
14.fclose(arduino); % end communication with arduino
(MY edit code)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.%while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer='1'%('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.%end
13.
14.fclose(arduino); % end communication with arduino
The difference is the following:
answer = input('bla')
yields an answer that is a number, i.e. answer is of type double. In your third case, you wrote answer='1' which is a char, so in fact, the variable answer is different in both cases. Try and change the code in the third part to this here:
answer = 1;
or change the fprintf command to
fprintf(arduino, '%s', str2num(answer));

analogRead function not working in MATLAB

I am new to Arduino and I am trying to control it through MATLAB. I have downloaded the arduino IO package and installed it.I am using MATLAB 2014a.I have a LED and I change its brightness by changing the PWM voltage as follows :
a=arduino('COM3');
brightness_step = (5-0)/20;
for i = 1:20
writePWMVoltage(a, 11, i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
for i = 1:20
writePWMVoltage(a, 11, 5-i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
clear a
Everything works great except for the analogRead part which throws the following error :
No appropriate method, property, or field analogRead for class arduino.
What am I doing wrong?
Maybe you can try to use fscanf() like in the following example from AllAboutEE.
I had the same problem with the analogRead when I connected my ArduinoUno R3.
After an hour's struggle, I changed to readVoltage, as Controller replied in 2014, and it works perfectly.

Compile Matlab code for Arduino

I understand there is a way to compile and upload MATLAB Simulink code to an Arduino so you don't have to have the Arduino connected to your computer. Is there a way to compile and upload a MATLAB script file of code that I have written myself to the Arduino so it can work independently from being plugged in. i.e. could I upload something like:
a = arduino('COM3')
a.pinMode(13,'output');
i = 0
while i>0
a.digitalWrite(13,1)
pause(1)
a.digitalWrite(13,0)
pause(1)
end
how would I do this and does it still require code lines like:
a = arduino
and putting the a. in front of the lines of code

Triggering Matlab with DAQ toolbox MCC card

I need to collect data using Matlab and a MCC USB-2527 card.
Everything works except triggering. Here is my code:
BoardNR=1;
Chan=[0 1 2 3];
fs = 100
duration=60
ai = analoginput('mcc',BoardNR);
ch = addchannel(ai,Chan);
set(ai,'SampleRate',fs);
set(ai,'SamplesPerTrigger',duration*fs);
set(ai,'TriggerChannel',ch(3)) %Channel 3 is the position signal of a cyclic movement
set(ai,'TriggerType','Software')
set(ai,'TriggerCondition','Leaving') %Should trigger when the position signal is not 0
set(ai,'TriggerConditionValue',[0,0])
set(ai,'TriggerDelay',1);
set(ai,'Timeout',5);
%The data collection is started
start(ai);
wait(ai, duration+1)
data=getdata(ai,fs*duration);
plot(0.01:(1/fs):duration,data); %Plotting real time vs signals
If I delete the parts with the triggering, I collect data perfectly. I need to trigger however, and when I run this code I get the error: "WAIT reached its timeout before OBJ stopped running" . I think this means that it stopped waiting for the triggering signal. I am 100 % positive that the position signal will be 0 every 20 seconds or so, so it should def. trigger.
I hope you can help. Ive tried everything, including having another TriggerCondition (like Entering, Rising, Falling) and another TriggerConditionValue.
Thanks in advance,
Henrik
I'm not sure, but I reckon, you have to set the TriggerType to Manual. Which options are possible the command 'propinfo(ai.TriggerType)' should show.
I also use USB 2527 with MatLab ;-)