while loop iteration in App Designer with Arduino

조회 수: 3 (최근 30일)
Min
Min 2024년 9월 20일
댓글: Gojo 2024년 9월 25일
Hi,
So I have this code
if value == 1
while value == 1
i = readDigitalPin(app.Output_Arduino,'D7');
if i == 1
app.ThrottleSW412Lamp.Color = 'g'
else
app.ThrottleSW412Lamp.Color = 'r'
end
pause(1/2)
ii = readDigitalPin(app.Output_Arduino,'D6');
if ii == 1
app.FlywheelMagPickup28Lamp.Color = 'g'
else
app.FlywheelMagPickup28Lamp.Color = 'r'
end
pause(1/2)
end
% readDitigalPin(app.Output_Arduino, 'D6')
% readDitigalPin(app.Output_Arduino, 'D5')
% readDitigalPin(app.Output_Arduino, 'D4')
% readDitigalPin(app.Output_Arduino, 'D3')
% readDitigalPin(app.Output_Arduino, 'D21')
% readDitigalPin(app.Output_Arduino, 'D20')
% readDitigalPin(app.Output_Arduino, 'D19')
% readDitigalPin(app.Output_Arduino, 'D18')
% readDitigalPin(app.Output_Arduino, 'D32')
% readDitigalPin(app.Output_Arduino, 'D31')
% readDitigalPin(app.Output_Arduino, 'D30')
% readDitigalPin(app.Output_Arduino, 'D29')
% readDitigalPin(app.Output_Arduino, 'D28')
% readDitigalPin(app.Output_Arduino, 'D27')
% readDitigalPin(app.Output_Arduino, 'D26')
% readDitigalPin(app.Output_Arduino, 'D25')
% readDitigalPin(app.Output_Arduino, 'D24')
% readDitigalPin(app.Output_Arduino, 'D13')
% readDitigalPin(app.Output_Arduino, 'D12')
end
as you can see that if I click the toggle button then the while loop starts which read the digital input from the Arduino and change the color or the lamp.
I truely believe that there has to be a way to iterate this instead of me adding the lamp and if statement for all pins. I have the pins read from excel file which I hope it would be easier since I might be able to use strcmpi and find function to fin the lamp name and then iterate it but I am not sure where to start my approach. If someone can suggest some ideas would be appreciated.
Thanks!

채택된 답변

Gojo
Gojo 2024년 9월 22일
편집: Gojo 2024년 9월 23일
Hey Min,
I understand that you wish to modify certain properties in a class within an iteration without the need of hardcoding it.
Based on your description of the query, I assume you have already stored the necessary data from the excel file into a table. You can simply extract the string containing the property names from the table and modify them accordingly. For instance refer to the following code snippet:
% Assuming you stored the reqd. info in a variable "data".
pins = data.Pin;
lamps = data.Lamp;
% Your loop iterations
while value == 1
for k = 1:length(pins)
pin = pins{k};
lamp = lamps{k};
pinValue = readDigitalPin(app.Output_Arduino, pin);
% Update the lamp color
if pinValue == 1
app.(lamp).Color = 'g'; %you can use the string itself to access the property value
% Make apt changes as you need
For referencing the properties using variables, I suggest you to check out the highlighted section of the following documentation: https://www.mathworks.com/help/matlab/matlab_oop/defining-properties.html#mw_5f7e0964-b8f2-4350-a393-ec3cb4e115f8
I hope this helps!
  댓글 수: 5
Min
Min 2024년 9월 24일
Looks like it works! Thannk you!
Gojo
Gojo 2024년 9월 25일
Glad to know your query has been resolved!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by