Sending values from Matlab to arduino using serial communication
11 ビュー (過去 30 日間)
古いコメントを表示
I want to send numeric value from matlab to arduino but code is not working. Matlab code is as:
doi = 3 ;
arduino=serial('COM5','BaudRate',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
fclose(arduino);
Arduino code is as:
int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
{
Serial.begin(9600);
//pinMode(13, OUTPUT);
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
}
void loop()
{
if(Serial.available()>0)
{
doi = Serial.read();
//Serial.println(doi);
//Serial.println("\n");
//doi = doi - 48;
if (doi == 1)
{
//Serial.println(1);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait .15 Second
digitalWrite(solenoidPin, LOW);
}
else if (doi == 2)
{
//Serial.println(2);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(2000); //Wait .165 Second
digitalWrite(solenoidPin, LOW);
}
else
{
//Serial.println(3);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(3000); //Wait .180 Second
digitalWrite(solenoidPin, LOW);
}
}
}
I used str2num(doi) also instead of fprintf(arduino, '%s', char(doi)) but no output.
Please give suggestion to correct this.
Thanks.
5 件のコメント
Walter Roberson
2019 年 11 月 20 日
There are a few possibilities for display:
- you can send the values to some kind of display such as an LCD display; https://www.arduino.cc/en/Tutorial/HelloWorld
- you can send the values back to MATLAB and have MATLAB display them
- if you use a different connection method between MATLAB and arduino, so that the communications between MATLAB and arduino is not through the serial port monitor, then you can send the values to the serial port and use some kind of monitor system on there.
- You can use a serial port (or USB port) monitor on the MATLAB host side to see what is getting sent. https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/capture-and-view-ing-usb-traces-with-microsoft-message-analyzer- . Or see https://freeusbanalyzer.com/ which permits short monitoring for free (and presumably there is a paid version.)
回答 (3 件)
Walter Roberson
2017 年 2 月 21 日
"But what the problem is I do not know how to use _fopen(arduino) command between functions in GUI?"
fopen once and save the object somewhere so that you do not need to fopen again.
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
You could have a look at https://www.mathworks.com/matlabcentral/fileexchange/26371-simple-gui-for-serial-port-communication to see how they set up communications.
1 件のコメント
Rouis Jihene
2017 年 5 月 31 日
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
3 件のコメント
Rouis Jihene
2017 年 6 月 4 日
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Azrg
2019 年 10 月 23 日
The thing you want to send has to be in a while loop like
while true
fprint(something);
end
Because Matlab has to keep trying to send the information until Arduino receives it.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で MATLAB Support Package for Arduino Hardware についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!