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개)

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

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

질문:

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