// *** Thank : https://wokwi.com/projects/298603580442542605
#include <FastLED.h> //*******************************
#define DATA_PIN 3
#define NUM_ROWS 16
#define NUM_COLS 1
#define BRIGHTNESS 255
#define NUM_LEDS NUM_ROWS * NUM_COLS
const int sampleWindow = 120; // Sample window width in mS (50 mS = 20Hz) 50***********
const int maxScale = 16; //*******hi
const int redZone = 11; //*******mid
const int yellowZone = 4; //*******low
const int direct = 1; // 1 or 0 RGB UP/DN
unsigned int sample;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // Peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0); // pin A0
Serial.println(sample);
if (sample < 1024) // Toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // Save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // Save just the min levels
}
}
}
peakToPeak = signalMax - signalMin;
// Map 1v p-p level to the max scale of the display
int displayPeak = map(peakToPeak, 0, 1023, 0, maxScale);
// Draw the new sample
if (direct == 1){ // RGB = UP
for (int i = maxScale - 1; i >= 0; i--) // (int i = maxScale - 1; i >= 0; i--) //(int i = 0; i <= maxScale-1; i++) //
{
if (i >= displayPeak) // Blank these pixels *************************>=
{
leds[i] = CRGB::Black; //*********leds[ i] = CRGB::Black;
}
else if (i >= redZone) // Draw in red
{
leds[i] = CRGB::Red;
}
else if ((i >= yellowZone) && (i < redZone)) // Draw in yellow
{
leds[i] = CRGB::Yellow;
}
else if (i < yellowZone) // Draw in green
{
leds[i] = CRGB::Green;
}
}
}
if (direct == 0){ // RGB = DN
for (int i = maxScale - 1 ; i >= 0; i--) // (int i = maxScale - 1; i >= 0; i--) //(int i = 0; i <= maxScale-1; i++) //
{
if (i >= displayPeak) // Blank these pixels *************************>=
{
leds[NUM_ROWS-1-i] = CRGB::Black; //*********leds[ i] = CRGB::Black;
}
else if (i >= redZone) // Draw in red
{
leds[NUM_ROWS-1-i] = CRGB::Red;
}
else if ((i >= yellowZone) && (i < redZone)) // Draw in yellow
{
leds[NUM_ROWS-1-i] = CRGB::Yellow;
}
else if (i < yellowZone) // Draw in green
{
leds[NUM_ROWS-1-i] = CRGB::Green;
}
}
}
FastLED.show(); // Write the changes we just made to the display
}