Azzera filtri
Azzera filtri

Edit Text Pushbutton GUI to enter data to Arduino serial communication

1 visualizzazione (ultimi 30 giorni)
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 Commento
Geoff Hayes
Geoff Hayes il 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.

Accedi per commentare.

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by