Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

% % 9 . Using the flex sensor and potentiometer to control an RGB LED % Now we're going to control an RGB LED using

%%9. Using the flex sensor and potentiometer to control an RGB LED
% Now we're going to control an RGB LED using the flex sensor and
% potentiometer. To hook up your RGB LED, connect R to pin D12, G to D11,
% and B to D10. Connect the '-' sign on the LED to GND. Leaving your flex
% sensor voltage divider intact, hook up your potentiometer to 3.3V and
% GND. Hook up the wiper pin of the potentiometer to pin A2 on the Arduino.
%
% We're going to control the brightness of the LED using the potentiometer,
% and we'll make the LED change color from green to red depending on how
% much it's been flexed.
nb.pinMode('A1', 'ainput'); % For flex sensor
nb.pinMode('A2', 'ainput'); % For potentiometer
nb.initRGB('D12','D11','D10'); % Initialize the RGB
% First, we want to find our ADC values that bound the range of each
% sensor. The easiest way to do this could be by doing a live plot of each
% sensor and noting what ADC values the range seems to be bound by.
% Uncomment the following line(s) to run a live plot
% nb.livePlot('analog','A1');
% nb.livePlot('analog','A2');
% Record your max and min ADC values for each sensor:
flexMax =575;
flexMin =480;
potMax =1022;
potMin =10;
% Let's start our loop:
tic
while toc <30
% Since the pot simply controls the brightness, and both the ADC and RGB
% LED ranges have a minimum value of zero, we can scale the brightness
% using a ratio of current reading over the max value.
potVal = nb.analogRead('A2');
bright = potVal/potMax; % ratio of the pot ADC reading over the max pot ADC value
% Now we normalize the current flex reading to a range between 0 and
%255. Let's compare ratios (in this case, read reflects our ADC, and
% targ reflects the RGB range) This is called linear interpolation:
%(readVal - readMin)/(readMax - readMin)=(targVal - targMin)/(targMax - targMin)
% Rearranging:
% targVal =((readVal - readMin)/(readMax - readMin))*(targMax - targMin)+ targMin
% Now let's take a reading from the flex sensor. We want to ensure our
% value is within the range we declared in order for our linear
% interpolation to work.
flexVal = nb.analogRead('A1');
if flexVal > flexMax % if flex reading is greater than maximum reading
flexVal = flexMax; % set to what?
elseif flexVal < flexMin % if flex reading is less than minimum reading
flexVal = flexMin; % set to what?
end
% Time to linearly interpolate the RGB value from our flex reading.
rawRGB =((flexVal - flexMin)/(flexMax - flexMin))*(255-0)+0;
% To color shift, lets make green correspond to the minimum flex, and
% red correspond to maximum flex. We'll set redRGB to roundRGB, and
% greenRGB to whatever is left over.
redRGB = rawRGB;
greenRGB =255- redRGB;
% Now we can scale the values by brightness
redRGB = redRGB * bright;
greenRGB = greenRGB * bright;
% Note that our RGB values may not be integers, and they need to be
% to work with the function we use to control the LED. Lets round them.
redRGB = round(redRGB);
greenRGB = round(greenRGB);
% Finally, set the RGB to the right values (and include an optional
% pause for less computation/noise)
nb.setRGB('?','?',0);
pause(0.05);
end
nb.setRGB(0,0,0);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions