How to use Arduino code written in Arduino IDE file in MATLAB? How does it work?

168 ビュー (過去 30 日間)
An Gie Lo
An Gie Lo 2018 年 5 月 20 日
コメント済み: Purvi 2023 年 1 月 3 日
Greetings. I came across with this project:
In the link, two files were provided: one for matlab, one for arduino. The programmers used the centroid extracted from the face detected and tracked in matlab, and sent the coordinates to arduino to control the servos.
My question is: 1. How does the fopen(arduino) work? How does matlab know which arduino code file to access?

回答 (2 件)

Ameer Hamza
Ameer Hamza 2018 年 5 月 20 日
MATLAB does not need to know which Arduino sketch file you are using. Here is the procedure of what you need to do.
  1. You will first program your Arduino board, with the given code, using Arduino IDE. Download the Arduino code, open the Arduino IDE and create a new file and paste the downloaded code in the new file. Upload the program to the Arduino board.
  2. Note the port number for Arduino board from the Arduino IDE. Go to Tools menu > Port and note the port number such as COMx.
  3. After uploading the code, keep the Arduino connected using USB cable and open MATLAB.
  4. In the MATLAB code change the line
arduino=serial('COM6','BaudRate',9600);
and change COM6 to COMx you noted from MATLAB.
Now when you run the MATLAB code, it will communicate with the Arduino.
  2 件のコメント
amin ya
amin ya 2019 年 5 月 19 日
So in this workflow it is possible to program Arduino hybridly? Both coding in native IDE and MATLAB?
Purvi
Purvi 2023 年 1 月 3 日
I did the same but i didnt get the desired output. What should i do?

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


Eugenio Anselmino
Eugenio Anselmino 2020 年 9 月 1 日
I don't think is possible to write hybrid code, you have two solution in my opinion:
1) write everything in matlab: performances arent great, but you can comunicate with the board very easily and in real time.
2) write everything in arduino and comunicate to the board through the COM port as shown by Ameer: it's faster, but less easy to do.
  2 件のコメント
Ngcebo Shezi
Ngcebo Shezi 2022 年 9 月 14 日
how do i convert the arduino code from arduino ide to matlab is that possible. my problem is that i dont know how to code using matlab
Rodrigo Oliveira
Rodrigo Oliveira 2022 年 10 月 21 日
編集済み: Rodrigo Oliveira 2022 年 10 月 21 日
There is a somewhat simple solution: use command lines executed from MATLAB. By using Arduino CLI you can execute the files you made with Arduino IDE just from the terminal itself. Now, using MATLAB function "system" you can execute commands like so:
sytem('cd Documents/MyArduinoDir')
Executing the command: system('arduino-cli upload yourfile.ino') executes the code you built. MATLAB is free to continue down its commands on the .m script while the 'yourfile.ino' is still running.

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

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by