ارتباط ورودی و خروجی
.
%InputOutput
function [] = InputOutput()
a = arduino();
% Define a cleanup function to be executed when the program is terminated
finishup = onCleanup(@() StopProgram(a));
configureDigitalPin(a, 2,'pullup');
LED=(13);
while true
button_status=readDigitalPin(a,2);
writeDigitalPin(a,LED,button_status);
%disp(button_state)
disp(['push button status is: ' , num2str(button_status)])
pause(0.1)
end
end
% Define a function to be executed when the program is terminated while press ctrl+c
function StopProgram(Arduino)
clc
clear
close all
disp('Program Terminated')
end
.
.
PWM با پتانسیومتر با روش LED کنترل روشنایی
.
%Control an LED using a potentiometer_PWM
function [] = LED_Potentiometer_PWM()
a=arduino();
finishup = onCleanup(@() StopProgram(a));
LED=11;
POT=0;
while true
voltage = readVoltage(a,POT);
writePWMVoltage(a, LED, voltage);
disp(['Analog Voltage is: ',num2str(voltage,'%0.2f')]);
pause(0.1);
end
end
% Define a function to be executed when the program is terminated while press ctrl+c
function StopProgram(Arduino)
clc
clear
close all
disp('Program Terminated')
end
.
.
Duty Cycle با پتانسیومتر با روش LED کنترل روشنایی
.
%Control an LED using a potentiometer_Dutu-Cycle
function [] = LED_Potentiometer_DutyCycle()
a=arduino();
finishup = onCleanup(@() StopProgram(a));
LED=11;
POT=0;
while true
voltage = readVoltage(a,POT);
DutyCycle=voltage * 0.2 ;
writePWMDutyCycle(a, LED, DutyCycle);
disp(['Your PWM DutyCycle is: ',num2str(DutyCycle*100,'%0.0f'),'%']);
pause(0.1);
end
end
% Define a function to be executed when the program is terminated while press ctrl+c
function StopProgram(Arduino)
clc
clear
close all
disp('Program Terminated')
end
.
*توضیح:چون ولتاژ خروجی آردوینو ۵ ولت است پس برای تبدیل آن به 0 و 1 برای duty cycle ولتاژ را در 0.2 ضرب میکنیم. و در خط disp برای نمایش به صورت درصد آنرا در 100 ضرب میکنیم.
.
چشمک زن دوتایی
.
.
%Blinking two leds
function [] = Two_LED_Blink()
a=arduino();
finishup = onCleanup(@() StopProgram(a));
while true
writeDigitalPin(a, 9, 1);
writeDigitalPin(a, 10, 0);
pause(0.5);
writeDigitalPin(a, 9, 0);
writeDigitalPin(a, 10, 1);
pause(0.5);
end
end
% Define a function to be executed when the program is terminated while press ctrl+c
function StopProgram(Arduino)
clc
clear
close all
disp('Program Terminated')
end
دیدگاهها
هیچ نظری هنوز ثبت نشده است.