Saving Simulink data in Ardunio Variables

I am new to Arduino programming. I have successfully been able to send one piece of data from Simulink to Arduino (Mega 2560) and store it in a variable called "voltage". Now, I want to send more than one piece of information and store it in a different variable, for instance, "current". Attached is the code to receive and store data from Simulink in a variable "voltage" in Arduino.

#include <SoftwareSerial.h>

//Variables
int character_location;

const int buffer_size=16; //buffer size and buffer used to recieve the
byte buf[buffer_size]; //data from Simulink

float output;
float output_current;

#define RX 10
#define TX 11

int voltage = 1;
int current = 1;

SoftwareSerial esp8mod(RX,TX);

union BytetoFloat //byte to float
{
byte b[16]; //size of 16 bytes
float fval;
} uni; //name of the defined union to be used

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
esp8mod.begin(115200);

}

void loop() {

// send data only when receive data, when in the serial port:
while (Serial.available()==0)
{
}

if (Serial.available()>0) {

voltage = readFromSimulink();
delay(0.001);
writeToMatlab(voltage);

}
}

float readFromSimulink() //gets numbers from data type as a float
{
character_location = Serial.readBytesUntil('\r\n', buf, buffer_size);
for (int i=0; i<buffer_size; i++) //go through each element until you looked through all the bytes in the buffer
//i++ means increment i each time
{
uni.b[i] = buf[i]; //store in b from union the item in buffer located i
}
output = uni.fval; //store in fval which was defined in union uni
return output;
}

void writeToSimulink (float number)
{
byte *uni = (byte *) &number; //the * means the address of the variable
//uni. The & is used to declare a pointer, I want
//to access the number's memory address.
Serial.write(uni,4); //write to ther serial port the union value
}

void writeToMatlab (float number)
{
byte *uni = (byte *) &number;
Serial.write(uni,4);
}

It is very hard to see how that code could be doing anything useful.

Float values take up four bytes on an AVR based Arduino, so this union definition won't work for Arduino.

union BytetoFloat //byte to float
{
byte b[16]; //size of 16 bytes
float fval;
} uni; //name of the defined union to be used

Why is the Arduino involved in this project, and what is it supposed to do?

So we are getting a bunch of values from Simulink. What I need to do is store these values in different variables after which I will be writing code based on these variables to get other parameters. For instance, I am getting voltage and current values from Simulink, I want to store them in variables to be able to calculate Power using them.

Why is the Arduino involved in this project, and what is it supposed to do?

Just a hint: This is how your post would look like if you use the </> tags (from the forum editor menu bar) or type the tags in front and behind your sketch as shown here:

[code]
  // Copy your sketch between these tags
[/code]

Alternatively you may also use the function "Copy for forum" in the Arduino IDE.

It helps others to read and correctly copy your code-

#include <SoftwareSerial.h>

//Variables
int character_location;

const int buffer_size=16; //buffer size and buffer used to recieve the
byte buf[buffer_size]; //data from Simulink

float output;
float output_current;

#define RX 10
#define TX 11

int voltage = 1;
int current = 1;

SoftwareSerial esp8mod(RX,TX);

union BytetoFloat //byte to float
{
byte b[16]; //size of 16 bytes
float fval;
} uni; //name of the defined union to be used

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
esp8mod.begin(115200);

}

void loop() {

// send data only when receive data, when in the serial port:
while (Serial.available()==0)
{
}

if (Serial.available()>0) {

voltage = readFromSimulink();
delay(0.001);
writeToMatlab(voltage);

}
}

float readFromSimulink() //gets numbers from data type as a float
{
character_location = Serial.readBytesUntil('\r\n', buf, buffer_size);
for (int i=0; i<buffer_size; i++) //go through each element until you looked through all the bytes in the buffer
//i++ means increment i each time
{
uni.b[i] = buf[i]; //store in b from union the item in buffer located i
}
output = uni.fval; //store in fval which was defined in union uni
return output;
}

void writeToSimulink (float number)
{
byte *uni = (byte *) &number; //the * means the address of the variable
//uni. The & is used to declare a pointer, I want
//to access the number's memory address.
Serial.write(uni,4); //write to ther serial port the union value
}

void writeToMatlab (float number)
{
byte *uni = (byte *) &number;
Serial.write(uni,4);
}

We are generating values like Voltage and Current in Simulink. We need to transmit these values to Arduino and store in different variable to be able to process them like find Power, Resistance and other parameters. Yes, this can also be done in Simulink and then just transfer the parameters to Arduino but our assignment requires us to recevie the raw values in Arduino then process them in Arduino to be able to send them to an app going forward. We have everything figured out but we are not able to get these raw values from Simulink and store them in varaibles. So, if you have any idea about taking raw values from Simulink and storing them in different variables, it would be really helpful!

Mathworks provides an interface to Arduino for both Matlab and Simulink. That includes methods for transferring data to and from the Arduino, so take a look.

I am using this method to transfer data to and from Arduino but I am not able to store this data in different variables in the Arduino.

If you transfer data to an Arduino, it is by definition stored on the Arduino.

What is the real problem? What do you mean by "different variables"?

I suggest that you spend some time learning how to use the Arduino IDE, try out some of the examples that come with it, and learn the C/C++ programming language.

The data that is transferred to Arduino I want to store them in variables. For example, if I am sending the values 1, 2, and 3 to Arduino I want them to be stored in three separate variables that I define in Arduino, for eg, "voltage" stores the value "1", current stores the value "2", and resistance stores the value "3".

The data are already stored in variables. It will take some additional program code to separate the values.

Since this is serial transfer, you will greatly benefit by studying this Arduino tutorial: Serial Input Basics - updated

I will go through the link, thanks for the help!

I will reply back to your comment if I have any more questions

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.