Neopixel Farbe zuweisen bei Tastendruck

Die Zuordnung kann man mit einem eigenen Feld machen. Außerdem habe ich delay rausgeworfen und mich hemmungslos bei #8 bedient:

#include <SimpleKeypad.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 20
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const byte nb_rows = 4;                         // four rows
const byte nb_cols = 5;                         // four columns
const char key_chars[nb_rows][nb_cols] = {      // The symbols of the keys
  {'1', '2', '3', '4', '5'},
  {'6', '7', '8', '9', 'A'},
  {'B', 'C', 'D', 'E', 'F'},
  {'G', 'H', 'I', 'J', 'K'}
};
const byte led_pos[nb_rows][nb_cols] = {
  { 0,  9, 10, 19,  1},
  { 8, 11, 18,  2,  7},
  {12, 17,  3,  6, 13},
  {16,  4,  5, 14, 15}
};
const uint32_t WEISS = 0xFFFFFF, AKTIV = 0x00FFFF;
byte rowPins[nb_rows] = {2, 3, 4, 5};
byte colPins[nb_cols] = {7, 8, 9, 10, 11};

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 j = 0; j < NUMPIXELS; j++) {
    pixels.setPixelColor(j, WEISS);
  }
  pixels.show();
}

void loop() {
  uint32_t jetzt = millis();
  static uint32_t vorhin = 0;
  const uint32_t ENTPRELL_INTERVALL = 100;
  if (jetzt - vorhin >= ENTPRELL_INTERVALL) {
    char key = kp1.getKey();                      // The getKey function scans the keypad every 10 ms and returns a key only one time, when you start pressing it
    if (key) {                                    // If getKey returned any key
      vorhin = jetzt;
      Serial.println(key);
      for (byte r = 0; r < nb_rows; r++) {
        for (byte c = 0; c < nb_cols; c++) {
          if ( key == key_chars[r][c] ) {
            if (pixels.getPixelColor(led_pos[r][c]) != AKTIV) {
              pixels.setPixelColor(led_pos[r][c], AKTIV);
            } else {
              pixels.setPixelColor(led_pos[r][c], WEISS);
            }
          }
        }
      }
      pixels.show(); // Durchführen der Pixel-Ansteuerung
    }
  }
}

Getestet mit Keypad.h.


Ich wollte nicht in Konkurrenz treten, bin nur langsamer :innocent:

1 Like