Clear Filters
Clear Filters

Edit Text Pushbutton GUI to enter data to Arduino serial communication

1 view (last 30 days)
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 Comment
Geoff Hayes
Geoff Hayes on 3 Feb 2017
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.

Sign in to comment.

Answers (1)

Muhammad Imran
Muhammad Imran on 27 Jan 2018
plz tell me if u have corrected this code m.suhaib96@gmail.com
  1 Comment
Jan
Jan on 27 Jan 2018
@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.

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!