Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 12:06:31 12:06


Login with username, password and session length


Pages: [1]
Print
Author Topic: Matlab- Arduino- serial com  (Read 4041 times)
0 Members and 1 Guest are viewing this topic.
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« on: July 04, 2014, 10:07:21 22:07 »

I want to send 3 numbers separated by commas from the GUI serially through the COM port  to the arduino.
the arduino will later on use these values for example as the delay() time input for each of the 3 LEDs.

i found this code:
MATLAB:
Quote
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:

Quote
int x;

int y;

int z;

void setup() {
pinMode(2,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
}

void loop() {
if (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);  
    }
 }
}
but it is not working at all
can u help me figure out the problem
« Last Edit: July 05, 2014, 05:46:28 17:46 by pouchito » Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #1 on: July 05, 2014, 12:00:57 00:00 »

Code:
xyz = get(hObject,'String');
'xyz' here is a single variable, if the code had been:
Code:
[x y z] = get(hObject,'String');
then 'x', 'y' and 'z' would be different variables. I can't tell anything else without testing the code myself but the problem seems to be the Arduino code:
Code:
x = Serial.parseInt();
y = Serial.parseInt();
z = Serial.parseInt();
is reading the SINGLE 'xyz' variable into 'x' and not reading anything else into 'y' and 'z'. You may want to check it out.

And next time please use code tags and COMMENT your code so that others may get an idea about what it's doing without the need to consult MATLAB documentation!
Logged

Regards...
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #2 on: July 05, 2014, 06:23:08 18:23 »

Thank you hate and sorry for the bad formatting, i edited my original post.

i modified it as u suggested:[x y z] = get(hObject,'String');    instead of xyz = get(hObject,'String');

but nothing changed.

i m not able to read any values.
do u think the problem is in the arduino part.

what i want to do is to send 3 different long bytes variable to the arduino.
can u help me please ?
Logged
TucoRamirez
Senior Member
****
Offline Offline

Posts: 307

Thank You
-Given: 257
-Receive: 115


Tuco ... dead or Alive


« Reply #3 on: July 05, 2014, 06:36:06 18:36 »

have you tested your serial data on any terminal soft in order to verify that are you sending what u expected?
Logged

Whoever double crosses me and leaves me alive... he understands nothing about Tuco.
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #4 on: July 05, 2014, 06:55:42 18:55 »

no i did not, i m sure that information are being transmitted since the arduino is flashing
i will try to test it on a serial terminal, hope i ll know how :S
Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #5 on: July 05, 2014, 08:00:03 20:00 »

i modified it as u suggested:[x y z] = get(hObject,'String');    instead of xyz = get(hObject,'String');
My suggestion was based on a general issue in your code. For this change to help with anything, get() function must be returning 3 values which I have no idea if it does. I even have no idea what get() function performs! Before I can be of more help can you please explain the MATLAB code further? For example what is the purpose of the definition of the function in the first line and where it is used in the code?
Code:
function stepInput_Callback(hObject, eventdata, handles)
Logged

Regards...
pickit2
Moderator
Hero Member
*****
Offline Offline

Posts: 4639

Thank You
-Given: 823
-Receive: 4194


There is no evidence that I muted SoNsIvRi


« Reply #6 on: July 05, 2014, 10:16:00 22:16 »

no i did not, i m sure that information are being transmitted since the arduino is flashing
i will try to test it on a serial terminal, hope i ll know how :S
Did you use the built in serial coms in Arduino Ctrl Shift & M it will show more than just flashing a led.
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #7 on: July 06, 2014, 09:27:35 09:27 »

Did you use the built in serial coms in Arduino Ctrl Shift & M it will show more than just flashing a led.
Unfortunately that can only work if he has 2 serial ports, one for MATLAB communication and one for IDE communication.
Logged

Regards...
TucoRamirez
Senior Member
****
Offline Offline

Posts: 307

Thank You
-Given: 257
-Receive: 115


Tuco ... dead or Alive


« Reply #8 on: July 06, 2014, 11:25:06 11:25 »

except if you create a bridge on Virtual ports ...
Logged

Whoever double crosses me and leaves me alive... he understands nothing about Tuco.
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #9 on: July 06, 2014, 12:01:38 12:01 »

I tried the arduino part and the serial monitor
It s working properly
But when i m sending from matlab it s not working
So i guess my problem is with matlab, right ?

Hate, concerning the function i used:
function stepInput_Callback(hObject, eventdata, handles)
it s simply what i got when i insert a text box in the gui interface of matlab, it s nthg imp.
« Last Edit: July 06, 2014, 12:05:08 12:05 by pouchito » Logged
pablo2048
Active Member
***
Offline Offline

Posts: 113

Thank You
-Given: 133
-Receive: 86


« Reply #10 on: July 06, 2014, 12:59:18 12:59 »

I think that there can be problem in Your Arduino sketch - main loop can be executed too fast to catch all characters of an integer for .parseint() method - try check if there is valid '\n' character (if Serial.read() == '\n' is true...)
Logged
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #11 on: July 06, 2014, 01:17:34 13:17 »

I inserted a
Quote
"}"
after the:
Quote
z = Serial.parseInt();

instead of the end

and i changed the :
Quote
if (Serial.read() =='\n')
to
Quote
if (Serial.read() <='\n')


now the leds are turning ON whenever i press the "enter" key regardless of the values

where am i mistaken ?
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 113

Thank You
-Given: 133
-Receive: 86


« Reply #12 on: July 06, 2014, 01:46:49 13:46 »

Ok, simple solution - get characters from serial into an array (use stream class) and after receiving "\n" just use .parseInt ...
Logged
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #13 on: July 06, 2014, 02:30:27 14:30 »

not working :S
can you modify it maybe i m doing sthg wrong :S
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 113

Thank You
-Given: 133
-Receive: 86


« Reply #14 on: July 06, 2014, 02:45:13 14:45 »

Ok - one maybe weird idea - are You sure that Your Matlab code sends "\n" correctly (i'm not familiar with matlab)? Can You please check this? It seems that Arduino serial.parseint method wait internally for the next character...
Logged
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #15 on: July 06, 2014, 06:04:28 18:04 »

Do u think the problem is in the matlab part ?
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 113

Thank You
-Given: 133
-Receive: 86


« Reply #16 on: July 06, 2014, 07:31:23 19:31 »

I'm not sure, but now i suspect line-ending output from matlab rather than Arduino part...
Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #17 on: July 06, 2014, 09:22:21 21:22 »

except if you create a bridge on Virtual ports ...
Can you please elaborate this? I always thought debugging an Arduino with the serial port already connected to some application was a real pain. I believe the method you mentioned is meant for these situations. Wink

@pouchito:
Can you please answer the following questions with appropriate code lines.

1- In which variables are your 3 delay values stored?
-> insert code line(s) here!
Can you see those values in the corresponding variables on MATLAB debugger which is usually at the top right of the main window named 'workspace'?

2- Which com port does the code initialize and where in the code?
-> insert code line(s) here!

3- How are these values are sent to serial port and where in the code?
-> insert code line(s) here!

4- Can you receive anything at the Arduino side, did you test it with the built-in serial monitor?

5- If the answer is yes to the previous question, did you try sending something like:
23
85
15
and did you get the expected behavior on the Arduino part?
Logged

Regards...
pouchito
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 61
-Receive: 2


« Reply #18 on: July 06, 2014, 10:41:03 22:41 »

Thank you hate for your time.
i found the code on the net and it is not me who wrote it.

i will try to answer ur questions:
Quote
1- In which variables are your 3 delay values stored?
-> insert code line(s) here!
Can you see those values in the corresponding variables on MATLAB debugger which is usually at the top right of the main window named 'workspace'?
in xyz :
Quote
xyz = get(hObject,'String');
no, i cannot see it in the corresponding variables debugger but in the command window i m receiving like a summary and i found this:
Quote
Read/Write State 
      TransferStatus:     idle
      BytesAvailable:     227
      ValuesReceived:     0
      ValuesSent:         3



Quote
2- Which com port does the code initialize and where in the code?
i m working on com5
Quote
s = serial('COM5','BaudRate',9600);

Quote
3- How are these values are sent to serial port and where in the code?
fprintf(s,'%f',char(xyz));
i think here i have a problem

Quote
4- Can you receive anything at the Arduino side, did you test it with the built-in serial monitor?
i tried the arduino code with the serial monitor (no matlab) and it is working properly. i sent (1000,1000,1000) and i receive (1000,1000,1000)
but when i use the matlab to send the info serially, i can see that the COM port opens since the arduino flashes but i receive no output.



Posted on: July 06, 2014, 11:02:21 23:02 - Automerged

i ve been working on this project nights and now one line (pause) simply make it work yeyyyyyy Smiley
thank you all for ur help
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC