필터 지우기
필터 지우기

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 CenterFile 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!

Translated by