Neopixel Farbe zuweisen bei Tastendruck

Ach stimmt - versuch mal den:

#include <SimpleKeypad.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 20
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int pause = 100; // 100 Millisekunden Pause bis zur Ansteuerung der nächsten LED.

const byte nb_rows = 4;                         // four rows
const byte nb_cols = 5;                         // four columns
char key_chars[nb_rows][nb_cols] =              // The symbols of the keys
{
  {1, 2, 3, 4, 5},
  {6, 7, 8, 9, 10},
  {11, 12, 13, 14, 15},
  {16, 17, 18, 19, 20}
};
byte rowPins[nb_rows] = {2, 3, 4, 5};
byte colPins[nb_cols] = {7, 8, 9, 10, 11};

uint32_t pinColor[] =
{
  pixels.Color(0, 255, 255), pixels.Color(255, 255, 0), pixels.Color(0, 255, 0), pixels.Color(255, 0, 255)
};
SimpleKeypad kp1((char *)key_chars, rowPins, colPins, nb_rows, nb_cols);  // New keypad called kp1

void setup()
{
  Serial.begin(9600);
  Serial.println("Press any key on the keypad and it will show up here :");
  pixels.begin(); // Initialisierung der NeoPixel
  for (byte b = 0; b < 20; b++)
  {
    pixels.setPixelColor(b, pixels.Color(255, 255, 255));
  }
}
void loop()
{
  char key = kp1.scan();
  if (key)
  {
    //Serial.println(key);
    if (pixels.getPixelColor(key - 1) != pinColor[key - 1])
    {pixels.setPixelColor(key - 1, pinColor[key - 1]); }
    else
    {pixels.setPixelColor(key - 1, pixels.Color(255, 255, 255));}
    pixels.show();
    delay(pause);
  }
}

(NACHTRAG Ich hab das loop() nopchmal geändert - da fehlte das show() ;( )
Da ist auch drin, wie die am Anfang alle gestellt werden :wink:

1 Like