Project 6.01 Changing Color with the Potentiometer

This project will demonstrate the use of a potentiometer to control the color of the Neopixels. As the potentiometer rotates, the color of the Neopixels will change.

Project Code:

/////////////////////////////////////////////////////
// 6.01 - Changing Color with Potentiometer

#include <Adafruit_NeoPixel.h>

byte value = 255;
byte saturation = 255;

byte dataPin = 10;
byte numberOfPixels = 2;
byte brightness = 10;

byte potPin = A0;

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  Serial.begin(9600);

  pinMode(potPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);
}

void loop() {

  int potValue = analogRead(potPin);
  long colorValue = map(potValue, 0, 1023, 0, 65535);

  pixels.setPixelColor(0, pixels.ColorHSV(colorValue, saturation, value));
  pixels.setPixelColor(1, pixels.ColorHSV(colorValue, saturation, value));
  pixels.show();

}
/////////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

 After declaring variables, in the setup() function Serial is initialized, the pinMode of the potPin is set, the Neopixels are initialized and their brightness is set.

void setup() {
  Serial.begin(9600);

  pinMode(potPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);
}

In the loop() function, an int variable called potValue is created to hold the analog value read from the pot.

int potValue = analogRead(potPin);

At this point we have a problem. We have a potentiometer value somewhere between 0 and 1023 (10-bit) but our color range goes from 0 – 65535 (16-bit). This is where the map function comes in. The map function allows us to map two value ranges together linearly. This is much less scary than it sounds. Essentially, it will make 0 on the potentiometer equal 0 on the color range, and 1023 on the potentiometer equal 65535 on the color range. For example, if the potentiometer value read was 543 the map function would return a value of 34785.

We can check this by using the equation:

1.)    (potValue * MaxHueValue) / MaxPotValue = MappedValue

2.)    (543 * 65535)/1023 = 34,785

The map function takes five parameters. It takes the value to map, the min value of that range, the max value of that range, the min value of the range to be mapped to, and the max value of the range to be mapped to. See the table below:

Parameters Value
Value to map analogValue read from pot
Min value of that range 0
Max value of that range 1023
Min value of the range to be mapped to 0
Max value of the range to be mapped to 65535
long colorValue = map(potValue, 0, 1023, 0, 65535);

Lastly, the value that was mapped is written to the Neopixels and shown.

pixels.setPixelColor(0, pixels.ColorHSV(colorValue, saturation, value));
pixels.setPixelColor(1, pixels.ColorHSV(colorValue, saturation, value));
pixels.show();
Previous
Previous

Project 7.00 Using the OLED

Next
Next

Project 6.00 Using the Potentiometer