Project 5.03 Individually addressing Neopixels using buttons

In this project, you'll learn how to control the color of each onboard NeoPixel individually 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

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.