while loop iteration in App Designer with Arduino

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월 23일
Hi Shubham,
I see that grabing the data from the variable 'data' will do the job but the problem is that each lamp must match with a specific pin. Will that not matter since the k iteration is grab all and reading the value from excel file and find the k iteration over and over?
Gojo
Gojo 2024년 9월 24일
"the problem is that each lamp must match with a specific pin" : wouldn't the mapping for the lamp and pin be available in the excel sheet? I assumed that you have already stored the mapping in a table where lamps(i) and pins(i) would be a match.
"Will that not matter since the k iteration is grab all and reading the value from excel file and find the k iteration over and over?": Could you elaborate this query? I used a nested loop, because in your original post, you were checking each individual pin which could be done in a loop.
Min
Min 2024년 9월 24일
I see, I did not fully understood the code that you had above.
Since they already match within the excel file it shouldn't be a problem with the iteration.
The second question I had was regarding the processing speed. I was wondering if have the find function to find the specific lamp name on the excel file and iterate that would be faster but since I need to read all of them, thus it wouldn't matter.
I will try this code and see how it works!
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개)

카테고리

도움말 센터File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

Min
2024년 9월 20일

댓글:

2024년 9월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by