# How to implement multiple LED modes in a sketch with potentiometers?

My hardware is 4 potentiometers connected to Arduino Analog 0-3, the output is an RGB led driver connected to Digital outputs 9,10,11. The output channels provide access to PWM control of those pins. So idea is the color and intensity can be controlled by the Arduino. I wrote a simple Sketch that reads the potentiometers and then outputs PWM to the RGB driver circuitry. This works great! So the hardware is working! Now comes the last potentiometer...

I want to make the last potentiometer a "mode select" control. It would function like this: If the last potentiometer (Pot4) is turned down all the way, then all lights are off. If the level is turned up a little, the Arduino then controls the RGB channels via Pots 1, 2 & 3. If Pot4 is turned up to just past halfway, the sketch enters a "strobe flash pattern" pulsing all channels on and off. And if the mode control Pot4 is turned up all the way, Arduino turns on all RGB channels.

How do I implement? Mode selection via a potentiometer?

• Have you looked at how `analogRead()` works yet? – Ignacio Vazquez-Abrams Mar 13 '14 at 16:37
• It seems you already have code for the 3 first pots to drive the RGB LED; it would be good to show it here so that answers can better show where to modify the current code. – jfpoilpret Mar 13 '14 at 17:19

Since you already know how to hook up a pot and read it's value, it's quite simple. You already have it hooked up in a voltage divider configuration and are getting a 10 bit (0-1023) value using `analogRead()`, you just need to decide what to do with it.

Assume `potVal` has the value of the pot.;

``````if (potVal < 256) {
// Pot is one quarter turn or less
} else if (potVal < 512) {
// Pot is between 256 and 512, so its more than quarter but less than half
} else if (potVal < 768) {
// Pot is between half and 3/4ths of the way there
} else {
// Pot is greater than 3/4 of it's full range
}
``````

So I have broken it down into four quadrants for you, but you can do it however you like.

• There should be at least a little calibration involved (for instance, if the pot being read involved is a logarithmic audio pot), but these threshold values are the best starting point. – WineSoaked Mar 16 '14 at 19:48

It would be relatively simple to do a threshold test on analogRead() to switch modes (e.g. if the value is above 512 (analogRead() gives values between 0-1023) then you switch modes (probably with a boolean variable that you can then use to decide whether to strobe the LEDs).