How do you want to use these buttons in your project? With a bank of blinky buttons, the sky is the limit.
- Control the colors or properties of the buttons themselves
- Control other LED arrays in the same project
- Trigger sound events
- Send someone an SMS message with Fona
- Trigger a web based event with a Feather Wifi
Here is one example of how to wire 7 buttons to an Adafruit Metro Mini.
In this example, the data line is wired to pin 2 on the Metro Mini, and the buttons are wired to pins 3-9. Pins 0 and 1 are left available for serial connections or debugging.
Sample Code
Here is some sample code that shows one simple way to address all 7 buttons. In this code, the buttons are running a color palette-based gradient animation, and the animation's color changes whenever you press a button.
Install the FastLED Library
FastLED is a very powerful tool for creating all kinds of LED lighting patterns and effects. Learn more and find lots of yummy code samples at the FastLED Google Plus community page.
Use the Library Manager in the Arduino IDE to install the FastLED library (Sketch→Include Library→Manage Libraries…).
Once you've got the library installed, select your Arduino board from the Tools→Board menu and upload the code.
#include <FastLED.h> // Because conditional #includes don't work w/Arduino sketches... //#include <SPI.h> // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET #include <avr/power.h> // ENABLE THIS LINE FOR GEMMA OR TRINKET #define DATA_PIN 2 #define COLOR_ORDER GRB #define NUM_LEDS 7 #define DEBOUNCE 10 // button debouncer, how many ms to debounce, 5+ ms is usually plenty int HUE=15; int SATURATION=255; int BRIGHTNESS=150; int SPEEDO=10; int STEPS=25; CRGB leds[NUM_LEDS]; TBlendType currentBlending; CRGBPalette16 currentPalette; // here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc byte buttons[] = {3,4,5,6,7,8,9}; // This handy macro lets us determine how big the array up above is, by checking the size #define NUMBUTTONS sizeof(buttons) // we will track if a button is just pressed, just released, or 'currently pressed' byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS]; void setup() { byte i; // Make input & enable pull-up resistors on switch pins for (i=0; i<NUMBUTTONS; i++){ pinMode(buttons[i], INPUT_PULLUP); } // pin13 LED pinMode(13, OUTPUT); //FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA); FastLED.addLeds<WS2812B,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); } void loop() { Gradient(); digitalWrite(13, LOW); check_switches(); // when we check the switches we'll get the current state for (byte i = 0; i<NUMBUTTONS; i++){ if (pressed[i]) { digitalWrite(13, HIGH); // is the button pressed down at this moment } if (justreleased[i]) { if (i == 0){ HUE=190; }else if (i == 1){ HUE=170; }else if (i == 2){ HUE=140; }else if (i == 3){ HUE=100; }else if (i == 4){ HUE=70; }else if (i == 5){ HUE=30; }else if (i == 6){ HUE=0; } for (byte i=0; i<NUMBUTTONS; i++){ // remember, check_switches() will necessitate clearing the 'just pressed' flag justpressed[i] = 0; } } } } void check_switches() { static byte previousstate[NUMBUTTONS]; static byte currentstate[NUMBUTTONS]; static long lasttime; byte index; if (millis() < lasttime){ // we wrapped around, lets just try again lasttime = millis(); } if ((lasttime + DEBOUNCE) > millis()) { // not enough time has passed to debounce return; } // ok we have waited DEBOUNCE milliseconds, lets reset the timer lasttime = millis(); for (index = 0; index<NUMBUTTONS; index++){ // when we start, we clear out the "just" indicators justreleased[index] = 0; currentstate[index] = digitalRead(buttons[index]); // read the button if (currentstate[index] == previousstate[index]) { if ((pressed[index] == LOW) && (currentstate[index] == LOW)) { // just pressed justpressed[index] = 1; } else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) { // just released justreleased[index] = 1; } pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed } //Serial.println(pressed[index], DEC); previousstate[index] = currentstate[index]; // keep a running tally of the buttons } } // GRADIENT -------------------------------------------------------------- void Gradient() { SetupGradientPalette(); static uint8_t startIndex = 0; startIndex = startIndex + 2; // motion SPEEDO FillLEDsFromPaletteColors( startIndex); FastLED.show(); FastLED.delay(SPEEDO); } void SetupGradientPalette() { CRGB light = CHSV( HUE + 5, SATURATION - 15, BRIGHTNESS); CRGB light1 = CHSV( HUE + 10, SATURATION - 10, BRIGHTNESS); CRGB light2 = CHSV( HUE + 15, SATURATION - 20, BRIGHTNESS); CRGB medium = CHSV ( HUE - 3, SATURATION, BRIGHTNESS); CRGB medium1 = CHSV ( HUE - 7, SATURATION, BRIGHTNESS); CRGB medium2 = CHSV ( HUE - 11, SATURATION, BRIGHTNESS); CRGB dark = CHSV( HUE + 3, SATURATION - 30, BRIGHTNESS); CRGB dark1 = CHSV( HUE, SATURATION - 20, BRIGHTNESS); CRGB dark2 = CHSV( HUE -3, SATURATION - 15, BRIGHTNESS); currentPalette = CRGBPalette16( light, light1, light2, light1, medium, medium1, medium2, medium1, dark, dark1, dark2, dark1, medium, medium1, medium2, medium1 ); } void FillLEDsFromPaletteColors( uint8_t colorIndex) { uint8_t brightness = BRIGHTNESS; for( int i = 0; i < NUM_LEDS; i++) { leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); colorIndex += STEPS; } }
Having a button array is a very powerful, effective and interactive way to control your project. What will you control?
Page last edited June 27, 2016
Text editor powered by tinymce.