Effacer les filtres
Effacer les filtres

Edit Text Pushbutton GUI to enter data to Arduino serial communication

1 vue (au cours des 30 derniers jours)
Ishi
Ishi le 3 Fév 2017
Commenté : Jan le 27 Jan 2018
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 commentaire
Geoff Hayes
Geoff Hayes le 3 Fév 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.

Connectez-vous pour commenter.

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by