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!

 Accepted Answer

Gojo
Gojo on 22 Sep 2024
Edited: Gojo on 23 Sep 2024
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 Comments

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?
"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.
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!
Looks like it works! Thannk you!
Glad to know your query has been resolved!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Asked:

Min
on 20 Sep 2024

Commented:

on 25 Sep 2024

Community Treasure Hunt

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

Start Hunting!