while loop iteration in App Designer with Arduino

2 ビュー (過去 30 日間)
Min
Min 2024 年 9 月 20 日
コメント済み: Shubham 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!

採用された回答

Shubham
Shubham 2024 年 9 月 22 日
編集済み: Shubham 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!
Shubham
Shubham 2024 年 9 月 25 日
Glad to know your query has been resolved!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by