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 次查看(过去 30 天)
显示 更早的评论
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
回答(1 个)
Walter Roberson
2023-3-8
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"
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!