i believe there may be a problem with entering my switch case

2 次查看(过去 30 天)
i am using this code to read a value from the moisture sensor and light up an led depending on the moisuture. however when i run this code it makes it as far as reading the moisture value and not doing anything else. any help at all would be greatly appreciated
code for reference:
clear
clc
a = arduino();
PB = 'D8';
key = readDigitalPin(a,PB);
pbc = 0;
b = mod(pbc, 3);
moisture = readVoltage(a,"A0")
while true
switch b
case 0
if moisture >= 4 && moisture <= 5
writeDigitalPin(a,'D2',1) % green
writeDigitalPin(a,'D4',0) % orange
writeDigitalPin(a,'D7',0) % red
%writePWMVoltage(a, 'D11',1.67)
pause(5)
writePWMVoltage(a, 'D11',0 )
pause(120)
else
writeDigitalPin(a,'D2',1) % green
end
case 1
if moisture >= 3 && moisture <= 4
writeDigitalPin(a,'D2',1) % green
writeDigitalPin(a,'D4',0) % orange
writeDigitalPin(a,'D7',0) % red
%writePWMVoltage(a, 'D11',1.67)
pause(5)
writePWMVoltage(a, 'D11',0 )
pause(120)
else
writeDigitalPin(a,'D2',1) % green
end
case 2
if moisture >= 2 && moisture <= 3
writeDigitalPin(a,'D2',1) % green
writeDigitalPin(a,'D4',0) % orange
writeDigitalPin(a,'D7',0) % red
% writePWMVoltage(a, 'D11',1.67)
pause(5)
writePWMVoltage(a, 'D11',0 )
pause(120)
else
writeDigitalPin(a,'D2',1) % green
end
end
if key == 1
pbc = pbc + 1;
end
end

回答(1 个)

Alexander
Alexander 2023-5-25
Shouldn't be
moisture = readVoltage(a,"A0")
not also in the while-loop?

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by