Is it possible to control/read Arduino pins at different frequencies?
Show older comments
Hello,
The attached code is meant to flash an RGB LED and record data from a photoresistor. Currently, the code flashes the LED on one line, and then records data from the analog chanel on the next line. Ideally, I would like to have parallel code that flashes the LEDs based on a specific duty cycle (e.g. 6 Hz flashes cycling red, green, and blue) but a higher sampling frequency for the photoresistor analog chanel (e.g. 30 Hz). Is this possible within MATLAB's paradigm of sequential code execution?
Thanks in advance.
%% Arduino RGB LED Control%%
clear all
clc
% Create an arduino object
a = arduino();
% Collection parameters
cycles = 10;
flashTime = .1;
pauseTime = .1;
photoResistor = zeros(cycles,3);
% Configure and pulse LED based on collection parameters
writePWMVoltage(a,'D3',5); % Red
writePWMVoltage(a,'D5',5); % Green
writePWMVoltage(a,'D6',5); % Blue
for i = 1:cycles
pause(pauseTime);
%Flash Red
writePWMVoltage(a,'D3',0); % Red
writePWMVoltage(a,'D5',5); % Green
writePWMVoltage(a,'D6',5); % Blue
pause(flashTime); photoResistor(i,1) = readVoltage(a, 'A0');
writePWMVoltage(a,'D3',5); % Red
pause(pauseTime)
writePWMVoltage(a,'D3',5); % Red
writePWMVoltage(a,'D5',0); % Green
writePWMVoltage(a,'D6',5); %Blue
pause(flashTime); photoResistor(i,2) = readVoltage(a, 'A0');
writePWMVoltage(a,'D5',5); % Green
pause(pauseTime)
writePWMVoltage(a,'D3',5); % Red
writePWMVoltage(a,'D5',5); % Green
writePWMVoltage(a,'D6',0); %Blue
pause(flashTime); photoResistor(i,3) = readVoltage(a, 'A0');
writePWMVoltage(a,'D6',5); % Blue
pause(pauseTime);
plot(photoResistor(:,1),'r');hold on;
plot(photoResistor(:,2),'g');hold on
plot(photoResistor(:,3),'b');
legend('Red', 'Green', 'Blue');hold off;
end
writePWMVoltage(a,'D3',5); % Red
writePWMVoltage(a,'D5',5); % Green
writePWMVoltage(a,'D6',5); %Blue
clear a %Disconnect from Arduino
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Mobile in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!