Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MSGEQ7Lib.h>
- #include <Wire.h>
- #include <Adafruit_PWMServoDriver.h>
- const int ADAFRUIT_BOARD_COUNT = 1;//Change to how many PCA9685 boards you have
- const int PWM_GEN_COUNT = 45;//change to how many MAXIMUM channels you wish to have, NOT sets, CHANNELS! 45-> 3 channels per set max = 15 sets.
- int MSGEQ7_DATA = 0;//pin for analog data from eq7
- int MSGEQ7_RESET = 53;//reset pin
- int MSGEQ7_STROBE = 52;//strobe pin
- int MSGEQ7_FILTER[7];//filtering array
- int MSGEQ7_AMP[7];//amplifying array
- int MSGEQ7_MIN[7];//minimum value to add amplification value
- int maxValues[7];//for tracking the maximum value when there is no audio playing - static
- uint8_t dc[256] = { // dimming table in flash memory
- 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
- 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
- 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
- 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
- 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
- 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
- 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
- 27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
- 36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
- 48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
- 63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
- 83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
- 110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
- 146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
- 193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
- };
- class Controller
- {
- private:
- Adafruit_PWMServoDriver boards[ADAFRUIT_BOARD_COUNT];
- int Sets[PWM_GEN_COUNT][3][3];
- int pwmCount;
- int MSGEQ7Values[7];
- public:
- int Musics[PWM_GEN_COUNT][3];
- int musicsCount;
- void setPWMCount(int count)
- {
- Controller::pwmCount = count;
- }
- void editChannel(int set, int channel, int msgFreq, int pwmChannel = -99)//set is the INDEX of channels, channel is the INDEX of the channels[set] array.
- {
- if (pwmChannel == -99)
- {
- Controller::pwmCount++;
- pwmChannel = Controller::pwmCount;
- }
- Controller::Sets[set][channel][2] = 1;
- Controller::Sets[set][channel][0] = pwmChannel;
- Controller::Sets[set][channel][1] = msgFreq;
- Controller::setValue(set, channel);
- }
- void Initialize()
- {
- pinMode(MSGEQ7_DATA, INPUT);
- pinMode(MSGEQ7_RESET, OUTPUT);
- pinMode(MSGEQ7_STROBE, OUTPUT);
- digitalWrite(MSGEQ7_RESET, LOW);
- digitalWrite(MSGEQ7_STROBE, HIGH);
- //Configure filter
- MSGEQ7_FILTER[0] = 75;
- MSGEQ7_FILTER[1] = 90;
- MSGEQ7_FILTER[2] = 120;
- MSGEQ7_FILTER[3] = 190;
- MSGEQ7_FILTER[4] = 425;
- MSGEQ7_FILTER[5] = 585;
- MSGEQ7_FILTER[6] = 515;
- //initialize maximum value array
- maxValues[0] = 0;
- maxValues[1] = 0;
- maxValues[2] = 0;
- maxValues[3] = 0;
- maxValues[4] = 0;
- maxValues[5] = 0;
- maxValues[6] = 0;
- //Configure amplification values
- MSGEQ7_AMP[0] = 25;
- MSGEQ7_AMP[1] = 25;
- MSGEQ7_AMP[2] = 100;
- MSGEQ7_AMP[3] = 50;
- MSGEQ7_AMP[4] = 175;
- MSGEQ7_AMP[5] = 100;
- MSGEQ7_AMP[6] = 100;
- //Configure mimimum values after filtering applied
- MSGEQ7_MIN[0] = 0;
- MSGEQ7_MIN[1] = 0;
- MSGEQ7_MIN[2] = 5;
- MSGEQ7_MIN[3] = 5;
- MSGEQ7_MIN[4] = 5;
- MSGEQ7_MIN[5] = 5;
- MSGEQ7_MIN[6] = 5;
- Controller::pwmCount = 0;
- for (int i = 0;i < PWM_GEN_COUNT + 1;i++)
- {
- for (int a = 0; a < 3;a++)
- {
- Controller::Sets[i][a][2] = 0;
- Controller::Sets[i][a][0] = Controller::pwmCount++;
- Controller::Sets[i][a][1] = 4;
- Controller::pwmCount++;
- }
- }
- for (int i = 0; i < ADAFRUIT_BOARD_COUNT; i++)
- {
- Controller::boards[i] = Adafruit_PWMServoDriver(0x40 + i);
- Controller::boards[i].begin();
- Controller::boards[i].setPWMFreq(1000);
- }
- Controller::musicsCount = 0;
- }
- int getPWMChannel(int set, int channel)
- {
- if (Controller::Sets[set][channel][2] == 1) {
- return Controller::Sets[set][channel][0];
- }
- else
- {
- return -1;
- }
- }
- int getMsgFreq(int set, int channel)
- {
- if (Controller::Sets[set][channel][2] == 1)
- {
- return Controller::Sets[set][channel][1];
- }
- else
- {
- return -1;
- }
- }
- void setValue(int set, int channel, int value = 0)
- {
- if (Controller::Sets[set][channel][2] == 1)
- {
- int pwmChannel = Controller::Sets[set][channel][0];
- if (pwmChannel <= 13)
- {
- analogWrite(pwmChannel, value);
- }
- else
- {
- Controller::boards[((pwmChannel - 14) / 16)].setPWM(pwmChannel - 14, 0, map(value, 0, 255, 0, 4095));
- }
- }
- }
- void disableChannel(int set, int channel)
- {
- Controller::Sets[set][channel][2] = 0;
- }
- void stopMusic(int set, int channel)
- {
- //Controller::Musics.removeNode(set, channel);
- int index;
- for (int i = 0; i < Controller::musicsCount; i++)
- {
- if (Controller::Musics[i][0] == set && Controller::Musics[i][1] == channel)
- {
- index = i;
- break;
- }
- }
- for (int d = index; d < Controller::musicsCount; d++)
- {
- Controller::Musics[d][0] = Controller::Musics[d + 1][0];
- Controller::Musics[d][1] = Controller::Musics[d + 1][1];
- Controller::Musics[d][2] = Controller::Musics[d + 1][2];
- }
- Controller::musicsCount--;
- }
- void startMusic(int set, int channel)
- {
- for (int i = 0; i < Controller::musicsCount; i++)
- {
- if (Controller::Musics[i][0] == set && Controller::Musics[i][1] == channel)
- {
- return;
- }
- }
- Controller::Musics[Controller::musicsCount][0] = set;
- Controller::Musics[Controller::musicsCount][1] = channel;
- Controller::Musics[Controller::musicsCount][2] = Controller::Sets[set][channel][1];
- Controller::musicsCount++;
- }
- void debugMusics()
- {
- Serial.println("------------------------");
- for (int i = 0; i < Controller::musicsCount; i++)
- {
- Serial.print("Set: ");
- Serial.print(Controller::Musics[i][0]);
- Serial.print(" | Channel: ");
- Serial.print(Controller::Musics[i][1]);
- Serial.print(" | MSGFreq: ");
- Serial.println(Controller::Musics[i][2]);
- }
- Serial.println("------------------------");
- }
- void updateMusics()
- {
- //Controller::MSGEQ7IC.Read(MSGEQ7IC.spectrumValues);
- digitalWrite(MSGEQ7_RESET, HIGH);
- digitalWrite(MSGEQ7_RESET, LOW);
- for (int i = 0; i < 7;i++)
- {
- digitalWrite(MSGEQ7_STROBE, LOW);
- delayMicroseconds(30);
- Controller::MSGEQ7Values[i] = analogRead(MSGEQ7_DATA);
- Controller::MSGEQ7Values[i] = constrain(Controller::MSGEQ7Values[i], MSGEQ7_FILTER[i], 1023);
- Controller::MSGEQ7Values[i] = map(Controller::MSGEQ7Values[i], MSGEQ7_FILTER[i], 1023, 0, 255);
- if (Controller::MSGEQ7Values[i] > maxValues[i])
- maxValues[i] = MSGEQ7Values[i];
- if (Controller::MSGEQ7Values[i] > MSGEQ7_MIN[i])
- Controller::MSGEQ7Values[i] += MSGEQ7_AMP[i];
- Controller::MSGEQ7Values[i] = constrain(Controller::MSGEQ7Values[i], 0, 255);
- Serial.print(Controller::MSGEQ7Values[i]);
- Serial.print(" ");
- digitalWrite(MSGEQ7_STROBE, HIGH);
- }
- Serial.println();
- for (int i = 0; i < Controller::musicsCount; i++)
- {
- Controller::setValue(Controller::Musics[i][0], Controller::Musics[i][1], dc[Controller::MSGEQ7Values[Controller::Musics[i][2]]]);
- }
- }
- Controller()
- {
- pwmCount = 0;
- }
- };
- Controller LEDs;
- void setup()
- {
- /* add setup code here */
- Serial.begin(250000);
- analogReference(DEFAULT);
- delay(100);
- Serial.println("Beginning initialization.");
- delay(100);
- LEDs.setPWMCount(2);
- LEDs.Initialize();
- delay(100);
- Serial.println("Completed initialization.");
- delay(100);
- /*LEDs.debugMusics();
- LEDs.startMusic(0, 0);
- LEDs.debugMusics();
- LEDs.startMusic(1, 0);
- LEDs.startMusic(2, 0);
- LEDs.startMusic(5, 2);
- LEDs.startMusic(5, 2);
- LEDs.debugMusics();
- LEDs.stopMusic(0, 0);
- LEDs.stopMusic(2, 0);
- LEDs.debugMusics();*/
- /*LEDs.editChannel(0, 0, 0, 2);
- LEDs.editChannel(0, 1, 3, 3);
- LEDs.editChannel(0, 2, 5, 4);
- LEDs.editChannel(1, 0, 0, 5);
- LEDs.editChannel(1, 1, 3, 6);
- LEDs.editChannel(1, 2, 5, 7);
- LEDs.editChannel(2, 0, 0, 8);
- LEDs.editChannel(2, 1, 3, 9);
- LEDs.editChannel(2, 2, 5, 10);
- LEDs.editChannel(3, 0, 0, 11);
- LEDs.editChannel(3, 1, 3, 12);
- LEDs.editChannel(3, 2, 5, 13);
- LEDs.startMusic(0, 2);
- LEDs.startMusic(0, 3);
- LEDs.startMusic(0, 4);
- LEDs.startMusic(1, 5);
- LEDs.startMusic(1, 6);
- LEDs.startMusic(1, 7);
- LEDs.startMusic(2, 8);
- LEDs.startMusic(2, 9);
- LEDs.startMusic(2, 10);
- LEDs.startMusic(3, 11);
- LEDs.startMusic(3, 12);
- LEDs.startMusic(3, 13);*/
- /*LEDs.setValue(0, 0, 255);
- LEDs.setValue(0, 1, 255);
- LEDs.setValue(0, 2, 255);
- LEDs.setValue(1, 0, 255);
- LEDs.setValue(1, 1, 255);
- LEDs.setValue(1, 2, 255);
- LEDs.setValue(2, 0, 255);
- LEDs.setValue(2, 1, 255);
- LEDs.setValue(2, 2, 255);
- LEDs.setValue(3, 0, 255);
- LEDs.setValue(3, 1, 255);
- LEDs.setValue(3, 2, 255);*/
- LEDs.editChannel(0, 0, 2, 3);
- LEDs.editChannel(0, 1, 4, 2);
- LEDs.editChannel(0, 2, 0, 4);
- LEDs.startMusic(0, 0);
- LEDs.startMusic(0, 1);
- LEDs.startMusic(0, 2);
- //LEDs.setValue(0, 1, 255);
- LEDs.editChannel(1, 0, 2, 5);
- LEDs.editChannel(1, 1, 4, 6);
- LEDs.editChannel(1, 2, 0, 7);
- LEDs.startMusic(1, 0);
- LEDs.startMusic(1, 1);
- LEDs.startMusic(1, 2);
- //LEDs.setValue(1, 0, 255);
- //LEDs.setValue(0, 0, 0);
- //LEDs.setValue(0, 1, 0);
- //LEDs.setValue(0, 2, 0);
- //LEDs.setValue(1, 0, 0);
- //LEDs.setValue(1, 1, 0);
- //LEDs.setValue(1, 2, 0);
- Serial.println("Config Loaded!");
- Serial.println();
- }
- void loop()
- {
- //if command -> disable specific channel on MUSIC_LEDs
- //TODO: Write serial command handling
- //else update music
- boolean t = false;
- if (t)
- {
- }
- else
- {
- LEDs.updateMusics();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement