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);
}
}