Azzera filtri
Azzera filtri

How to translate Arduino code to MATLAB code?

20 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Asad Mirza
Asad Mirza il 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

Più risposte (0)

Categorie

Scopri di più su Arduino Hardware in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by