Filter löschen
Filter löschen

How to translate Arduino code to MATLAB code?

20 Ansichten (letzte 30 Tage)
Anna Tessman
Anna Tessman am 21 Feb. 2019
Beantwortet: Asad Mirza am 22 Feb. 2019
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?

Akzeptierte Antwort

Asad Mirza
Asad Mirza am 22 Feb. 2019
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

Weitere Antworten (0)

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by