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 Comment

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

Sign in to comment.

Answers (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

Tags

Asked:

on 17 Jul 2015

Commented:

on 24 Sep 2015

Community Treasure Hunt

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

Start Hunting!