Azzera filtri
Azzera filtri

How to turn on arduino digital pin using an external push button in app designer

7 visualizzazioni (ultimi 30 giorni)
I want to turn on a led when a push button connected to arduino is pressed, also when push button on the app is pressed it should turn on the led for 0.5 sec and turn off. To turn on the Led push button on the app or external push button should be pressed again.
# It gives an error that --- Undefined function 'writeDigitalPin' for input arguments of type 'double'
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
end
properties (Access = public)
a % Description
ButtonIsPressed
correctPin
end
methods (Access = private)
function CheckPts(app)
app.ButtonIsPressed = readDigitalPin(app.a, app.buttonPin);
if ~app.ButtonIsPressed && app.doneReward
writeDigitalPin(app.a, app.correctPin, 1);
pause(.005)
writeDigitalPin(app.a, app.correctPin, 0);
app.doneReward = 0;
end
if readDigitalPin(app.a,app.buttonPin)
app.doneReward = 1;
end
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% Initialize variables
clear all
% close all
app.a = arduino('COM11', 'nano');
app.buttonPin = 'D3';
app.correctPin = 'D5';
app.ButtonIsPressed = 0;
app.doneReward = 1;
end
% Button pushed function: Button
function ButtonPushed(app, event)
writeDigitalPin(app.a, app.correctPin, 1);
pause(.5)
writeDigitalPin(app.a, app.correctPin, 0);
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [289 230 100 22];
end
end
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Risposte (1)

Ahsan Ayyaz
Ahsan Ayyaz il 28 Ago 2021
Modificato: Ahsan Ayyaz il 28 Ago 2021
This code served the purpose without using app designer
close all
clear all
global a;
a = arduino('COM11','nano');
done= 1;
h_fig=figure;
drawnow
set(h_fig,'KeyPressFcn',{@myfun});
kk=1;
while kk<2
drawnow
if ~readDigitalPin(a,'D3')&& done
writeDigitalPin(a, 'D5', 1);
pause (0.0005);
writeDigitalPin(a, 'D5', 0);
done = 0;
end
if readDigitalPin(a,'D3')
done= 1;
end
end %kk
function myfun(~,event)
global a;
value = event.Key;
if value
writeDigitalPin(a, 'D5', 1);
pause (0.0005);
writeDigitalPin(a, 'D5', 0);
end
end

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by