I updated @auct's code to include the built in button as well:
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 3
#define PIXEL_PIN 2
#define NUM_PIXELS 1
Adafruit_NeoPixel pixel(NUM_PIXELS, PIXEL_PIN, NEO_GRBW + NEO_KHZ400);
bool currentButtonPressed = false;
void setup()
{
// set up serial out
Serial.begin(115200);
// set up neopixel
pixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixel.clear(); // Set pixel colors to 'off'
pixel.show();
// set up GPIO
pinMode(BUTTON_PIN, INPUT_PULLUP);
// initial button pressed state
setPixelToCurrentButtonState();
}
void loop()
{
// read button state (LOW when pressed)
bool newButtonPressed = digitalRead(BUTTON_PIN) == LOW;
setPixelStateIfChanged(newButtonPressed);
delay(10); // debounce
}
void setPixelStateIfChanged(bool newButtonPressed) {
if (newButtonPressed != currentButtonPressed) {
currentButtonPressed = newButtonPressed;
setPixelToCurrentButtonState();
}
}
void setPixelToCurrentButtonState() {
if (currentButtonPressed) {
Serial.println("turning LED blue");
pixel.setPixelColor(0, pixel.Color(0, 0, 128));
pixel.show();
} else {
Serial.println("turning LED red");
pixel.setPixelColor(0, pixel.Color(128, 0, 0));
pixel.show();
}
}