Sending a variable from MATLAB to Arduino

I have a probelm in sending variables from MATLAB to Arduino using the serial connection. Here it is just a test that I prepared but it does not work.
When I run the MATLAB code I see that something happens on Arduino and it seems that the Arduino IDE load the code on the board again (I load it before running MATLAB), but the variable does not change and nothing happens.
Here is the MATLAB Code:
clear all; clc;
variable = 100;
arduino=serialport('/dev/tty.usbmodem1401',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fwrite(arduino, '%d', variable) % send answer variable content to arduino
clear arduino; % end communication with arduino
%fclose(arduino);
And here is the Arduino Code:
int variable = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
if (Serial.available() > 0){
variable = Serial.read();
}
if(variable != 0 ){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
void loop() {
}
Thank you!

 採用された回答

Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 15 日
編集済み: Bora Eryilmaz 2022 年 12 月 15 日

1 投票

You have to call Serial.read() in loop() and not in setup(), since setup() is called only once when Arduino starts running. Also, the second if-block should probably be nested inside the first if-block.
void loop() {
if (Serial.available() > 0){
variable = Serial.read();
if(variable != 0 ){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
}

5 件のコメント

Lorenzo Lellini
Lorenzo Lellini 2022 年 12 月 15 日
Thank you for your answer, but it is still not working.. Every time I press Run on MATLAB I see the blinkng LED on Arduino such as when it loads the code from its IDE but nothing else happens.
I also tried to specify that I am sending a16 bit integer variable..
I am sure that all the connections on the breadboard are correct because if I try to blink the LED from Arduino IDE without MATLAB it works.
It should be quite easy but I cannot understand why.
Just to be clear, my code now are:
clear all; clc;
variable = 100;
arduino=serialport('/dev/tty.usbmodem1401',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fwrite(arduino, variable, 'int16') % send answer variable content to arduino
clear arduino; % end communication with arduino
and
int variable = 0;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0){
variable = Serial.read();
if(variable != 0 ){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
}
Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 15 日
Is "/dev/tty.usbmodem1401" in your MATLAB code the same serial device that you use in Arduino IDE?
Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 15 日
"Every time I press Run on MATLAB I see the blinkng LED on Arduino such as when it loads the code from its IDE but nothing else happens."
That is what your code is programmed for: It waits for a variable from MATLAB, blinks the LED once, and stops.
Did you want it to do something else?
Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 15 日
If you want it to blink continuously once the variable is received, then you would need to take the second if-block out of ther first if-block as in your original code.
Lorenzo Lellini
Lorenzo Lellini 2022 年 12 月 15 日
The aim of this test is to give the variable "step_1cm" in this following stepper motor control code.
And it does not works.
I tried directly with the Arduino MATLAB extension but I cannot find a way to control a all the stepper motor parameters without an Adafruit shield. I can just control the pins and not other parameters such as the speed as with the <stepper.h> library.
I could use the Arduino IDE script but, for this reason, I need to send that varaible to control the motor rotation.
#include <Stepper.h>
int speed = 64;
int step_1cm;
Stepper motore1(512, 2,3,4,5);
void setup() {
Serial.begin(9600);
motore1.setSpeed(speed);
}
void loop() {
if (Serial.available() > 0){
step_1cm = Serial.read();
}
motore1.step(step_1cm);
delay(2000);
motore1.step(-step_1cm*3);
delay(500);
motore1.step(step_1cm*2);
delay(500);
}

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

製品

リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by