//7 BAND SPECTRUM ANALYZER MSGE07
//DESIGN PLATINUM 2019 www.youtube.com/c/PLATINUMKIT
//FACEBOOK https://www.facebook.com/LegendMasters/
//YOUTUBE VIDEO DIY ACRYLIC PART.1 https://youtu.be/xDKbpwumgF8
//YOUTUBE VIDEO DIY ACRYLIC PART.2
//YOUTUBE VIDEO DIY INFINITY MIRROR https://youtu.be/j2-0d9xBDJk
//THIS IS NOT FOR SALE ONLY FOR HOBBIES.
//WE SHARE TO ADD A KNOWLEDGE SCIENCE.
#include <Arduino.h>
#include <math.h>
#include <Adafruit_NeoPixel.h>
#include "MSGEQ7.h"
Timer t;
#define Acolor A0
#define Scolor A6
#define Pcolor A7
#define MAX_BRIGHTNESS 255
#define MIN_BRIGHTNESS 20
const int brightnessInPin = A2;
int led = 5;
unsigned long x;
unsigned long y = 0;
const byte analogPin = A1;
const byte strobePin = 3;
const byte resetPin = 2;
const byte dataPin = 4;
const byte numBand = 20; //number of LEDs
const byte numTop = 0;
const int noise[] = {20, 20, 20, 20, 20, 20, 20};
const float gain[] = {2.05, 2.05, 2.05, 2.05, 2.05, 2.05, 2.05}; //Frequency GAIN
//Frequency = 63Hz, 160Hz, 400Hz, 1kHz, 2.5kHz, 6.25kHz, 16kHz
const unsigned long dlay = 30;
void CReSet();
void readBand(byte band);
void anyBand(byte band);
void peakLower();
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numBand * 7, dataPin, NEO_GRB + NEO_KHZ800);
enum audio
{
MONO,
RIGHT,
LEFT
};
int spectrumReadR;
int spectrumReadL;
int audio = MONO;
int mag = 0;
int numON = 0;
float fl_mag = 0.0;
int k;
int a;
int s;
int peakArray[7];
byte
peak = 0,
dotCount = 0;
void setup()
{
Serial.begin(9600);
pinMode(resetPin, OUTPUT);
pinMode(strobePin, OUTPUT);
pinMode(dataPin, OUTPUT);
strip.begin();
strip.show();
digitalWrite(resetPin, HIGH);
delayMicroseconds(5);
digitalWrite(strobePin, HIGH);
delayMicroseconds(50);
digitalWrite(strobePin, LOW);
delayMicroseconds(50);
digitalWrite(resetPin, LOW);
delayMicroseconds(5);
digitalWrite(strobePin, HIGH);
delayMicroseconds(100);
t.every(80, peakLower);
}
void loop()
{
CReSet();
x = millis();
}
int dimH = 0;
int p = 0;
const long interval = 1;
void CReSet()
{
if (x - y >= interval)
{
y = x;
dimH = map(p, 0, 4000, 0, 255);
analogWrite(led, dimH);
if (p >= 4000)
{
p = 0;
}
else
{
p = p + 1;
}
}
int mappedValue = map(analogRead(brightnessInPin), 0, 1023, 0, 255);
strip.setBrightness(constrain(mappedValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS));
a = analogRead(Acolor);
a = map(a, 0, 1023, 0, 400);
s = analogRead(Scolor);
s = map(s, 0, 1023, 35, 1500);
k = analogRead(Pcolor);
k = map(k, 0, 1023, 0, 750);
for (byte band = 1; band <= 7; band++)
{
digitalWrite(strobePin, LOW);
delayMicroseconds(40);
spectrumReadL = analogRead(analogPin);
digitalWrite(strobePin, HIGH);
mag = (spectrumReadR + spectrumReadL) / 2;
mag = max(0, (mag - noise[band - 1]));
fl_mag = gain[band - 1] * float(mag);
numON = map(fl_mag, 0, 1024, 1, numBand + 1);
anyBand(band);
if (peakArray[band - 1] == 0)
strip.setPixelColor(peakArray[band - 1] + numBand * (band - 1), strip.Color(0, 0, 0));
else
strip.setPixelColor(peakArray[band - 1] + numBand * (band - 1), Wheel(map(10, 0, numBand - 0, k, 255)));
t.update();
}
strip.show();
delay(dlay);
}
void readBand(byte band)
{
for (byte band = 1; band <= 7; band++)
{
digitalWrite(strobePin, LOW);
delayMicroseconds(40);
spectrumReadL = analogRead(analogPin);
digitalWrite(strobePin, HIGH);
mag = (spectrumReadR + spectrumReadL) / 2;
mag = max(0, (mag - noise[band - 1]));
fl_mag = gain[band - 1] * float(mag);
numON = map(fl_mag, 0, 1024, 1, numBand + 1);
anyBand(band);
}
}
void anyBand(byte band)
{
for (byte i = 0; i < numBand; i++)
{
if (i < (numON - numTop - 1))
{
strip.setPixelColor(i + numBand * (band - 1), Wheel(map(i, 0, numBand - s, a, 255)));
}
else if (i >= numON)
{
strip.setPixelColor(i + numBand * (band - 1), strip.Color(0, 0, 0));
}
else
{
if (i > peakArray[band - 1])
peakArray[band - 1] = i;
}
}
}
void peakLower()
{
for (byte i = 0; i < 7; i++)
{
if (peakArray[i] > 1)
peakArray[i]--;
else
continue;
}
}
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if (WheelPos < 85)
{
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170)
{
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}