В данной статье мы рассмотрим создание стерео измерителя уровня звука (stereo VU meter) на основе платы Arduino и светодиодных лентах (кольцах) от компании NeoPixel.
Необходимые компоненты
- Плата Arduino Nano (купить на AliExpress).
- Adafruit NeoPixel Ring: WS2812 5050 RGB LED (кольцо из светодиодов) – 2 шт. (купить на AliExpress).
- Потенциометр – 2 шт. (купить на AliExpress).
- Кнопка.
- Резисторы 10 кОм и 470 Ом (купить на AliExpress).
- Конденсатор 1 мкФ 6,3 В (купить на AliExpress).
- Макетная плата.
- Соединительные провода.
Реклама: ООО "АЛИБАБА.КОМ (РУ)" ИНН: 7703380158
Основная идея проекта
В интернете можно найти достаточно много различных проектов измерителей уровня звука (в том числе и на нашем сайте рассматривался подобный проект), но приведенный здесь проект измерителя уровня является одним из самых "красивых" и наглядных, в связи с чем он стал одним из самых популярных в этой тематике – в связи с этим я и решил перевести его на наш сайт. Статья является переводом проекта, размещенного в сборнике проектов официального сайта Arduino, ссылка на ее первоисточник приведена в самом конце данной статьи. Автор проекта данного измерителя звука сделал за прошедшие несколько лет несколько модификаций своего творения, в этой статье я приведу схемы и коды программ только для двух самых популярных модификаций данного проекта, с большим количеством модификаций (к каждой приведено видео) вы можете ознакомиться в первоисточнике статьи.
Схема проекта
Схема стерео измерителя уровня звука на Arduino и светодиодных лентах от 05.11.2017 представлена на следующем рисунке.
Схема стерео измерителя уровня звука на Arduino и светодиодных лентах от 02.02.2018 представлена на следующем рисунке.
Внешний вид собранной конструкции измерителя уровня звука (вид спереди и сзади) показан на следующих рисунках.
Объяснение работы стерео измерителя уровня звука
Принцип работы проекта наглядно показан на следующем видео.
Автор данного проекта сначала тестировал работу измерителя уровня звука с помощью ЖК дисплея, потом с помощью 2-х светодиодных матриц, затем с помощью светодиодной ленты и только после этого он пришел к идее использования двух светодиодных колец, которые сделали его проект максимально выделяющимся и наглядным на фоне других проектов измерителей уровня звука, которые можно найти в сети. Также автор тестировал свой проект как с микрофоном, так и и без него - все это вы можете подробно посмотреть на официальной странице данного проекта (ссылка на нее внизу статьи).
Автор начинал этот проект с простого скетча для Arduino, который использовал один канал и пару светодиодов. Постепенно он добавил в проект следующие дополнительные возможности:
- поддержку стерео;
- переключаемая функция хранения максимумов с опцией "падения вниз" ('falling down');
- настраиваемая чувствительность;
- настраиваемая яркость свечения светодиодов;
- можно использовать одну светодиодную ленту для отображения левого и правого каналов или два светодиодных кольца (по одному на каждый канал);
- функция медленного "падения вниз" ('falling down') светодиодов;
- переменные, которые можно использовать для поддержки любого числа светодиодов и другой возможной кастомизации измерителя уровня звука.
Для работы кода программы вам потребуется библиотека Adafruit_NeoPixel, которую можно скачать по следующей ссылке.
Добавление микрофона вместо линейного ввода
Автор проекта также тестировал свой измеритель уровня звука с модулем микрофона вместо линейного ввода. Схема соединений аналогична использованию линейного ввода: выход микрофона подключается к аналоговому контакту A0 платы Arduino (также необходимо подключить контакты VCC и ground микрофона).
В скетч при подключении модуля микрофона не нужно вносить никаких изменений, хотя могут потребоваться небольшие изменения в настройках чувствительности. Более подробно работа данного варианта проекта показана в следующем видео:
Исходный код программы (скетча)
Скетч для версии проекта 05.11.2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
/* ********************************************************************** * Stereo VU Meter for 1 or 2 LED rings or strips build by ericBcreator * Designed to be used with an Arduino UNO, Nano or compatible device. ********************************************************************** * Last updated 20171105 by ericBcreator * * This code is free for personal use, not for commercial purposes. * Please leave this header intact. * * contact: ericBcreator@gmail.com ********************************************************************** */ //#define DEBUG // for debugging // // include the NeoPixel library: // #include <Adafruit_NeoPixel.h> // // uncomment the definition for the connected strip or ring(s) // //#define led_ring_60 //#define led_strip_60 //#define led_strip_30 #define led_2_rings_24 //#define led_2_rings_30 //#define led_strip_200 //#define led_strip_144 // // important setting: using potentiometer sensor values or not // This setting has to be set right or the script will not work correctly: // - set this to true if using potentiometers // - set this to false if not using potentiometers // const int useSensorValues = true; // // setup pins // int leftPin = A0, rightPin = A1; // left audio in on analog 0, right on analog 1 int brightnessPin = A4, sensitivityPin = A5; // potentiometers for brightness and sensitivity on analog 4 and 5 int stripPin = 6; // DIN of leds on digital pin 6 int showPeaksPin = 7; // switch to toggle peaks on or off on digital pin 7 int momentarySwitch = false; // set false for an on/off toggle switch // // setup variables for the number of leds and led strip or 2 rings // #if defined (led_ring_60) //settings for a 60 led ring int stripNumOfLeds = 60; // the total number of leds uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = true; // set to true when using 2 strips or rings, false for one strip int middleOffset = 0; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 4; // hold time before dropping the leds float dropFactor = .92; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) int peakTimeFirstDropDelay = 130; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .93; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_strip_60) //settings for a 60 led ring int stripNumOfLeds = 60; // the total number of leds uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = false; // set to true when using 2 strips or rings, false for one strip int middleOffset = 1; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 4; // hold time before dropping the leds float dropFactor = .92; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) int peakTimeFirstDropDelay = 130; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .93; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_strip_30) //settings for a 30 led strip int stripNumOfLeds = 30; // the total number of leds uint32_t stripColor[16]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = false; // set to true when using 2 strips or rings, false for one strip int middleOffset = 1; // offset for the middle led when using one strip int startupAnimationDelay = 10; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 10; // hold time before dropping the leds float dropFactor = .9; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak int peakTimeDropDelay = 15; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 3; // how many leds to bounce up (max) int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 9; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_2_rings_24) //settings for 2 24 led rings int stripNumOfLeds = 48; uint32_t stripColor[25]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 5; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 2; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 10; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 3; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined(led_2_rings_30) //settings for 2 30 led rings int stripNumOfLeds = 60; uint32_t stripColor[31]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 5; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 2; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 10; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 3; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined (led_strip_200) //settings for a 200 led strip int stripNumOfLeds = 200; uint32_t stripColor[101]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 10; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 30; float peakDropFactor = .99; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 8; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined (led_strip_144) //settings for a 200 led strip int stripNumOfLeds = 145; uint32_t stripColor[73]; int displayMiddleLed = true; int splitStrip = false; int middleOffset = 1; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 10; float dropFactor = .85; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 5; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 10; int bouncingPeakDelay = 2; int bouncingPeakCounterInc = 10; #endif // // setup other variables, user editable // // basic settings int minValue = 10; // min analog input value int maxValue = 350; // max analog input value (0-1023 equals 0-5V) int sensitivityValue = 128; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) int maxSensitivity = 2 * 255; // let the 'volume' go up to 200%! int ledBrightness = 30; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) int sensorDeviationBrightness = 1; // eliminate fluctuating values int overflowDelay = 20; // overflow hold time // peak settings int displayPeaks = true; // value will be set by the switch if useSensorValues = true int droppingPeak = true; // display dropping peaks or not. note: displayPeaks has to be true int bouncingPeaks = true; // display bouncing peaks or not. note: displayPeaks has to be true // // initialize other variables needed for the sketch // int numOfSegments = stripNumOfLeds / 2; int halfNumOfSegments = numOfSegments / 2; int stripMiddle = stripNumOfLeds / 2; int maxDisplaySegments = stripMiddle - 1; float sensitivityFactor; int brightnessValue, prevBrightnessValue; float ledFactor, ledFactor_div_numOfSegments; int leftValue = 0, rightValue = 0, maxReadValue = 0; int leftAnalogValue = 0, rightAnalogValue = 0; int prevLeftValue = 0, prevRightValue = 0; int prevLeftAnalogValue = 0, prevRightAnalogValue = 0; int i, j; int dropLeft, dropRight; int leftDropTime, rightDropTime; int leftPeak = 0, rightPeak = 0; int leftPeakTime = 0, rightPeakTime = 0; int leftFirstPeak = true, rightFirstPeak = true; int readShowPeaksPin, prevReadShowPeaksPin; uint32_t stripMiddleColor, stripOverflowColor, stripHoldColor; int leftPeakBouncing = false, rightPeakBouncing = false; int leftPeakBounce = 0, rightPeakBounce = 0; int prevLeftPeakBounce = 0, prevRightPeakBounce = 0; int leftPeakBounceCounter = 0, rightPeakBounceCounter = 0; int leftPeakBounceDelayCounter = 0, rightPeakBounceDelayCounter = 0; // // initialize the strip or rings // Adafruit_NeoPixel strip = Adafruit_NeoPixel(stripNumOfLeds, stripPin, NEO_GRB + NEO_KHZ800); // // setup // void setup() { #ifdef DEBUG Serial.begin(9600); #endif pinMode(showPeaksPin, INPUT); strip.begin(); setStripColors(); startupAnimation(); if (useSensorValues) setInitialDisplayPeaks(); else setSensitivityFactor(); } // // main loop // void loop() { if (useSensorValues) readSensorValues(); readValues(); drawValues(); if (displayPeaks) { getPeaks(); drawPeaks(); } storePrevValues(); } // // functions // void setInitialDisplayPeaks() { readShowPeaksPin = digitalRead(showPeaksPin); if (readShowPeaksPin == HIGH) displayPeaks = false; else displayPeaks = true; prevReadShowPeaksPin = readShowPeaksPin; } void readSensorValues() { readShowPeaksPin = digitalRead(showPeaksPin); if (momentarySwitch) { if (readShowPeaksPin == LOW && prevReadShowPeaksPin == HIGH) { if (displayPeaks == true) { displayPeaks = false; clearLeftPeak(); clearRightPeak(); if (momentarySwitch) while (digitalRead(showPeaksPin) == LOW) {} } else { displayPeaks = true; } } } else { if (readShowPeaksPin == LOW && prevReadShowPeaksPin == HIGH) displayPeaks = true; else if (readShowPeaksPin == HIGH && prevReadShowPeaksPin == LOW) { displayPeaks = false; clearLeftPeak(); clearRightPeak(); } } prevReadShowPeaksPin = readShowPeaksPin; brightnessValue = analogRead(brightnessPin); brightnessValue = map(brightnessValue, 0, 1023, 0, 255); if (abs(brightnessValue - prevBrightnessValue) > sensorDeviationBrightness) { ledBrightness = brightnessValue; setStripColors(); prevBrightnessValue = brightnessValue; } sensitivityValue = analogRead(sensitivityPin); sensitivityValue = map(sensitivityValue, 0, 1023, 0, 255); setSensitivityFactor(); } void setSensitivityFactor() { //sensitivityValue_div_numOfSegments = sensitivityValue / numOfSegments; sensitivityFactor = ((float) sensitivityValue / 255 * (float) maxSensitivity / 255); } void readValues() { leftAnalogValue = analogRead(leftPin); rightAnalogValue = analogRead(rightPin); if (swapLeftRight) { int tempValue = leftAnalogValue; leftAnalogValue = rightAnalogValue; rightAnalogValue = tempValue; } if (leftAnalogValue < prevLeftAnalogValue) { leftDropTime++; if (leftDropTime > dropDelay) { leftAnalogValue = prevLeftAnalogValue * dropFactor; leftDropTime = 0; } else leftAnalogValue = prevLeftAnalogValue; } if (rightAnalogValue < prevRightAnalogValue) { rightDropTime++; if (rightDropTime > dropDelay) { rightAnalogValue = prevRightAnalogValue * dropFactor; rightDropTime = 0; } else rightAnalogValue = prevRightAnalogValue; } #ifdef DEBUG Serial.print(leftAnalogValue); Serial.print(" "); Serial.println(rightAnalogValue); #endif // map values leftValue = map(leftAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); rightValue = map(rightAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); if (leftValue > maxDisplaySegments) { leftValue = maxDisplaySegments; drawOverflow(); } if (rightValue > maxDisplaySegments) { rightValue = maxDisplaySegments; drawOverflow(); } } void storePrevValues() { prevLeftAnalogValue = leftAnalogValue; prevRightAnalogValue = rightAnalogValue; prevLeftValue = leftValue; prevRightValue = rightValue; } void getPeaks() { if (leftValue > leftPeak) { leftPeak = leftValue; leftPeakTime = 0; leftFirstPeak = true; if (bouncingPeaks) { leftPeakBouncing = true; leftPeakBounceCounter = 0; leftPeakBounceDelayCounter = 0; } } else { leftPeakTime++; if (droppingPeak) { if (leftFirstPeak) { if (leftPeakTime > peakTimeFirstDropDelay) { clearLeftPeak(); leftFirstPeak = false; } } else { if (leftPeakTime > peakTimeDropDelay) { clearLeftPeak(); } } } else { if (leftPeakTime > peakTimeNoDropDelay) { clearLeftPeak(); } } } if (leftPeakBouncing) { if (leftFirstPeak) { leftPeakBounceDelayCounter++; if (leftPeakBounceDelayCounter >= bouncingPeakDelay) { leftPeakBounceDelayCounter = 0; leftPeakBounceCounter += bouncingPeakCounterInc; if (leftPeakBounceCounter >= 180) { clearLeftBouncePeak(); clearLeftBounce(); } else { leftPeakBounce = min((sin(leftPeakBounceCounter * 0.0174532925) * bouncingPeaksNumOfLeds), (maxDisplaySegments - leftPeak)); if (leftPeakBounce != prevLeftPeakBounce) { clearLeftBouncePeak(); } prevLeftPeakBounce = leftPeakBounce; } } } } if (rightValue > rightPeak) { rightPeak = rightValue; rightPeakTime = 0; rightFirstPeak = true; if (bouncingPeaks) { rightPeakBouncing = true; rightPeakBounceCounter = 0; rightPeakBounceDelayCounter = 0; } } else { rightPeakTime++; if (droppingPeak) { if (rightFirstPeak) { if (rightPeakTime > peakTimeFirstDropDelay) { clearRightPeak(); rightFirstPeak = false; } } else { if (rightPeakTime > peakTimeDropDelay) clearRightPeak(); } } else { if (rightPeakTime > peakTimeNoDropDelay) clearRightPeak(); } } if (rightPeakBouncing) { if (rightFirstPeak) { rightPeakBounceDelayCounter++; if (rightPeakBounceDelayCounter >= bouncingPeakDelay) { rightPeakBounceDelayCounter = 0; rightPeakBounceCounter += bouncingPeakCounterInc; if (rightPeakBounceCounter >= 180) { clearRightBouncePeak(); clearRightBounce(); } else { rightPeakBounce = min((sin(rightPeakBounceCounter * 0.0174532925) * bouncingPeaksNumOfLeds), (maxDisplaySegments - rightPeak)); if (rightPeakBounce != prevRightPeakBounce) { clearRightBouncePeak(); } prevRightPeakBounce = rightPeakBounce; } } } } } void drawValues() { if (splitStrip) { for (i = middleOffset; i < leftValue; i++) strip.setPixelColor(i, stripColor[i]); for (i = prevLeftValue; i > leftValue; i--) strip.setPixelColor(i, 0); for (i = middleOffset; i < rightValue; i++) strip.setPixelColor(stripMiddle + i, stripColor[i]); for (i = prevRightValue; i > rightValue; i--) strip.setPixelColor(stripMiddle + i, 0); } else { for (i = middleOffset; i < leftValue; i++) strip.setPixelColor(stripMiddle + i, stripColor[i]); for (i = prevLeftValue; i > leftValue; i--) strip.setPixelColor(stripMiddle + i, 0); for (i = middleOffset; i < rightValue; i++) strip.setPixelColor(stripMiddle - i, stripColor[i]); for (i = prevRightValue; i > rightValue; i--) strip.setPixelColor(stripMiddle - i, 0); } if (displayMiddleLed) strip.setPixelColor(stripMiddle, stripMiddleColor); strip.show(); } void drawPeaks() { if (leftPeak > 0) { if (droppingPeakFade && leftPeakBouncing == false) stripHoldColor = strip.Color(max(1, (255 * leftPeak * ledFactor_div_numOfSegments)), 0, 0); else stripHoldColor = stripColor[numOfSegments]; if (splitStrip) strip.setPixelColor((leftPeak + leftPeakBounce), stripHoldColor); else strip.setPixelColor(stripMiddle + (leftPeak + leftPeakBounce), stripHoldColor); } if (rightPeak > 0) { if (droppingPeakFade && rightPeakBouncing == false) stripHoldColor = strip.Color(max(1, (255 * rightPeak * ledFactor_div_numOfSegments)), 0, 0); else stripHoldColor = stripColor[numOfSegments]; if (splitStrip) strip.setPixelColor(stripMiddle + rightPeak + prevRightPeakBounce, stripHoldColor); else strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), stripHoldColor); } if (leftPeak > 0 || rightPeak > 0) strip.show(); } void clearLeftPeak() { if (splitStrip) strip.setPixelColor((leftPeak + prevLeftPeakBounce), 0); else strip.setPixelColor(stripMiddle + (leftPeak + prevLeftPeakBounce), 0); if (droppingPeak) leftPeak = leftPeak * peakDropFactor; else leftPeak = 0; leftPeakTime = 0; } void clearLeftBounce() { leftPeakBouncing = false; leftPeakBounceCounter = 0; leftPeakBounce = 0; prevLeftPeakBounce = 0; } void clearLeftBouncePeak() { if (splitStrip) strip.setPixelColor((leftPeak + prevLeftPeakBounce), 0); else strip.setPixelColor(stripMiddle + (leftPeak + prevLeftPeakBounce), 0); } void clearRightPeak() { if (splitStrip) strip.setPixelColor(stripMiddle + rightPeak + prevRightPeakBounce, 0); else strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), 0); if (droppingPeak) rightPeak = rightPeak * peakDropFactor; else rightPeak = 0; rightPeakTime = 0; } void clearRightBounce() { rightPeakBouncing = false; rightPeakBounceCounter = 0; rightPeakBounce = 0; prevRightPeakBounce = 0; } void clearRightBouncePeak() { if (splitStrip) strip.setPixelColor((stripMiddle + rightPeak + prevRightPeakBounce), 0); else strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), 0); } void drawOverflow() { for (i = 0; i <= numOfSegments; i++) { strip.setPixelColor(stripMiddle + i, stripOverflowColor); strip.setPixelColor(stripMiddle - i, stripOverflowColor); } strip.show(); delay(overflowDelay); for (i = 0; i <= numOfSegments; i++) { strip.setPixelColor(stripMiddle + i, 0); strip.setPixelColor(stripMiddle - i, 0); } strip.show(); } void setStripColors() { int orangeLimit; ledFactor = (float)ledBrightness / 255; float orangeFactor = orangeLimitAmount / halfNumOfSegments; ledFactor_div_numOfSegments = ledFactor / numOfSegments; stripOverflowColor = strip.Color(min(255, 255 * ledFactor * 1.5), 0, 0); stripMiddleColor = strip.Color(0, 0, 255 * ledFactor); stripColor[0] = strip.Color(0, 255 * ledFactor, 0); for (i = 1; i <= numOfSegments; i++) { if (i <= halfNumOfSegments) orangeLimit = (i * orangeFactor); else orangeLimit = ((numOfSegments - i) * orangeFactor); stripColor[i] = strip.Color((255 * i * ledFactor_div_numOfSegments), ((255 - orangeLimit) * (numOfSegments - i) * ledFactor_div_numOfSegments), 0); } stripHoldColor = stripColor[numOfSegments]; } void startupAnimation() { for (j = 0; j < 2; j++) { for (i = 0; i <= numOfSegments; i++) { strip.setPixelColor(stripMiddle - i, stripColor[i]); strip.setPixelColor(stripMiddle + i, stripColor[i]); strip.show(); delay(startupAnimationDelay); } for (i = 0; i <= numOfSegments; i++) { strip.setPixelColor(stripMiddle + i, 0); strip.setPixelColor(stripMiddle - i, 0); strip.show(); delay(startupAnimationDelay); } } } |
Скетч для версии проекта 02.02.2018
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 |
/* ********************************************************************** * Stereo VU Meter for 1 or 2 LED rings or strips build by ericBcreator * Designed to be used with an Arduino UNO, Nano or compatible device. ********************************************************************** * Notice: search for @EB in the Sketch for important variables to set * for the Sketch to work with your setup. ********************************************************************** * Last updated 20180202 by ericBcreator * * This code is free for personal use, not for commercial purposes. * Please leave this header intact. * * contact: ericBcreator@gmail.com ********************************************************************** */ // // include the NeoPixel library: // #include <Adafruit_NeoPixel.h> // // debugging settings // //#define DEBUG // debug: enable serial.print //#define DEBUG_NO_PEAK_SWITCH // debug: no peak switch connected @EB //#define DEBUG_TEST_LEDS // debug: display each led (color) slowly at startup //#define DEBUG_PRINT_LOOP_TIME // debug: serial.print the looptime in ms //#define DEBUG_PRINT_ANALOGVALUES // debug: serial.print analog input values //#define DEBUG_NO_PEAKS // debug: display no peaks, ignoring other settings //#define DEBUG_PEAKS // debug: display peaks, ignoring other settings // // uncomment to average the input levels to the number defined by averageNumOfReadings. increasing the value will make the script less responsive // //#define averageReadings // average input levels or not //#define averageNumOfReadings 3 // num of readings for averaging // // uncomment to map the linear audio input to non-linear response // //#define nonLinearSinAudio // uncomment to map the linear audio input signal to a non-linear, reverse audio-taper response (sin wave) //#define nonLinearReverseSinAudio // uncomment to map the linear audio input signal to a non-linear, audio-taper response (reverse sin wave) #define nonLinearLogAudio // uncomment to map the linear audio input signal to a log response //#define nonLinearAvr2 // uncomment to average the original input with the non-linear response // // overflow settings // //#define displayOverflow // display overflow or not //#define compressOverflowPeaks // compress overflow peaks or not //#define compressOverflowNumOfTimes 2 // num of times to apply the compressOverflowFactor //float compressOverflowFactor = .05; // factor for compression // // uncomment when using high level (non-consumer) inputs // //#define highLevelInput // @EB define for high level inputs // // uncomment the definition for the connected strip or ring(s) @EB // //#define led_matrix_40 //#define led_ring_60 //#define led_ring_60_ps #define led_rhombus_160_ps //#define led_strip_60 //#define led_strip_60_qr //#define led_strip_30 //#define led_2_rings_24 //#define led_2_rings_30 //#define led_strip_200 //#define led_strip_144 //#define led_2_strip_63 //#define led_2_strip_63_qr // // important setting: using potentiometer sensor values or not // This setting has to be set right or the script will not work correctly: // - set to true if using potentiometers // - set to false if not using potentiometers // const int useSensorValues = true; // @EB // // setup pins // int leftPin = A0, rightPin = A1; // left audio in on analog 0, right on analog 1 int brightnessPin = A4, sensitivityPin = A5; // potentiometers for brightness and sensitivity on analog 4 and 5 int leftStripPin = 5; // DIN of left led strip on digital pin 5 int rightStripPin = 6; // DIN of right led strip on digital pin 6 int showPeaksPin = 7; // switch to toggle peaks on or off on digital pin 7 (7, 9 for box version) int showPeaksMomentarySwitch = false; // set false for an on/off toggle switch int reverseShowPeaks = true; // reverses the on/off setting in case you made a wiring mistake ;-) @EB int selectButton1Pin = 8; // push button for changing settings on digital pin 8 int useSelectButton1 = true; // set to false if no push button1 for selecting the color scheme is connected @EB int selectButton2Pin = 9; // push button for changing settings on digital pin 9 int useSelectButton2 = true; // set to false if no push button2 is connected @EB // // setup variables for the number of leds and led strip or 2 rings // #if defined (led_matrix_40) //settings for a 40 led matrix int stripNumOfLeds = 40; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[21]; // half of the number of leds + 1 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = true; // set to true when using 2 strips or rings, false for one strip int middleOffset = 0; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 5; // hold time before dropping the leds float dropFactor = .94; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 6; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_ring_60) //settings for a 60 led ring int stripNumOfLeds = 60; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = true; // set to true when using 2 strips or rings, false for one strip int middleOffset = 0; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 5; // hold time before dropping the leds float dropFactor = .94; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_ring_60_ps) //settings for a 60 led ring - pulsating and spinning settings int stripNumOfLeds = 60; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = true; // set to true when using 2 strips or rings, false for one strip int middleOffset = 0; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 5; // hold time before dropping the leds float dropFactor = .93; // value for dropping the leds int peakTimeNoDropDelay = 10; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 10; // peak hold time when dropping the first peak int peakTimeDropDelay = 6; // peak hold time when dropping the rest of the peaks float peakDropFactor = .92; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_rhombus_160_ps) //settings for a 160 led rhombus - pulsating and spinning settings int stripNumOfLeds = 160; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[81]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = true; // set to true when using 2 strips or rings, false for one strip int middleOffset = 0; // offset for the middle led when using one strip int startupAnimationDelay = 0; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 4; // hold time before dropping the leds float dropFactor = .92; // value for dropping the leds int peakTimeNoDropDelay = 150; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 10; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 5; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 6; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_strip_60) //settings for a 60 led strip int stripNumOfLeds = 60; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = false; // set to true when using 2 strips or rings, false for one strip int middleOffset = 1; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 5; // hold time before dropping the leds float dropFactor = .94; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_strip_60_qr) //settings for a 60 led strip - quick response int stripNumOfLeds = 60; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[31]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = false; // set to true when using 2 strips or rings, false for one strip int middleOffset = 1; // offset for the middle led when using one strip int startupAnimationDelay = 6; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 4; // hold time before dropping the leds float dropFactor = .92; // value for dropping the leds int peakTimeNoDropDelay = 0; // peak hold time when not dropping the peaks (when droppingPeak is false) int peakTimeFirstDropDelay = 0; // peak hold time when dropping the first peak int peakTimeDropDelay = 0; // peak hold time when dropping the rest of the peaks float peakDropFactor = 1; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 0; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 0; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 0; // delay between peak bounce updates int bouncingPeakCounterInc = 180; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_strip_30) //settings for a 30 led strip int stripNumOfLeds = 30; // the total number of leds int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins uint32_t stripColor[16]; // half of the number of leds + 1 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings int splitStrip = false; // set to true when using 2 strips or rings, false for one strip int middleOffset = 1; // offset for the middle led when using one strip int startupAnimationDelay = 10; // delay for the startup animation int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange int swapLeftRight = false; // swap the left and right input values or not int dropDelay = 10; // hold time before dropping the leds float dropFactor = .9; // value for dropping the leds int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak int peakTimeDropDelay = 15; // peak hold time when dropping the rest of the peaks float peakDropFactor = .94; // value for dropping the peaks int droppingPeakFade = false; // display the dropping peak fading to black or not int bouncingPeaksNumOfLeds = 4; // how many leds to bounce up (max) int bouncingPeaksNumOfLedsMin = 2; // how many leds to bounce up (min) when using dynamicBouncingPeaks int bouncingPeakDelay = 4; // delay between peak bounce updates int bouncingPeakCounterInc = 9; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing #elif defined (led_2_rings_24) //settings for 2 24 led rings int stripNumOfLeds = 48; int stripsOn2Pins = false; uint32_t stripColor[25]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 5; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 2; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 10; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 4; int bouncingPeaksNumOfLedsMin = 2; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined(led_2_rings_30) //settings for 2 30 led rings int stripNumOfLeds = 60; int stripsOn2Pins = false; uint32_t stripColor[31]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 5; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 2; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 10; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 4; int bouncingPeaksNumOfLedsMin = 2; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined (led_strip_200) //settings for a 200 led strip int stripNumOfLeds = 200; int stripsOn2Pins = false; uint32_t stripColor[101]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 10; float dropFactor = .96; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 30; float peakDropFactor = .99; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 8; int bouncingPeaksNumOfLedsMin = 4; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 9; #elif defined (led_strip_144) //settings for a 144 led strip int stripNumOfLeds = 145; int stripsOn2Pins = false; uint32_t stripColor[73]; int displayMiddleLed = true; int splitStrip = false; int middleOffset = 1; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 4; float dropFactor = .92; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 100; int peakTimeDropDelay = 5; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 10; int bouncingPeaksNumOfLedsMin = 4; int bouncingPeakDelay = 2; int bouncingPeakCounterInc = 10; #elif defined (led_2_strip_63) //settings for 2 63 led strips int stripNumOfLeds = 63; int stripsOn2Pins = true; uint32_t stripColor[64]; int displayMiddleLed = true; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 5; float dropFactor = .94; int peakTimeNoDropDelay = 250; int peakTimeFirstDropDelay = 70; int peakTimeDropDelay = 7; float peakDropFactor = .94; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 12; int bouncingPeaksNumOfLedsMin = 4; int bouncingPeakDelay = 4; int bouncingPeakCounterInc = 10; #elif defined (led_2_strip_63_qr) //settings for 2 63 led strips - quick response int stripNumOfLeds = 63; int stripsOn2Pins = true; uint32_t stripColor[64]; int displayMiddleLed = false; int splitStrip = true; int middleOffset = 0; int startupAnimationDelay = 1; int orangeLimitAmount = 0; int swapLeftRight = false; int dropDelay = 4; float dropFactor = .92; int peakTimeNoDropDelay = 200; int peakTimeFirstDropDelay = 60; int peakTimeDropDelay = 6; float peakDropFactor = .92; int droppingPeakFade = false; int bouncingPeaksNumOfLeds = 12; int bouncingPeaksNumOfLedsMin = 4; int bouncingPeakDelay = 3; int bouncingPeakCounterInc = 10; #endif // // setup other user variables // // basic settings int pulsing = true; // pulsing will display from the middle of each strip or ring @EB int spinCircle = true; // spin the animation. will not work with stripsOn2Pins @EB int animType = 0; // startup animation selection (1 looks nice for 1 ring) @EB int colorScheme = 10; // 0: green-red, 1: blue-green, 2: blue-red, 3: red-blue, 4: green-blue, 5: red-green, 6: blue-white-red // 7: red-white-blue, 8: green-white-red, 9: green-white-blue, 10: color wheel, 11: spinning color wheel, // 12: as 11 but spread with factor colorScheme12Factor @EB int maxColorScheme = 12; // used for looping through the color schemes with the switch button int colorScheme11SpinDelay = stripNumOfLeds / 4 ; // delay for spinning scheme 11 int colorScheme12Factor = 3; // wheel spread factor for scheme 12 @EB int minValue = 10; // min analog input value int sensitivityValue = 110; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) #ifdef highLevelInput int maxValue = 700; // max analog input value (0-1023 equals 0-5V). try 300 for low level input, 700 for high int maxSensitivity = 2 * 255; // set to a higher setting to amplify low input levels. try 4 * 255 for low level input, 2 * 255 for high #else int maxValue = 300; // max analog input value (0-1023 equals 0-5V). try 300 for low level input, 700 for high int maxSensitivity = 4 * 255; // set to a higher setting to amplify low input levels. try 4 * 255 for low level input, 2 * 255 for high #endif int ledBrightness = 30; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) int sensorDeviationBrightness = 3; // eliminate fluctuating values int overflowDelay = 10; // overflow hold time // peak settings @EB int displayPeaks = false; // value will be set by the switch if useSensorValues = true int displayTopAsPeak = true; // always display the top LED in peak color int droppingPeak = true; // display dropping peaks or not. note: displayPeaks has to be true int bouncingPeaks = false; // display bouncing peaks or not. note: displayPeaks has to be true int dynamicBouncingPeaks = false; // bounce less with lower peaks. note: bouncingPeaks has to be true // // initialize other variables // int numOfSegments, halfNumOfSegments, stripMiddle, maxDisplaySegments; float sensitivityFactor; float nonLinearResponseFactor; int brightnessValue, prevBrightnessValue; float ledFactor, ledFactor_div_numOfSegments; uint32_t stripMiddleColor, stripOverflowColor, stripHoldColor; uint32_t colorValue; int leftValue = 0, rightValue = 0, maxReadValue = 0; int leftValueN = 0, rightValueN = 0; int leftAnalogValue = 0, rightAnalogValue = 0; float log10MaxDisplaySegments; int prevLeftValue = 0, prevRightValue = 0; int prevLeftAnalogValue = 0, prevRightAnalogValue = 0; int selectButton1PinState = 0, prevSelectButton1PinState = 0; int selectButton2PinState = 0, prevSelectButton2PinState = 0; int selectButton1PinSetting = colorScheme; int selectButton2PinSetting = 0; int i, j; int dropLeft, dropRight; int leftDropTime, rightDropTime; int leftPeak = 0, rightPeak = 0; int leftPeakTime = 0, rightPeakTime = 0; int leftFirstPeak = true, rightFirstPeak = true; int showPeaksPinSetting, prevShowPeaksPinSetting; int stripPulseMiddle = 0; int halfLeftValue, halfRightValue, halfPrevLeftValue, halfPrevRightValue; int leftPeakBouncing = false, rightPeakBouncing = false; int leftPeakBounce = 0, rightPeakBounce = 0; int prevLeftPeakBounce = 0, prevRightPeakBounce = 0; int leftPeakBounceCounter = 0, rightPeakBounceCounter = 0; int leftPeakBounceDelayCounter = 0, rightPeakBounceDelayCounter = 0; int leftBouncingPeaksNumOfLeds = 0, rightBouncingPeaksNumOfLeds = 0; float bounceFactor; int colorScheme11SpinValue = 0, colorScheme11SpinDelayValue = 0; int colorSchemeFactor = 1; long selectButton1Timer; int spinDelayCounter = 0, spinCounter = 0, spinTurnsCounter = 0, spinTurnsMax = 0, spinTurnsDelay = 0, spinTurnsDelayMax = 0; int spinCounterInc = 1; int spinDelay = 0; // // initialize the strip or rings // Adafruit_NeoPixel left_strip = Adafruit_NeoPixel(stripNumOfLeds, leftStripPin, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel right_strip = Adafruit_NeoPixel(stripNumOfLeds, rightStripPin, NEO_GRB + NEO_KHZ800); // // setup // void setup() { #ifdef DEBUG Serial.begin(9600); #endif randomSeed(analogRead(2)); if (stripsOn2Pins) { numOfSegments = stripNumOfLeds; maxDisplaySegments = numOfSegments - 1; stripMiddle = stripNumOfLeds; stripPulseMiddle = stripMiddle / 2; spinCircle = false; } else { numOfSegments = stripNumOfLeds / 2; stripMiddle = stripNumOfLeds / 2; maxDisplaySegments = stripMiddle - 1; stripPulseMiddle = stripMiddle / 2; } halfNumOfSegments = numOfSegments / 2; bounceFactor = (float) bouncingPeaksNumOfLeds / (maxDisplaySegments - bouncingPeaksNumOfLeds); nonLinearResponseFactor = 90 / (float) maxDisplaySegments; log10MaxDisplaySegments = log10(maxDisplaySegments); pinMode(showPeaksPin, INPUT); if (useSelectButton1) pinMode(selectButton1Pin, INPUT); left_strip.begin(); if (stripsOn2Pins) right_strip.begin(); if (useSensorValues) { readSensorValues(); setInitialDisplayPeaks(); } else { setStripColors(); setSensitivityFactor(); } #ifdef DEBUG_TEST_LEDS displayTest(); #endif startupAnimation(); } // // main loop // void loop() { #ifdef DEBUG_PRINT_LOOP_TIME long time = millis(); #endif if (useSensorValues) readSensorValues(); readValues(); #if defined (DEBUG_NO_PEAKS) displayPeaks = false; #endif #if defined (DEBUG_PEAKS) displayPeaks = true; #endif if (pulsing) { drawPulsingValues(); } else { drawValues(); if (displayPeaks) { getPeaks(); drawPeaks(); } } left_strip.show(); if (stripsOn2Pins) right_strip.show(); storePrevValues(); checkSpinCircle(); #ifdef DEBUG_PRINT_LOOP_TIME time = millis() - time; Serial.println(time); #endif } // // functions // void setInitialDisplayPeaks() { #if !defined (DEBUG_NO_PEAK_SWITCH) showPeaksPinSetting = digitalRead(showPeaksPin); if (showPeaksPinSetting == HIGH) displayPeaks = false; #endif if (reverseShowPeaks) { if (!displayPeaks) displayPeaks = true; else displayPeaks = false; } prevShowPeaksPinSetting = showPeaksPinSetting; } void readSensorValues() { // // peaks pin // #if !defined (DEBUG_NO_PEAK_SWITCH) showPeaksPinSetting = digitalRead(showPeaksPin); if (showPeaksMomentarySwitch) { if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { if (displayPeaks == true) { displayPeaks = false; clearLeftPeak(); clearRightPeak(); if (showPeaksMomentarySwitch) while (digitalRead(showPeaksPin) == LOW) {} } else { displayPeaks = true; } } } else { if (reverseShowPeaks) { if (showPeaksPinSetting == HIGH && prevShowPeaksPinSetting == LOW) displayPeaks = true; else if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { displayPeaks = false; clearLeftPeak(); clearRightPeak(); } } else { if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) displayPeaks = true; else if (showPeaksPinSetting == HIGH && prevShowPeaksPinSetting == LOW) { displayPeaks = false; clearLeftPeak(); clearRightPeak(); } } } if (pulsing) { if (displayPeaks) displayTopAsPeak = true; else displayTopAsPeak = false; } prevShowPeaksPinSetting = showPeaksPinSetting; #endif // // selectButtonPin 1 and 2 // if (useSelectButton1) { selectButton1PinState = digitalRead(selectButton1Pin); if (selectButton1PinState == HIGH && prevSelectButton1PinState == LOW) selectButton1Timer = millis(); if (selectButton1PinState == HIGH && prevSelectButton1PinState == HIGH) { if ((millis() - selectButton1Timer) > 1000) { pulsing = !pulsing; setStripColors(); displayNumber(colorScheme, 250); while (digitalRead(selectButton1Pin) == HIGH) {} selectButton1PinState = LOW; clearValues(); } } else if (selectButton1PinState == LOW && prevSelectButton1PinState == HIGH) { selectButton1PinSetting++; if (selectButton1PinSetting > maxColorScheme) { selectButton1PinSetting = 0; } colorScheme = selectButton1PinSetting; if (colorScheme == 12) colorScheme11SpinValue = (colorScheme11SpinValue * colorScheme12Factor); setStripColors(); displayNumber(colorScheme, 250); } prevSelectButton1PinState = selectButton1PinState; } if (useSelectButton2) { selectButton2PinState = digitalRead(selectButton2Pin); if (selectButton2PinState == HIGH && prevSelectButton2PinState == LOW) { selectButton2PinSetting++; switch(selectButton2PinSetting) { case 0: case 3: { pulsing = false; spinCircle = false; selectButton2PinSetting = 0; break; } case 1: { pulsing = true; spinCircle= false; break; } case 2: { pulsing = true; spinCircle = true; break; } } setStripColors(); displayNumber(colorScheme, 250); } prevSelectButton2PinState = selectButton2PinState; } // // brightness // brightnessValue = analogRead(brightnessPin); brightnessValue = map(brightnessValue, 0, 1023, 0, 255); if (abs(brightnessValue - prevBrightnessValue) > sensorDeviationBrightness) { ledBrightness = brightnessValue; setStripColors(); prevBrightnessValue = brightnessValue; } // // colorscheme 11 spinning wheel // if (colorScheme == 11 || colorScheme == 12) { colorScheme11SpinDelayValue++; if (colorScheme11SpinDelayValue == colorScheme11SpinDelay) { colorScheme11SpinDelayValue = 0; colorScheme11SpinValue++; if (colorScheme11SpinValue > maxDisplaySegments * colorSchemeFactor) colorScheme11SpinValue = 0; setStripColors(); } } // // sensitivity // sensitivityValue = analogRead(sensitivityPin); sensitivityValue = map(sensitivityValue, 0, 1023, 0, 255); setSensitivityFactor(); } void setSensitivityFactor() { //sensitivityValue_div_numOfSegments = sensitivityValue / numOfSegments; sensitivityFactor = ((float) sensitivityValue / 255 * (float) maxSensitivity / 255); } void readValues() { #ifdef averageReadings leftAnalogValue = 0; rightAnalogValue = 0; for (i = 0; i <= averageNumOfReadings; i++) { leftAnalogValue += analogRead(leftPin); rightAnalogValue += analogRead(rightPin); } leftAnalogValue /= averageNumOfReadings; rightAnalogValue /= averageNumOfReadings; #else leftAnalogValue = analogRead(leftPin); rightAnalogValue = analogRead(rightPin); #endif if (swapLeftRight) { int tempValue = leftAnalogValue; leftAnalogValue = rightAnalogValue; rightAnalogValue = tempValue; } if (leftAnalogValue < prevLeftAnalogValue) { leftDropTime++; if (leftDropTime > dropDelay) { leftAnalogValue = prevLeftAnalogValue * dropFactor; leftDropTime = 0; } else leftAnalogValue = prevLeftAnalogValue; } if (rightAnalogValue < prevRightAnalogValue) { rightDropTime++; if (rightDropTime > dropDelay) { rightAnalogValue = prevRightAnalogValue * dropFactor; rightDropTime = 0; } else rightAnalogValue = prevRightAnalogValue; } #ifdef DEBUG_PRINT_ANALOGVALUES Serial.print(leftAnalogValue); Serial.print(" "); Serial.println(rightAnalogValue); #endif // map values leftValue = map(leftAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); rightValue = map(rightAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); // if defined, convert to (reverse) non linear response boolean flagNonLinear = false; #if defined (nonLinearSinAudio) flagNonLinear = true; leftValueN = ((sin(((leftValue * nonLinearResponseFactor) + 270) * 0.0174533) + 1) * maxDisplaySegments); rightValueN = ((sin(((rightValue * nonLinearResponseFactor) + 270) * 0.0174533) + 1) * maxDisplaySegments); #elif defined (nonLinearReverseSinAudio) flagNonLinear = true; leftValueN = ((sin(((leftValue * nonLinearResponseFactor)) * 0.0174533)) * maxDisplaySegments); rightValueN = ((sin(((rightValue * nonLinearResponseFactor)) * 0.0174533)) * maxDisplaySegments); #elif defined (nonLinearLogAudio) flagNonLinear = |