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

조회 수: 1 (최근 30일)
Jake
Jake 2023년 5월 25일
답변: Alexander 2023년 5월 25일
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