Hello, I am trying to send data from Simulink to Arduino Mega, in simulink I am using the "To instrument" block, I have configured the standard parameters: 9600, 8, None, 1. and I have tested it with virtual serial Monitor, the data is they are sending correctly in '% 3.2f' format; in Arduino I made a program that receives each of these data and orders them to form the quantity, for the moment I am using the data to vary the intensity of a led, I have also tried to send the data from the serial monitor of Arduino and it works correctly as well; I have even copied the data taken from the virtual serial Monitor and I have pasted it into the serial arduino monitor and it works correctly. but when sending the data directly from simulink to arduino, I can see the RX LED flashing but Serial Event or Serial.available do not respond. I attach my Arduino code, the Simulink file and screenshots so they can see what I'm talking about. Thank you very much, I hope you can help me.
const int LED = 11;
char ch;
int punto = 0;
int in;
float out;
int br;
int count = 0;
int countp = 0;
int x1 = 0;
int x2 = 0;
int x3 = 0;
float x1p = 0;
float x2p = 0;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while (Serial.available() > 0) {
ch = Serial.read();
digitalWrite(LED, HIGH);
if ((ch >= 48) && (punto == 0)) {
// Serial.println("holi");
count++;
in = ch - 48;
switch (count) {
case 1:
x1 = in;
// Serial.print("x1 = ");
// Serial.println(x1);
break;
case 2:
x2 = in;
// Serial.print("x2 = ");
// Serial.println(x2);
break;
case 3:
x3 = in;
// Serial.print("x3= ");
// Serial.println(x3);
break;
}
}
else if (ch == 46) {
// Serial.println("holip");
punto = 1;
}
else if ((ch >= 48) && (punto == 1)) {
in = ch - 48;
countp++;
switch (countp) {
case 1:
x1p = in;
// Serial.print("x1p = ");
// Serial.println(x1p);
break;
case 2:
x2p = in;
// Serial.print("x2p = ");
// Serial.println(x2p);
break;
}
// Serial.print("countp = ");
// Serial.println(countp);
if (countp == 2) {
// Serial.println("switch");
switch (count) {
case 1:
out = x1 + (x1p / 10) + (x2p / 100);
count = 0;
countp = 0;
punto = 0;
// Serial.print("out1 = ");
// Serial.println(out);
break;
case 2:
out = (x1 * 10) + x2 + (x1p / 10) + (x2p / 100);
count = 0;
countp = 0;
punto = 0;
// Serial.print("out2 = ");
// Serial.println(out);
break;
case 3:
out = (x1 * 100) + (x2 * 10) + x3 + (x1p / 10) + (x2p / 100);
count = 0;
countp = 0;
punto = 0;
// Serial.print("out3 = ");
// Serial.println(out);
break;
}
}
}
}
br = round(out);
br = map(br, 0, 160, 0, 255);
//
// Serial.print("brillo = ");
// Serial.println(br);
//analogWrite(LED, br);
}
PD: the line where I control the intensity of the led is commented because I'm trying to do a debugging and now I'm just trying to turn on the led when I receive a serial data.
serial_pwm.ino (2.47 KB)
Pendulo.zip (21.6 KB)