Serial Connection Problem in Simulink (Solved)

Hi guys,

I have a question. I am trying to connect 2 Arduino Uno with serial connection in Simulink. In the 1st Arduino board I read a sensor information and drive 1st servo. I want to transmit this information to the second Arduino board by serial connection. Based on the transmitted data, the 2nd Arduino board is supposed to drive 2nd servo.

Without Simulink the codes are working (you may find it below). However I couldn't do it in Simulink. I didn't touch the 1st Arduino board. In Simulink for the 2nd Arduino board, simply connecting "Serial Receive" block with "Servo Write" block doesn't function. Any recommendations? You may find the related codes for transmitting board and receiving board.

1st Board - Reading IMU data, driving servo and transmitting the signal to 2nd Arduino Uno

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo myservo;
int val;
int prevVal;

#define OUTPUT_READABLE_ACCELGYRO
#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

// initialize serial communication
    Serial.begin(38400);

// initialize device
    accelgyro.initialize();
// Attach Servo
    myservo.attach(9);
    
// configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    #ifdef OUTPUT_READABLE_ACCELGYRO
        val = map(ax, -17000, 17000, 0, 179);
        if (val != prevVal)
        {
           myservo.write(val);
           Serial.write(val);  
           prevVal = val;
        }
        delay(50);
        
    #endif

// blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

2nd Board - Reading data from 1st board and drive a 2nd servo

#include "Servo.h"

Servo myservo;
int val;
int prevVal;

void setup() {
// Serial port enable
Serial.begin(38400);
// declare pin 11 as output, this is the LED
   myservo.attach(9);
   pinMode (11, OUTPUT);
}
void loop() {

// if there is bytes available coming from the serial port
if (Serial.available()) {
  
// set the values to the ‘val’ variable
         val = Serial.read();
         Serial.println(val);
     
         if (val != prevVal)
         {
            myservo.write(val);
            prevVal = val;
         }
         delay(50);

}
}

This seems to be a question about Simulink, rather than Arduino. Your best chance of getting help within the Arduino community would be to move your post to the 'Interfacing w/ Software on the Computer' section of the forum. However, you're more likely to get help in a support community specific to Simulink.

Thanks for the reply. I will wait a couple of days and then move my post to the related section. I posted the same problem in Arduino Simulink Facebook group and no replies yet. Last chance is to express the problem in Matlab Central.

Ok I found the problem. When there is no available data (status = 0) the servo returns to initial position. Putting a simple enable block in simulink solved it. If the data is available update it otherwise retain the old value :slight_smile:

Ps: Sample Rate in Serial Receive block is 0.05