How do you run a DC motor from an Arduino from the app designer app? I have looked through countless tutorials and none of them work.

5 views (last 30 days)
classdef teset2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
end
properties (Access = private)
a
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
clear all
app.a = arduino();
end
% Button pushed function: Button
function ButtonPushed(app, event)
writePWMVoltage(app.a,'D6',5)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [36 399 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = teset2
% Create UIFigure and 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

Answers (1)

Walter Roberson
Walter Roberson on 8 Mar 2023
When you clear all, you clear app itself, and nothing after that can work.
The only place you should ever use "clear all" in code is in a small script that is intended to completely reset the state of MATLAB when you switch tasks, a shortcut to closing and reopening MATLAB. If you are not doing the equivalent of quitting MATLAB and restarting then you should not have "clear all"

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!