#include <Adafruit_NeoPixel.h>
// Grid dimensions
const int NUM_COLS = 9; // Number of columns in the grid (x-axis)
const int NUM_ROWS = 5; // Number of rows in the grid (y-axis)
const int NUM_LEDS = NUM_COLS * NUM_ROWS; // Total number of LEDs
// Pin definitions
#define LED_PIN 6 // Digital pin connected to NeoPixel DIN
#define MIC_PIN A0 // Analog pin reading the amplified microphone signal
// Create the NeoPixel object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int waveform[NUM_COLS];
int XY(int x, int y) {
int row = (NUM_ROWS - 1) - y;
return row * NUM_COLS + x;
}
void setup() {
strip.begin();
strip.show(); // Clear all pixels initially
Serial.begin(115200);
for (int i = 0; i < NUM_COLS; i++) {
waveform[i] = 1;
}
}
void loop() {
int micValue = analogRead(MIC_PIN);
Serial.println(micValue); // debug
int deviation = micValue - 512;
int y = map(deviation, -512, 511, 0, NUM_ROWS - 1);
for (int x = 0; x < NUM_COLS - 1; x++) {
waveform[x] = waveform[x + 1];
}
waveform[NUM_COLS - 1] = y;
strip.clear();
for (int x = 0; x < NUM_COLS; x++) {
int row = waveform[x];
int pixelIndex = XY(x, row);
strip.setPixelColor(pixelIndex, strip.Color(0, 255, 0));
}
strip.show();
delay(30);
}