Answered step by step
Verified Expert Solution
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
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 D G to D
and B to D Connect the sign on the LED to GND Leaving your flex
sensor voltage divider intact, hook up your potentiometer to V and
GND Hook up the wiper pin of the potentiometer to pin A 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.
nbpinModeA 'ainput'; For flex sensor
nbpinModeA 'ainput'; For potentiometer
nbinitRGBDDD; 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 lines to run a live plot
nblivePlotanalogA;
nblivePlotanalogA;
Record your max and min ADC values for each sensor:
flexMax ;
flexMin ;
potMax ;
potMin ;
Let's start our loop:
tic
while toc
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 nbanalogReadA;
bright potValpotMax; ratio of the pot ADC reading over the max pot ADC value
Now we normalize the current flex reading to a range between and
Let's compare ratios in this case, read reflects our ADC, and
targ reflects the RGB range This is called linear interpolation:
readVal readMinreadMax readMintargVal targMintargMax targMin
Rearranging:
targVal readVal readMinreadMax readMintargMax 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 nbanalogReadA;
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 flexMinflexMax flexMin;
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 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 roundredRGB;
greenRGB roundgreenRGB;
Finally, set the RGB to the right values and include an optional
pause for less computationnoise
nbsetRGB;
pause;
end
nbsetRGB;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started