MATLAB Answers

D P
0

Send SERIAL commands from MATLAB GUI to Arduino

Asked by D P
on 16 May 2013
Latest activity Answered by pouchito
on 6 Jul 2014
Hey all,
I am working on making a GUI that takes in Three(3) 'int' values from the user separated by commas (i.e. 1000,1000,1000) and sends that entire string through the COM port to the Arduino which uses those 3 int values as the delay() time input for each of the 3 LEDs.
I have it working to the point where:
1) the user can select the COM port from the GUI that the Arduino is connected to
2) user can enter the 3 'int' values into a "edit text" box separated by commas as mentioned above
3) then the GUI code retrieves that string and saved it to a variable called: xyz
4) using the command fprintf(s,xyz), where "s" is the serial object, the value of xyz is sent to the Arduino without any errors
5) the Arduino "RX" led blinks indicating the value from serial port has been received
-- now the problem is that Arduino is not doing anything with the received serial input from matlab! if I used the Arduino IDE's Serial Monitor to send 1000,1000,1000 it does what its suppose to!
So I am thinking Arduino doesn't like the "data type"/"format" of the value im sending through the matlab GUI and I am not sure how to fix this...!
I am a newbie with programing/arduino/matlab so please excuse any novice mistakes. ANY help will be greatly appreciated!!
Below is the TEST Arduino Code and the MATLAB GUI Code:
Thank you guys in advanced!!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Relevant MATLAB GUI CODE:
function stepInput_Callback(hObject, eventdata, handles)
xyz = get(hObject,'String');
disp('Value entered: ')
disp(xyz)
global COM;
global s;
s = serial(COM,'BaudRate',9600);
fopen(s);
disp('After OPEN')
disp(s)
fprintf(s,xyz);
fclose(s);
disp('After CLOSE')
disp(s)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Arduino Code:
int x;
int y;
int z;
void setup() {
pinMode(2,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available()) // check if Serial data is available to read
{
x = Serial.parseInt();
y = Serial.parseInt();
z = Serial.parseInt();
if (Serial.read() == '\n') // check if ENTER has been pressed (newline)
{
digitalWrite(2,1);
delay(x);
digitalWrite(6,1);
delay(y);
digitalWrite(10,1);
delay(z);
digitalWrite(10,0);
delay(z);
digitalWrite(6,0);
delay(y);
digitalWrite(2,0);
delay(x);
}
}
}

  0 Comments

Sign in to comment.

3 Answers

Answer by David Sanchez
on 16 May 2013
 Accepted Answer

I would delete the while.serial from arduino code and do it as:
void loop()
{
if (Serial.available()>0)
{
my_data = Serial.read();
...
}
] // end loop()
Since you open the serial port every time you want to send anything and close it again, it will take some time to perform the operation. Did you try to add a connect/disconnect button in your GUI to perform that operation so that the data is sent straight away once it is introduced by the user?

  1 Comment

Hey David, changing the "while loop" to "if statement" worked! Now the Arduino receives the user input (i.e 1000,1000,1000) through the GUI and executes the code on the Arduino! Thanks a lot for your help, I greatly appreciate it!
DP

Sign in to comment.


Answer by David Sanchez
on 16 May 2013

Hello, the problem is not what Arduino sends to your PC serial port, but how you try to read it. What Matlab function are you using to read from the serial port? Did you try fscanf to read from the serial port? That function (fscanf) should be able to read what Arduino sends to the serial port.

  2 Comments

I don't see any Serial.write() in your Arduino code either. Are you sending anything to the serial port from Arduino? How?
Hey David, thanks for your reply! I am not trying to send anything from Arduino to PC, I am trying to get MATLAB GUI to send the "user input" TO Arduino through serial communication. I hope that makes sense?
- DP

Sign in to comment.


Answer by pouchito
on 6 Jul 2014

i modify it as suggested to void loop() { if (Serial.available()>0) { my_data = Serial.read(); ... } ] // end loop()
but now whenever i hit the "enter" key, the LEDs turn on regardless the values
pls help

  0 Comments

Sign in to comment.