Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. #include <FastLED.h> // Подключаем библиотеку FastLED для управления LED лентой
  2. #define NUM_LEDS 210 // Общее число светодиодов
  3. #define DATA_PIN 8 // Пин подключения LED ленты
  4. CRGB leds[NUM_LEDS]; // Массив для управления LED лентой
  5.  
  6. // Количество барабасов
  7. const int drums = 3;
  8.  
  9. // Подключаем устройства к пинам ардуино
  10. const int pin__ModeChangeBtn = 2;
  11. const int pin__OnOffBtn[drums] = {3, 4, 5};
  12. const int pin__ColorSlider[drums] = {A0, A1, A2};
  13. const int pin__FadeSlider = A3;
  14. const int pin__SoundSensor[drums] = {A4, A5, A6};
  15.  
  16. // Разметка ленты
  17. const int startPart[drums] = {30, 90, 150};
  18. const int partLength = 60;
  19.  
  20. // Переменные
  21. int currentMode = 0; // Текущий режим
  22. float fadeStep = getFadestepFromSlider(); // Скорость затемнения после удара
  23. int thresholdLevel[drums] = {50,50,50}; // Порог срабатывания датчиков удара
  24.  
  25.  
  26. class Led {
  27. public:
  28. bool isActive;
  29.  
  30. int currentColor;
  31. int neededColor;
  32.  
  33. int saturation;
  34. int interfaceSaturation;
  35.  
  36. int brightness;
  37. int interfaceBrightness;
  38.  
  39. int thresholdLevel;
  40. };
  41.  
  42. Led drumLed[drums];
  43.  
  44. for (int i = 0; i < drums; i++){
  45.  
  46. drumLed[i].currentColor = 0;
  47. drumLed[i].neededColor = getNeededColor(i);
  48. drumLed[i].saturation = 255;
  49. drumLed[i].brightness = 255;
  50.  
  51. drumLed[i].isActive = true;
  52.  
  53. drumLed[i].interfaceSaturation = 255;
  54. drumLed[i].interfaceBrightness = 255;
  55.  
  56. drumLed[i].thresholdLevel = thresholdLevel[i];
  57.  
  58. }
  59.  
  60. class ChangeInterface {
  61. private:
  62. int m_isActive[drums] = {0,1,2},
  63. int m_colors[drums] = {0,1,2};
  64. int m_mode[3] = {4,5,6};
  65. int m_fade[7] = {8,9,10,11,12,13,14};
  66. void isActive(int m_isActive){
  67. for (int i = 0; i < drums; i++){
  68. drumLed[i].isActive ? leds[m_isActive[i]].saturation = 255 : leds[m_isActive[i]].saturation = 50;
  69. }
  70. };
  71. void colors(){};
  72. }
  73.  
  74.  
  75. void setup() {
  76. Serial.begin(9600); // Подключаем монитор порта для отладки
  77. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Доподключаем библиотеку FastLED
  78. leds[4] = 0x001100; leds[5] = 0x111111; leds[6] = 0x001100; // Отображаем в интерфейсе режим по умолчанию
  79. }
  80.  
  81. void loop() {
  82.  
  83. // слушаем то, чего один
  84. if (modeButtonWasPressed()) {
  85. changeCurrentMode();
  86. changeInterface.mode();
  87. }
  88.  
  89. if (fadeSliderWasTouched()) {
  90. changeFadeTime();
  91. changeInterface.fadeTime();
  92. }
  93.  
  94. // слушаем то, чего три
  95. for (int drum = 0; drum < drums; drum++) {
  96.  
  97. if (pressed__onOffBtn(drum)){
  98. switchDrum(drum);
  99. changeInterface.isActive(drum);
  100. }
  101.  
  102. if (colorSliderWasTouched(drum)){
  103. changeColor(drum);
  104. }
  105.  
  106. if (drumWasKicked(drum)){
  107. applyMode(drum);
  108. lightUp();
  109. }
  110. }
  111. fadeOut();
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. // functions
  125.  
  126. void doNothing(){};
  127.  
  128. void listetOnOffBtns() {
  129. for(int i = 0; i < drums; i++) {
  130. if(digitalRead(pin__OnOffBtn[i])){
  131. drumLed.isActive = !drumLed.isActive;
  132. Interface.isActive()
  133. }
  134. }
  135. }
  136.  
  137. void changeInterface(int section) {
  138. switch (is active)
  139. {
  140. case /* constant-expression */:
  141. /* code */
  142. break;
  143.  
  144. default:
  145. break;
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152. // ФУНКЦИИ
  153.  
  154. void doNothing(){};
  155.  
  156. void changeInterface(int i){
  157. if(interfaceValue[i] == 255) {
  158. interfaceValue[i] = 60;
  159. } else {
  160. interfaceValue[i] = 255;
  161. }
  162. }
  163.  
  164. // Циклично переключает режимы (0/1/2), отображает индикацию текущего режима
  165. void changeMode() {
  166. switch (currentMode) {
  167. case 0:
  168. currentMode++;
  169. leds[4] = 0x111111;
  170. leds[5] = 0x111111;
  171. leds[6] = 0x001100;
  172. break;
  173. case 1:
  174. currentMode++;
  175. leds[4] = 0x111111;
  176. leds[5] = 0x111111;
  177. leds[6] = 0x111111;
  178. break;
  179. case 2:
  180. currentMode = 0;
  181. leds[4] = 0x001100;
  182. leds[5] = 0x111111;
  183. leds[6] = 0x001100;
  184. break;
  185. }
  186. }
  187.  
  188. // Диапазон хода слайдера разбит на равные отрезки, за каждым отрезком закреплен свой цвет
  189. int getColor(int sliderValue) {
  190.  
  191. enum Colors
  192. {
  193. COLOR__RED = 0,
  194. COLOR__ORANGE = 32; // Оранжевый
  195. COLOR__YELLOW = 64; // Желтый
  196. COLOR__GREEN = 96; // Зеленый
  197. COLOR__BLUE = 128; // Голубой
  198. COLOR__DARKBLUE = 160; // Синий
  199. COLOR__PURPLE = 192; // Фиолетовый
  200. COLOR__PINK = 244; // Розовый
  201. COLOR__WHITE = 254 // Красный (верхнее положение слайдера)
  202. }
  203.  
  204. if (sliderValue == 0) return COLOR__RED; // Красный (нижнее положение слайдера)
  205. else if (sliderValue < 15) return COLOR__ORANGE; // Оранжевый
  206. else if (sliderValue < 92) return COLOR__YELLOW; // Желтый
  207. else if (sliderValue < 409) return COLOR__GREEN; // Зеленый
  208. else if (sliderValue < 725) return COLOR__BLUE; // Голубой
  209. else if (sliderValue < 981) return COLOR__DARKBLUE; // Синий
  210. else if (sliderValue < 1011) return COLOR__PURPLE; // Фиолетовый
  211. else if (sliderValue < 1022) return COLOR__PINK; // Розовый
  212. else return COLOR__WHITE; // Красный (верхнее положение слайдера)
  213.  
  214. enum Colors
  215. {
  216. COLOR__RED = 0,
  217. COLOR__ORANGE = 32; // Оранжевый
  218. COLOR__YELLOW = 64; // Желтый
  219. COLOR__GREEN = 96; // Зеленый
  220. COLOR__BLUE = 128; // Голубой
  221. COLOR__DARKBLUE = 160; // Синий
  222. COLOR__PURPLE = 192; // Фиолетовый
  223. COLOR__PINK = 244; // Розовый
  224. COLOR__WHITE = 254 // Красный (верхнее положение слайдера)
  225. }
  226.  
  227. // Плавно изменяем текущий цвет на цвет, установленый на слайдере
  228. int smoothlyChangeColor(int currentColor, int neededColor) {
  229. int colorChangeStep = 2;
  230. if (currentColor > neededColor) currentColor -= colorChangeStep;
  231. else if (currentColor < neededColor) currentColor += colorChangeStep;
  232. return currentColor;
  233. }
  234.  
  235. // В верхнем положении слайдера цвета плавно включаем белый цвет
  236. int smoothlyChangeSaturation(int saturation, int hue) {
  237. int saturationChangeStep = 5;
  238. if (hue == 254 && saturation > 0) saturation -= saturationChangeStep;
  239. else if (hue == 254 && saturation <= saturationChangeStep) saturation = 0;
  240. else if (saturation < 255) saturation += saturationChangeStep;
  241. return saturation;
  242. }
  243.  
  244. // Регулируем слайдером скорость затемнения после удара, отображаем индикацию
  245. float setFadeStepFromFadeSliderValue(int sliderValue) {
  246. float fadeStep;
  247. int indicator;
  248.  
  249. if (sliderValue < 15) {fadeStep = 127.5; indicator = 0;}
  250. else if (sliderValue < 92) {fadeStep = 15; indicator = 1;}
  251. else if (sliderValue < 409) {fadeStep = 6; indicator = 2;}
  252. else if (sliderValue < 725) {fadeStep = 4; indicator = 3;}
  253. else if (sliderValue < 981) {fadeStep = 3; indicator = 4;}
  254. else if (sliderValue < 1011) {fadeStep = 1.5; indicator = 5;}
  255. else if (sliderValue < 1022) {fadeStep = 1; indicator = 6;}
  256.  
  257. // Интерфейс
  258. fill_solid(&(leds[8 + indicator]), (6 - indicator), CHSV(0, 255, 100 ));
  259. fill_solid(&(leds[8]), indicator, CHSV(100, 0, 100 ));
  260.  
  261. // Serial.println(indicator);
  262. return fadeStep;
  263. }
  264.  
  265. bool drumWasKicked(int i){
  266. bool sensorHeardSound = (analogRead(pinSoundSensor[i]) > sensorThresholdLevel[i]);
  267. return sensorHeardSound;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement