#include <FastLED.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold12pt7b.h>
// Pin Definitions
#define LED_PIN 10
#define BUZZER_PIN 11
#define NUM_LEDS 36
#define BRIGHTNESS 255
// Matrix Pins
const int rowPins[7] = {0, 2, 3, 4, 5, 6, 7};
const int colPins[6] = {A3, A2, A1, A0, 13, 12};
// OLED Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Colors and Tones
CRGB row7Colors[6] = {CRGB::Red, 0xFFA500, CRGB::Yellow, CRGB::Green, CRGB::Blue, CRGB::Magenta};
const char* colorNames[6] = {"RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "MAGENTA"};
const int colorTones[6] = {440, 494, 523, 587, 659, 698}; // A4 to F4 notes
CRGB leds[NUM_LEDS];
CRGB currentColor = CRGB::Red;
const char* currentColorName = "RED";
void updateDisplay(const char* name) {
display.clearDisplay();
display.setFont(&FreeSansBold12pt7b);
display.setTextColor(SSD1306_WHITE);
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(name, 0, 0, &x1, &y1, &w, &h);
display.setCursor((SCREEN_WIDTH - w) / 2, (SCREEN_HEIGHT + h) / 2);
display.print(name);
display.display();
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
if(display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
updateDisplay(currentColorName);
}
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
for (int i = 0; i < 7; i++) pinMode(rowPins[i], INPUT);
for (int i = 0; i < 6; i++) pinMode(colPins[i], INPUT_PULLUP);
}
void loop() {
bool changed = false;
for (int r = 0; r < 7; r++) {
pinMode(rowPins[r], OUTPUT);
digitalWrite(rowPins[r], LOW);
delayMicroseconds(50);
for (int c = 0; c < 6; c++) {
if (digitalRead(colPins[c]) == LOW) {
if (r < 6) { // LED Buttons
int ledIndex = (r * 6) + c;
if (ledIndex < NUM_LEDS) {
leds[ledIndex] = currentColor;
changed = true;
tone(BUZZER_PIN, 1000, 20); // Short "click" sound for LEDs
}
}
else { // Color Selection Buttons
if (strcmp(currentColorName, colorNames[c]) != 0) {
currentColor = row7Colors[c];
currentColorName = colorNames[c];
updateDisplay(currentColorName);
tone(BUZZER_PIN, colorTones[c], 150); // Melodic beep for color change
}
}
delay(200);
}
}
pinMode(rowPins[r], INPUT);
}
if (changed) FastLED.show();
}