フィルターのクリア

Edit Text Pushbutton GUI to enter data to Arduino serial communication

1 回表示 (過去 30 日間)
Ishi
Ishi 2017 年 2 月 3 日
コメント済み: Jan 2018 年 1 月 27 日
I have made a simple GUI with an Edit Text box and a Pushbutton. The user can enter either 1 or 2 and the Led 13 on the arduino will switch ON and OFF respectively. Pin 13 blinks but does not stay on upon receiving "1" via the GUI.
Matlab code:
// Only pushbutton1_Callback function was changed
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
arduino=serial('COM17','BaudRate',9600);
fopen(arduino);
th1= get(handles.edit1, 'string');
th1= str2double(th1);
fprintf(arduino,'%d', th1); % send answer variable content to arduino
fclose(arduino)
Arduino Code:
int ledPin=13;
int matlabData;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin,LOW); // turn light off
}
  1 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 2 月 3 日
Ishi - have you verified what numbers are being written to the Arduino? Are you receiving a one or a two? You may want to add some debugging statements (if possible?) to the Arduino code to verify that you are receiving the correct integers and only when you send them.

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

回答 (1 件)

Muhammad Imran
Muhammad Imran 2018 年 1 月 27 日
plz tell me if u have corrected this code m.suhaib96@gmail.com
  1 件のコメント
Jan
Jan 2018 年 1 月 27 日
@Muhammad Imran: This is not an answer. Please remove it and post it as a comment.
Because Matlab Answers is a public forum, the idea is to share solutions in public, not by email. Posting your mail address here will increase the number of spam mails you get.

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

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by