How to translate Arduino code to MATLAB code?

17 次查看(过去 30 天)
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