Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h> // Подключаем библиотеку FastLED для управления LED лентой
- #define NUM_LEDS 210 // Общее число светодиодов
- #define DATA_PIN 8 // Пин подключения LED ленты
- CRGB leds[NUM_LEDS]; // Массив для управления LED лентой
- // Количество барабасов
- const int drums = 3;
- // Подключаем устройства к пинам ардуино
- const int pin__ModeChangeBtn = 2;
- const int pin__OnOffBtn[drums] = {3, 4, 5};
- const int pin__ColorSlider[drums] = {A0, A1, A2};
- const int pin__FadeSlider = A3;
- const int pin__SoundSensor[drums] = {A4, A5, A6};
- // Разметка ленты
- const int startPart[drums] = {30, 90, 150};
- const int partLength = 60;
- // Переменные
- int currentMode = 0; // Текущий режим
- float fadeStep = getFadestepFromSlider(); // Скорость затемнения после удара
- int thresholdLevel[drums] = {50,50,50}; // Порог срабатывания датчиков удара
- class Led {
- public:
- bool isActive;
- int currentColor;
- int neededColor;
- int saturation;
- int interfaceSaturation;
- int brightness;
- int interfaceBrightness;
- int thresholdLevel;
- };
- Led drumLed[drums];
- for (int i = 0; i < drums; i++){
- drumLed[i].currentColor = 0;
- drumLed[i].neededColor = getNeededColor(i);
- drumLed[i].saturation = 255;
- drumLed[i].brightness = 255;
- drumLed[i].isActive = true;
- drumLed[i].interfaceSaturation = 255;
- drumLed[i].interfaceBrightness = 255;
- drumLed[i].thresholdLevel = thresholdLevel[i];
- }
- class ChangeInterface {
- private:
- int m_isActive[drums] = {0,1,2},
- int m_colors[drums] = {0,1,2};
- int m_mode[3] = {4,5,6};
- int m_fade[7] = {8,9,10,11,12,13,14};
- void isActive(int m_isActive){
- for (int i = 0; i < drums; i++){
- drumLed[i].isActive ? leds[m_isActive[i]].saturation = 255 : leds[m_isActive[i]].saturation = 50;
- }
- };
- void colors(){};
- }
- void setup() {
- Serial.begin(9600); // Подключаем монитор порта для отладки
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Доподключаем библиотеку FastLED
- leds[4] = 0x001100; leds[5] = 0x111111; leds[6] = 0x001100; // Отображаем в интерфейсе режим по умолчанию
- }
- void loop() {
- // слушаем то, чего один
- if (modeButtonWasPressed()) {
- changeCurrentMode();
- changeInterface.mode();
- }
- if (fadeSliderWasTouched()) {
- changeFadeTime();
- changeInterface.fadeTime();
- }
- // слушаем то, чего три
- for (int drum = 0; drum < drums; drum++) {
- if (pressed__onOffBtn(drum)){
- switchDrum(drum);
- changeInterface.isActive(drum);
- }
- if (colorSliderWasTouched(drum)){
- changeColor(drum);
- }
- if (drumWasKicked(drum)){
- applyMode(drum);
- lightUp();
- }
- }
- fadeOut();
- }
- // functions
- void doNothing(){};
- void listetOnOffBtns() {
- for(int i = 0; i < drums; i++) {
- if(digitalRead(pin__OnOffBtn[i])){
- drumLed.isActive = !drumLed.isActive;
- Interface.isActive()
- }
- }
- }
- void changeInterface(int section) {
- switch (is active)
- {
- case /* constant-expression */:
- /* code */
- break;
- default:
- break;
- }
- }
- // ФУНКЦИИ
- void doNothing(){};
- void changeInterface(int i){
- if(interfaceValue[i] == 255) {
- interfaceValue[i] = 60;
- } else {
- interfaceValue[i] = 255;
- }
- }
- // Циклично переключает режимы (0/1/2), отображает индикацию текущего режима
- void changeMode() {
- switch (currentMode) {
- case 0:
- currentMode++;
- leds[4] = 0x111111;
- leds[5] = 0x111111;
- leds[6] = 0x001100;
- break;
- case 1:
- currentMode++;
- leds[4] = 0x111111;
- leds[5] = 0x111111;
- leds[6] = 0x111111;
- break;
- case 2:
- currentMode = 0;
- leds[4] = 0x001100;
- leds[5] = 0x111111;
- leds[6] = 0x001100;
- break;
- }
- }
- // Диапазон хода слайдера разбит на равные отрезки, за каждым отрезком закреплен свой цвет
- int getColor(int sliderValue) {
- enum Colors
- {
- COLOR__RED = 0,
- COLOR__ORANGE = 32; // Оранжевый
- COLOR__YELLOW = 64; // Желтый
- COLOR__GREEN = 96; // Зеленый
- COLOR__BLUE = 128; // Голубой
- COLOR__DARKBLUE = 160; // Синий
- COLOR__PURPLE = 192; // Фиолетовый
- COLOR__PINK = 244; // Розовый
- COLOR__WHITE = 254 // Красный (верхнее положение слайдера)
- }
- if (sliderValue == 0) return COLOR__RED; // Красный (нижнее положение слайдера)
- else if (sliderValue < 15) return COLOR__ORANGE; // Оранжевый
- else if (sliderValue < 92) return COLOR__YELLOW; // Желтый
- else if (sliderValue < 409) return COLOR__GREEN; // Зеленый
- else if (sliderValue < 725) return COLOR__BLUE; // Голубой
- else if (sliderValue < 981) return COLOR__DARKBLUE; // Синий
- else if (sliderValue < 1011) return COLOR__PURPLE; // Фиолетовый
- else if (sliderValue < 1022) return COLOR__PINK; // Розовый
- else return COLOR__WHITE; // Красный (верхнее положение слайдера)
- enum Colors
- {
- COLOR__RED = 0,
- COLOR__ORANGE = 32; // Оранжевый
- COLOR__YELLOW = 64; // Желтый
- COLOR__GREEN = 96; // Зеленый
- COLOR__BLUE = 128; // Голубой
- COLOR__DARKBLUE = 160; // Синий
- COLOR__PURPLE = 192; // Фиолетовый
- COLOR__PINK = 244; // Розовый
- COLOR__WHITE = 254 // Красный (верхнее положение слайдера)
- }
- // Плавно изменяем текущий цвет на цвет, установленый на слайдере
- int smoothlyChangeColor(int currentColor, int neededColor) {
- int colorChangeStep = 2;
- if (currentColor > neededColor) currentColor -= colorChangeStep;
- else if (currentColor < neededColor) currentColor += colorChangeStep;
- return currentColor;
- }
- // В верхнем положении слайдера цвета плавно включаем белый цвет
- int smoothlyChangeSaturation(int saturation, int hue) {
- int saturationChangeStep = 5;
- if (hue == 254 && saturation > 0) saturation -= saturationChangeStep;
- else if (hue == 254 && saturation <= saturationChangeStep) saturation = 0;
- else if (saturation < 255) saturation += saturationChangeStep;
- return saturation;
- }
- // Регулируем слайдером скорость затемнения после удара, отображаем индикацию
- float setFadeStepFromFadeSliderValue(int sliderValue) {
- float fadeStep;
- int indicator;
- if (sliderValue < 15) {fadeStep = 127.5; indicator = 0;}
- else if (sliderValue < 92) {fadeStep = 15; indicator = 1;}
- else if (sliderValue < 409) {fadeStep = 6; indicator = 2;}
- else if (sliderValue < 725) {fadeStep = 4; indicator = 3;}
- else if (sliderValue < 981) {fadeStep = 3; indicator = 4;}
- else if (sliderValue < 1011) {fadeStep = 1.5; indicator = 5;}
- else if (sliderValue < 1022) {fadeStep = 1; indicator = 6;}
- // Интерфейс
- fill_solid(&(leds[8 + indicator]), (6 - indicator), CHSV(0, 255, 100 ));
- fill_solid(&(leds[8]), indicator, CHSV(100, 0, 100 ));
- // Serial.println(indicator);
- return fadeStep;
- }
- bool drumWasKicked(int i){
- bool sensorHeardSound = (analogRead(pinSoundSensor[i]) > sensorThresholdLevel[i]);
- return sensorHeardSound;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement