필터 지우기
필터 지우기

How to translate Arduino code to MATLAB code?

조회 수: 20 (최근 30일)
Anna Tessman
Anna Tessman 2019년 2월 21일
답변: Asad Mirza 2019년 2월 22일
I am currently working on a project with the AppDesigner, an Arduino Uno board, and a SparkFun PIR Motion Sensor
Sadly, I cannot find example code in MATLAB without using Simulink. Does anyone know where I could find an example or have any tips on how to translate this code to MATLAB code?

채택된 답변

Asad Mirza
Asad Mirza 2019년 2월 22일
You're in luck! MATLAB and Arudino support is very well documentated here. What it boils down to is just replacing classic Arduino IDE functions with their MATLAB equivalent, such as digitalread with readDigitalPin. I've gone ahead and done a rough translation of the code you attached and it should work but I do not have an arudino with me to test it with.
MOTION_PIN = 2; %% Pin connected to motion detector
LED_PIN = 13; %% LED pin - active-high
a = arduino('COM4','Uno')
%% The PIR sensor's output signal is an open-collector,
%% so a pull-up resistor is required:
pinMode(MOTION_PIN, INPUT_PULLUP);
configurePin(a,MOTION_PIN,'Pullup')
configurePin(a,LED_PIN, 'DigitalOutput');
while 1
proximity = readDigitalPin(a,MOTION_PIN);
if (proximity == 0) %% If the sensor's output goes low, motion is detected
{
writeDigitalPin(a,LED_PIN, 1);
disp('Motion detected!');
}
else
{
writeDigitalPin(a,LED_PIN, 0);
}
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by