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