#include <FastLED.h>
// FASTLED
#define NUM_LEDS 256
#define DATA_PIN 13
#define MAX_BRIGHTNESS 200
#define MIN_BRIGHTNESS 50
#define FRAMES_PER_SECOND 60
#define ROWS 16
#define COLS 16
uint16_t keys[ROWS][COLS];
uint16_t leds[ROWS][COLS];
uint8_t colPins[COLS] = { 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 }; // Pins connected to R1, R2, R3, R4
CRGB led[NUM_LEDS];
uint8_t hue = 0;
//-------------------------------------------------------------
void putLeds(){
int pos = 0;
for (int x=0; x < COLS; x++) {
for (int y=0; y < ROWS; y++) {
if(leds[x][y] == 1) {
led[pos++] = CRGB::Red;
} else {
led[pos++] = CRGB(238, 255, 0);
}
}
}
FastLED.show();
}
//------------------------------------------------------------
uint16_t getKey() {
uint16_t k = -1;
// Reset all
for (uint8_t x=0; x < COLS; x++) { digitalWrite(colPins[x], HIGH); }
//Bit schreiben
for (uint8_t x=0; x < COLS; x++) {
digitalWrite(colPins[x], LOW); // Bit setzen
//Bit lesen
for (uint8_t y=0; y < ROWS; y++) {
uint16_t key = digitalRead(rowPins[y]);
if (key == LOW) {
k = keys[x][y];
leds[x][y] = 1;
}
}
digitalWrite(colPins[x], HIGH); // Bit rücksetzen
}
return k;
}
//----------------------------------------------------------------
void clearLeds() {
for (uint8_t x=0; x<COLS; x++) {
for (uint8_t y=0; y<ROWS; y++) {
leds[x][y] = 0;
}
}
}
void rainbow() {
for (int i = 0; i < NUM_LEDS; ++i) {
led[i] = CHSV(hue + (i * 10), 255, 255);
}
//You can change the pattern speed here
EVERY_N_MILLISECONDS(15){
hue++;
}
FastLED.show();
}
//----------------------------------------------------------------
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(led, NUM_LEDS);
FastLED.setBrightness(MAX_BRIGHTNESS);
// Init Array
// 0 1 2 3
// 7 6 5 4
// 8 9
/*
boolean sw = true;
int value = 0;
for (uint8_t x=0; x<COLS; x++) {
for (uint8_t y=0; y<ROWS; y++) {
if(sw == true) {
sw = !sw;
keys[x][y] = value++;
} else {
sw = !sw;
keys[x][y] = pos;
}
}
}
*/
for (uint8_t i=0; i<COLS; i++) { pinMode(colPins[i] , OUTPUT); }
for (uint8_t i=0; i<ROWS; i++) { pinMode(rowPins[i] , INPUT_PULLUP); }
clearLeds();
}
void loop() {
uint16_t key;
key = getKey();
if (key != -1) {
Serial.println(key);
}
rainbow();
//putLeds();
delay(200);
}