change this arduino code into matlab code:

int pd=2;
int buzz=13;
int senRead=0;
int limit=1000;
void setup()
{
pinMode(pd,OUTPUT);
pinMode(buzz,OUTPUT);
digitalWrite(pd,HIGH);
digitalWrite(buzz,LOW);
Serial.begin(9600);
}
void loop()
{
int val=analogRead(senRead);
Serial.println(val);
if(val <= limit)
{
digitalWrite(buzz,HIGH);
}
else if(val > limit)
{
digitalWrite(buzz,LOW);
delay(20);
}
}

1 个评论

The "if" after the else is redundant. With the int data type, every val that is not <= limit must be > limit.
If you were using floating point data types instead, then there is one case where both conditions can be false, and that is if val is NaN (Not A Number).

请先登录,再进行评论。

回答(1 个)

I have not tried this out, hope this works out. I am using R2015a syntaxes for which you will have to download the support package. If it does not, please post any error messages that you get.
pd = 'D2';
buzz = 'D13';
senRead = 'A0';
limit = 1000;
a = arduino;
configurePin(a, pd, 'DigitalOutput')
configurePin(a, buzz, 'DigitalOutput')
writeDigitalPin(a, pd, 1);
writeDigitalPin(a, buzz, 0);
for i = 1:1000
val = readVoltage(a,senRead)
if val <= limit
writeDigitalPin(a, buzz, 1);
elseif val > limit
writeDigitalPin(a, buzz, 0);
pause(0.002);
end
end

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB Support Package for Arduino Hardware 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by