Go Down

Topic: Setting variables with Matlab (Read 645 times) previous topic - next topic

kakaovnik

Mar 26, 2014, 03:07 pm Last Edit: Mar 26, 2014, 03:24 pm by kakaovnik Reason: 1
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: [Select]
 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: [Select]
data=['255,100,0,100,175,300'];
global s;
s=serial('COM4','BaudRate',9600);
fopen(s);
fprintf(s,data);
fclose(s);

PaulS

Code: [Select]
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?

kakaovnik

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


PaulS

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.

kakaovnik

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.

PaulS

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.

mart256

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

Go Up