Pages: [1]   Go Down
Author Topic: Setting variables with Matlab  (Read 501 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 2
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Arduinos!
I am working on my project and got stuck at sending variables from Matlab to Arduino.

What I want to do is to send this value from Matlab: 255,100,0,100,175,300
to fill 6 variables in Arduino so I can setup my PORTD properties. I want to generate signal like this on output:

I understand I can use Serial.parseint to fill my variables, but it is not really working, and since I'm really new to programming I don't know where I made a mistake. Can you PLEASE help me?

Arduino code looks like this:

Code:
 int a=0;
  int b=0;  
  int c=0;
  int d=0;  
  int e=0;
  int f=0;
void setup()
{
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0) // if there is data to read
  {
    a=Serial.parseInt(); // read data
    b=Serial.parseInt();
    c=Serial.parseInt();
    d=Serial.parseInt();
    e=Serial.parseInt();
    f=Serial.parseInt();
    PORTD = a;
    delay = b;
    PORTD = c;
    delay = d;
    PORTD = e;
    delay = f;
  }
}

Matlab code:

Code:
data=['255,100,0,100,175,300'];
global s;
s=serial('COM4','BaudRate',9600);
fopen(s);
fprintf(s,data);
fclose(s);
« Last Edit: March 26, 2014, 09:24:02 am by kakaovnik » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
fopen(s);
fprintf(s,data);
fclose(s);
Open the serial port, resetting the Arduino.
While it's rebooting, jam some data at it.
Close the serial port, resetting the Arduino again.

What was the point in sending it data if you don't wait for it to be ready to receive it?
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for answer!
And how can I do that? What would be the best solution?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And how can I do that?
We've hired a psychic. His start data is the 12th of never.

If you need help before then, you'll need to ask questions that have answers. No clue was provided as to what the "that" is that you want to do.
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am sorry Paul,

how am I supposed to make matlab wait for arduino to process my data?

Should I put it to a while cycle? And how? I am bit confused here.

Thank you.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how am I supposed to make matlab wait for arduino to process my data?
The best matlab answers would come form a matlab forum.
Logged

Offline Offline
Full Member
***
Karma: 6
Posts: 150
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may use matlab Simulink, there's an interface called Serial send and Serial receive. I've tested it with Arduino, works fine.
Logged

Pages: [1]   Go Up
Jump to: