How to better control an RGBLED with a colormap array?

What I need it to do, is to have my led change colors based off the temperature (or voltage) with the colors of jet. As of right now, running the latter I get an error saying Index in position 1 exceeds array bounds (must not exceed 1). Error in capstone (line 43). red = cmap(gn, 1);
I can get my plot to work fine beforehand, but I am at a loss for how to fix this. I'm attempting to have my colormap in an array of size 256 where each column is a color, for each rgbLED color. my cmap equation came from taking my highest voltage and lowest voltage as points and creating an equation. IE (.3,0) is low for blue and (.8, 256) is high for red.
This is with arduino uno r3 and matlab. No simulink
This is my code before tinkering with it:
cmap = jet(256); % 3 rows by 256 columns for color (red , green, blue) %defines the colormap and number of the entries with max %256
interv = 500;
initialTime=1;
x=0;
while (initialTime<interv)
v = readVoltage(a, 'A0');
cmap = 582v -192;
if v < .33 %if voltage is lower than whats allowed, set the %value above the lowest
v= .34;
end
if v > .77 %if voltage is higher than whats allowed, set the %value lower the highest
v = .76;
end
writePWMVoltage(a, 'D6', v); %blue
writePWMVoltage(a, 'D9', v); %green
writePWMVoltage(a, 'D5', v); %red
tempC = (v-.5)35;
tempF = tempC*9/5 + 32;
x=[x,tempF];
xlim([0 150])
grid ON
plot(x)
title('Voltages Recorded from Thermistor')
xlabel('Time(sec)')
ylabel('Temperature (F)')
initialTime=initialTime+1; drawnow
end
Here is some code with changes:
a = arduino;
%v = readVoltage(a,'A0');
%finds the resistance of the thermistor
voltRead = readVoltage(a,'A0');
voltTotal = 5;
volt = voltTotal-voltRead;
amps = voltRead/9750;
res = volt/amps;
% 3 rows by 256 columns for color (red , green, blue)
%defines the colormap and number of the entries with max 256
colormap jet;
cmap = jet(256);
colorbar;
colormap(gca, cmap);
green = 1; %greenLED
blue=1;
red=1;
gn = 0;
%Runs for 500 intervals
interv = 500;
initialTime=1;
x=0;
while (initialTime<interv)
v = readVoltage(a, 'A0');
%tempC = (v-.5)*35;
temp = (v*500)/1023;
tempF = temp*9/5 + 32;
cmap = 582*v -192;
gn = round(cmap, 0);
red = cmap(gn, 1);
green = cmap(gn, 2);
blue = cmap(gn, 3);
%if voltage is lower than whats allowed, set the
%value above the lowest
if v < .33
v= .34;
end
%if voltage is higher than whats allowed, set the
%value lower the highest
if v > .77
v = .76;
end
jetTemp = round((tempF - 32)*3.764);
jetTempRGB = jetTemp +30;
%removes error possibility of being over the array limit
if jetTempRGB > 256
jetTempRGB = 256;
end
writePWMVoltage(a, 'D6', cmap(jetTempRGB, 1)); %blue
writePWMVoltage(a, 'D9', cmap(jetTempRGB, 2)); %green
writePWMVoltage(a, 'D5', cmap(jetTempRGB, 3)); %red
x=[x,tempF];
xlim([0 150])
grid ON
plot(x)
title('Voltages Recorded from Thermistor')
xlabel('Time(sec)')
ylabel('Temperature (F)')
initialTime=initialTime+1;
drawnow
end

Answers (0)

Asked:

on 29 Nov 2020

Community Treasure Hunt

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

Start Hunting!