Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 06, 2016, 08:51:46 20:51


Login with username, password and session length


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

Posts: 29

Thank You
-Given: 55
-Receive: 2


« on: July 04, 2014, 11:07:21 23: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, 06:46:28 18:46 by pouchito » Logged
hate
Hero Member
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #1 on: July 05, 2014, 01:00:57 01: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: 55
-Receive: 2


« Reply #2 on: July 05, 2014, 07:23:08 19: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: 277

Thank You
-Given: 197
-Receive: 95


Tuco ... dead or Alive


« Reply #3 on: July 05, 2014, 07:36:06 19: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: 55
-Receive: 2


« Reply #4 on: July 05, 2014, 07:55:42 19: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
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #5 on: July 05, 2014, 09:00:03 21: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: 3816

Thank You
-Given: 567
-Receive: 2049


There is no evidence that I muted SoNsIvRi


« Reply #6 on: July 05, 2014, 11:16:00 23: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: If you have no posts other than, I want or reporting a dead link Then you can't complain If I remove your post So Stop Leeching
hate
Hero Member
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #7 on: July 06, 2014, 10:27:35 10: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: 277

Thank You
-Given: 197
-Receive: 95


Tuco ... dead or Alive


« Reply #8 on: July 06, 2014, 12:25:06 12: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: 55
-Receive: 2


« Reply #9 on: July 06, 2014, 01:01:38 13: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, 01:05:08 13:05 by pouchito » Logged
pablo2048
Active Member
***
Offline Offline

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #10 on: July 06, 2014, 01:59:18 13: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: 55
-Receive: 2


« Reply #11 on: July 06, 2014, 02:17:34 14: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: 103

Thank You
-Given: 96
-Receive: 82


« Reply #12 on: July 06, 2014, 02:46:49 14: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: 55
-Receive: 2


« Reply #13 on: July 06, 2014, 03:30:27 15:30 »

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

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #14 on: July 06, 2014, 03:45:13 15: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: 55
-Receive: 2


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

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

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #16 on: July 06, 2014, 08:31:23 20:31 »

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

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #17 on: July 06, 2014, 10:22:21 22: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: 55
-Receive: 2


« Reply #18 on: July 06, 2014, 11:41:03 23: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