Effacer les filtres
Effacer les filtres

How to translate Arduino code to MATLAB code?

20 vues (au cours des 30 derniers jours)
Anna Tessman
Anna Tessman le 21 Fév 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?

Réponse acceptée

Asad Mirza
Asad Mirza le 22 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Arduino Hardware dans Help Center et File Exchange

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by