I am trying to run a simple Led On and Off project using arduino and app designer. The program does seem to function however, the code has errors

15 views (last 30 days)
This is the code I put under the callbacks for the push buttons:
function LedOnButtonPushed(app, event)
clear all;
global a;
a = arduino;
while(1)
writeDigitalPin(a,'D5',1)
end
end
function LedOffButtonPushed(app, event)
clear all;
global a;
a = arduino;
while(1)
writeDigitalPin(a,'D5',0)
end
end
These are the errors that I receive:
Error using testing/LedOffButtonPushed (line 45)
MATLAB connection to Mega2560 at COM4 exists in your workspace. To create a new connection, clear the existing object.
Error using asyncio.Stream/wait (line 183)
Error while evaluating Button PrivateButtonPushedFcn.
Undefined function 'writeDigitalPin' for input arguments of type 'double'.
Error in testing/LedOnButtonPushed (line 26)
writeDigitalPin(a,'D5',1)
Error while evaluating Button PrivateButtonPushedFcn.
%Your help will be appreciated thank you!

Accepted Answer

Prateek
Prateek on 21 Nov 2022
Hi Kekeletso,
The code snippets you have posted seem fine in themselves; perhaps the problem lies in the rest of the code.
For better assistance, I request you to post the app designer file and complete code, so I can assess the source of the error.
Regards,
Prateek
  3 Comments
Prateek
Prateek on 28 Nov 2022
Thanks for sharing the code.
I have made some changes, as follows -
a) A private property "connObj" is declared (line 12) and placed in a StartupFcn called "initialize" (line 20). This property stores the Arduino object, which gets created when the app is launched. This ensures that callback functions have access to a single Arduino object.
b) The call back functions for buttons use "connObj" to change the state of "Ledpin". This operation does not need an infinite while loop and hence that has been removed (lines 31 and 38).
The edited app is attached here.
You may also want to refer to the following links for details on app designer-
Hope this helps.
Regards,
Prateek

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!