Sending values from Matlab to arduino using serial communication
조회 수: 14 (최근 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.
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!