Project 5.03 Individually addressing Neopixels using buttons

The goal of this project is to control the left Neopixel with SW1 and the right Neopixel with SW2. The left Neopixel will turn on when SW1 Is pressed, and the right Neopixel will turn on when SW2 is pressed. They will turn off when their respective buttons are not pressed.

Project Code:

////////////////////////////////////////////////////////
// 5.03 – Individually addressing Neopixels using buttons

#include <Adafruit_NeoPixel.h>
  
byte dataPin = 10;        
byte numberOfPixels = 2;  

byte SW1 = 1;
byte SW2 = 0;

bool pressed = LOW;

byte saturation = 255;
long neoPixelOneColor = 0;        
long neoPixelTwoColor = 45000;  

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

  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
}

void loop() {
  if (digitalRead(SW1) == pressed) {                                            
    pixels.setPixelColor(0, pixels.ColorHSV(neoPixelOneColor, saturation, 255)); 
  }
  else {
    pixels.setPixelColor(0, pixels.ColorHSV(neoPixelOneColor, saturation, 0)); 
  }

  if (digitalRead(SW2) == pressed) {                                          
    pixels.setPixelColor(1, pixels.ColorHSV(neoPixelTwoColor, saturation, 255)); 

  }
  else {
    pixels.setPixelColor(1, pixels.ColorHSV(neoPixelTwoColor, saturation, 0)); 
  }

  pixels.show(); // Show the Neopixel's color
}
////////////////////////////////////////////////////////

*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.

Like previous sketches, we want to make a decision based on if the buttons are pressed or not. In order to do so, we need to use if-statements.

if (digitalRead(SW1) == pressed) {                                            

Next, we need to add the code that turns on the Left Neopixel if SW1 is pressed.

pixels.setPixelColor(0, pixels.ColorHSV(neoPixelOneColor, saturation, 255)); 

Let’s break this function down again. In the setPixelColor function there are two parameters. The first is the address of the Neopixel you’d like to target.

Neopixels are all chained together in one big line. Like most things in programming the line count starts at 0. The Neopixel on the left is first in line so it’s Neopixel number 0. The Neopixel on the right is the second in line so it’s Neopixel number 1.

So, in this case since we have passed a 0 as our first parameter in the setPixelColor function, we are addressing the Neopixel on the left.

pixels.setPixelColor(0

The second parameter of setPixelColor is the pixel’s color. In this case we use the ColorHSV function to set that.

The third parameter of the ColorHSV function is the most important here. Remember, It’s the value of the color (brightness). If we set this to 255, the color is full brightness and if we set it to 0, the Neopixel is off. So, when SW1 is pressed we want to set the Neopixel to full brightness:

    pixels.setPixelColor(0, pixels.ColorHSV(neoPixelOneColor, saturation, 255)); 
}

And when the SW1 is not pressed we want to turn the Neopixel off:

else {
  pixels.setPixelColor(0, pixels.ColorHSV(neoPixelOneColor, saturation, 0)); 
}

The same process is repeated for the Neopixel on the right, but you’ll notice that Neopixel number 1 is addressed instead:

if (digitalRead(SW2) == pressed) {                                          
  pixels.setPixelColor(1, pixels.ColorHSV(neoPixelTwoColor, saturation, 255)); 
}
else {
  pixels.setPixelColor(1, pixels.ColorHSV(neoPixelTwoColor, saturation, 0)); 
}

Then to display these colors all we need to do is call the show() function:

  pixels.show(); // Show the Neopixel's color
}
////////////////////////////////////////////////////////

And the loop repeats.

To change the color that is being displayed, change the neoPixelOneColor and neoPixelTwoColor variables. This website will give you the HSV and RGB color codes for a desired color:

https://www.rapidtables.com/web/color/color-picker.html

Previous
Previous

Project 5.04 Fading Neopixels Using Buttons

Next
Next

Project 5.02 Using ColorHSV