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 件のコメント

Walter Roberson
Walter Roberson 2015 年 9 月 24 日
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 件)

Madhu Govindarajan
Madhu Govindarajan 2015 年 9 月 24 日
編集済み: Walter Roberson 2015 年 9 月 24 日

0 投票

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

カテゴリ

タグ

質問済み:

2015 年 7 月 17 日

コメント済み:

2015 年 9 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by