Components and supplies
Arduino Nano R3
Jumper wires (generic)
Resistor 10k ohm
NeoPixel Ring: WS2812 5050 RGB LED
Button
Breadboard (generic)
Rotary potentiometer (generic)
Capacitor 1 mF 6.3V
Resistor 470 ohm
Apps and platforms
Arduino IDE
Project description
Code
20180202 VU meter update
arduino
Updated code with a lot of changes. Note: use the updated diagram, there are some changes in pin connections - multiple color schemes - option to connect 2 strip or rings on 2 output pins of the Arduino (the previous code used 1) - option for pulsing and spinning - option for logarithmic audio response
1/* 2********************************************************************** 3* Stereo VU Meter for 1 or 2 LED rings or strips build by ericBcreator 4* Designed to be used with an Arduino UNO, Nano or compatible device. 5********************************************************************** 6* Notice: search for @EB in the Sketch for important variables to set 7* for the Sketch to work with your setup. 8********************************************************************** 9* Last updated 20180202 by ericBcreator 10* 11* This code is free for personal use, not for commercial purposes. 12* Please leave this header intact. 13* 14* contact: ericBcreator@gmail.com 15********************************************************************** 16*/ 17 18// 19// include the NeoPixel library: 20// 21 22#include <Adafruit_NeoPixel.h> 23 24// 25// debugging settings 26// 27//#define DEBUG // debug: enable serial.print 28//#define DEBUG_NO_PEAK_SWITCH // debug: no peak switch connected @EB 29//#define DEBUG_TEST_LEDS // debug: display each led (color) slowly at startup 30//#define DEBUG_PRINT_LOOP_TIME // debug: serial.print the looptime in ms 31//#define DEBUG_PRINT_ANALOGVALUES // debug: serial.print analog input values 32//#define DEBUG_NO_PEAKS // debug: display no peaks, ignoring other settings 33//#define DEBUG_PEAKS // debug: display peaks, ignoring other settings 34 35// 36// uncomment to average the input levels to the number defined by averageNumOfReadings. increasing the value will make the script less responsive 37// 38//#define averageReadings // average input levels or not 39//#define averageNumOfReadings 3 // num of readings for averaging 40 41// 42// uncomment to map the linear audio input to non-linear response 43// 44//#define nonLinearSinAudio // uncomment to map the linear audio input signal to a non-linear, reverse audio-taper response (sin wave) 45//#define nonLinearReverseSinAudio // uncomment to map the linear audio input signal to a non-linear, audio-taper response (reverse sin wave) 46#define nonLinearLogAudio // uncomment to map the linear audio input signal to a log response 47//#define nonLinearAvr2 // uncomment to average the original input with the non-linear response 48 49// 50// overflow settings 51// 52//#define displayOverflow // display overflow or not 53//#define compressOverflowPeaks // compress overflow peaks or not 54//#define compressOverflowNumOfTimes 2 // num of times to apply the compressOverflowFactor 55//float compressOverflowFactor = .05; // factor for compression 56 57// 58// uncomment when using high level (non-consumer) inputs 59// 60//#define highLevelInput // @EB define for high level inputs 61 62// 63// uncomment the definition for the connected strip or ring(s) @EB 64// 65 66//#define led_matrix_40 67//#define led_ring_60 68//#define led_ring_60_ps 69#define led_rhombus_160_ps 70//#define led_strip_60 71//#define led_strip_60_qr 72//#define led_strip_30 73//#define led_2_rings_24 74//#define led_2_rings_30 75//#define led_strip_200 76//#define led_strip_144 77//#define led_2_strip_63 78//#define led_2_strip_63_qr 79 80// 81// important setting: using potentiometer sensor values or not 82// This setting has to be set right or the script will not work correctly: 83// - set to true if using potentiometers 84// - set to false if not using potentiometers 85// 86 87const int useSensorValues = true; // @EB 88 89// 90// setup pins 91// 92 93int leftPin = A0, rightPin = A1; // left audio in on analog 0, right on analog 1 94int brightnessPin = A4, sensitivityPin = A5; // potentiometers for brightness and sensitivity on analog 4 and 5 95int leftStripPin = 5; // DIN of left led strip on digital pin 5 96int rightStripPin = 6; // DIN of right led strip on digital pin 6 97int showPeaksPin = 7; // switch to toggle peaks on or off on digital pin 7 (7, 9 for box version) 98int showPeaksMomentarySwitch = false; // set false for an on/off toggle switch 99int reverseShowPeaks = true; // reverses the on/off setting in case you made a wiring mistake ;-) @EB 100int selectButton1Pin = 8; // push button for changing settings on digital pin 8 101int useSelectButton1 = true; // set to false if no push button1 for selecting the color scheme is connected @EB 102int selectButton2Pin = 9; // push button for changing settings on digital pin 9 103int useSelectButton2 = true; // set to false if no push button2 is connected @EB 104 105// 106// setup variables for the number of leds and led strip or 2 rings 107// 108 109#if defined (led_matrix_40) 110 //settings for a 40 led matrix 111 112 int stripNumOfLeds = 40; // the total number of leds 113 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 114 uint32_t stripColor[21]; // half of the number of leds + 1 115 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings 116 int splitStrip = true; // set to true when using 2 strips or rings, false for one strip 117 int middleOffset = 0; // offset for the middle led when using one strip 118 int startupAnimationDelay = 6; // delay for the startup animation 119 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 120 int swapLeftRight = false; // swap the left and right input values or not 121 122 int dropDelay = 5; // hold time before dropping the leds 123 float dropFactor = .94; // value for dropping the leds 124 125 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) 126 int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak 127 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 128 float peakDropFactor = .94; // value for dropping the peaks 129 int droppingPeakFade = false; // display the dropping peak fading to black or not 130 131 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 132 int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks 133 int bouncingPeakDelay = 6; // delay between peak bounce updates 134 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 135 136#elif defined (led_ring_60) 137 //settings for a 60 led ring 138 139 int stripNumOfLeds = 60; // the total number of leds 140 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 141 uint32_t stripColor[31]; // half of the number of leds + 1 142 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings 143 int splitStrip = true; // set to true when using 2 strips or rings, false for one strip 144 int middleOffset = 0; // offset for the middle led when using one strip 145 int startupAnimationDelay = 6; // delay for the startup animation 146 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 147 int swapLeftRight = false; // swap the left and right input values or not 148 149 int dropDelay = 5; // hold time before dropping the leds 150 float dropFactor = .94; // value for dropping the leds 151 152 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) 153 int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak 154 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 155 float peakDropFactor = .94; // value for dropping the peaks 156 int droppingPeakFade = false; // display the dropping peak fading to black or not 157 158 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 159 int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks 160 int bouncingPeakDelay = 4; // delay between peak bounce updates 161 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 162 163#elif defined (led_ring_60_ps) 164 //settings for a 60 led ring - pulsating and spinning settings 165 166 int stripNumOfLeds = 60; // the total number of leds 167 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 168 uint32_t stripColor[31]; // half of the number of leds + 1 169 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 170 int splitStrip = true; // set to true when using 2 strips or rings, false for one strip 171 int middleOffset = 0; // offset for the middle led when using one strip 172 int startupAnimationDelay = 6; // delay for the startup animation 173 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 174 int swapLeftRight = false; // swap the left and right input values or not 175 176 int dropDelay = 5; // hold time before dropping the leds 177 float dropFactor = .93; // value for dropping the leds 178 179 int peakTimeNoDropDelay = 10; // peak hold time when not dropping the peaks (when droppingPeak is false) 180 int peakTimeFirstDropDelay = 10; // peak hold time when dropping the first peak 181 int peakTimeDropDelay = 6; // peak hold time when dropping the rest of the peaks 182 float peakDropFactor = .92; // value for dropping the peaks 183 int droppingPeakFade = false; // display the dropping peak fading to black or not 184 185 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 186 int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks 187 int bouncingPeakDelay = 4; // delay between peak bounce updates 188 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 189 190#elif defined (led_rhombus_160_ps) 191 //settings for a 160 led rhombus - pulsating and spinning settings 192 193 int stripNumOfLeds = 160; // the total number of leds 194 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 195 uint32_t stripColor[81]; // half of the number of leds + 1 196 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 197 int splitStrip = true; // set to true when using 2 strips or rings, false for one strip 198 int middleOffset = 0; // offset for the middle led when using one strip 199 int startupAnimationDelay = 0; // delay for the startup animation 200 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 201 int swapLeftRight = false; // swap the left and right input values or not 202 203 int dropDelay = 4; // hold time before dropping the leds 204 float dropFactor = .92; // value for dropping the leds 205 206 int peakTimeNoDropDelay = 150; // peak hold time when not dropping the peaks (when droppingPeak is false) 207 int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak 208 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 209 float peakDropFactor = .94; // value for dropping the peaks 210 int droppingPeakFade = false; // display the dropping peak fading to black or not 211 212 int bouncingPeaksNumOfLeds = 10; // how many leds to bounce up (max) 213 int bouncingPeaksNumOfLedsMin = 5; // how many leds to bounce up (min) when using dynamicBouncingPeaks 214 int bouncingPeakDelay = 4; // delay between peak bounce updates 215 int bouncingPeakCounterInc = 6; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 216 217#elif defined (led_strip_60) 218 //settings for a 60 led strip 219 220 int stripNumOfLeds = 60; // the total number of leds 221 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 222 uint32_t stripColor[31]; // half of the number of leds + 1 223 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 224 int splitStrip = false; // set to true when using 2 strips or rings, false for one strip 225 int middleOffset = 1; // offset for the middle led when using one strip 226 int startupAnimationDelay = 6; // delay for the startup animation 227 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 228 int swapLeftRight = false; // swap the left and right input values or not 229 230 int dropDelay = 5; // hold time before dropping the leds 231 float dropFactor = .94; // value for dropping the leds 232 233 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (when droppingPeak is false) 234 int peakTimeFirstDropDelay = 70; // peak hold time when dropping the first peak 235 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 236 float peakDropFactor = .94; // value for dropping the peaks 237 int droppingPeakFade = false; // display the dropping peak fading to black or not 238 239 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 240 int bouncingPeaksNumOfLedsMin = 3; // how many leds to bounce up (min) when using dynamicBouncingPeaks 241 int bouncingPeakDelay = 4; // delay between peak bounce updates 242 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 243 244#elif defined (led_strip_60_qr) 245 //settings for a 60 led strip - quick response 246 247 int stripNumOfLeds = 60; // the total number of leds 248 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 249 uint32_t stripColor[31]; // half of the number of leds + 1 250 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 251 int splitStrip = false; // set to true when using 2 strips or rings, false for one strip 252 int middleOffset = 1; // offset for the middle led when using one strip 253 int startupAnimationDelay = 6; // delay for the startup animation 254 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 255 int swapLeftRight = false; // swap the left and right input values or not 256 257 int dropDelay = 4; // hold time before dropping the leds 258 float dropFactor = .92; // value for dropping the leds 259 260 int peakTimeNoDropDelay = 0; // peak hold time when not dropping the peaks (when droppingPeak is false) 261 int peakTimeFirstDropDelay = 0; // peak hold time when dropping the first peak 262 int peakTimeDropDelay = 0; // peak hold time when dropping the rest of the peaks 263 float peakDropFactor = 1; // value for dropping the peaks 264 int droppingPeakFade = false; // display the dropping peak fading to black or not 265 266 int bouncingPeaksNumOfLeds = 0; // how many leds to bounce up (max) 267 int bouncingPeaksNumOfLedsMin = 0; // how many leds to bounce up (min) when using dynamicBouncingPeaks 268 int bouncingPeakDelay = 0; // delay between peak bounce updates 269 int bouncingPeakCounterInc = 180; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 270 271#elif defined (led_strip_30) 272 //settings for a 30 led strip 273 274 int stripNumOfLeds = 30; // the total number of leds 275 int stripsOn2Pins = false; // set to true if the LED strips or rings are connected to 2 input pins 276 uint32_t stripColor[16]; // half of the number of leds + 1 277 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 278 int splitStrip = false; // set to true when using 2 strips or rings, false for one strip 279 int middleOffset = 1; // offset for the middle led when using one strip 280 int startupAnimationDelay = 10; // delay for the startup animation 281 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 282 int swapLeftRight = false; // swap the left and right input values or not 283 284 int dropDelay = 10; // hold time before dropping the leds 285 float dropFactor = .9; // value for dropping the leds 286 287 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) 288 int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak 289 int peakTimeDropDelay = 15; // peak hold time when dropping the rest of the peaks 290 float peakDropFactor = .94; // value for dropping the peaks 291 int droppingPeakFade = false; // display the dropping peak fading to black or not 292 293 int bouncingPeaksNumOfLeds = 4; // how many leds to bounce up (max) 294 int bouncingPeaksNumOfLedsMin = 2; // how many leds to bounce up (min) when using dynamicBouncingPeaks 295 int bouncingPeakDelay = 4; // delay between peak bounce updates 296 int bouncingPeakCounterInc = 9; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 297 298#elif defined (led_2_rings_24) 299 //settings for 2 24 led rings 300 301 int stripNumOfLeds = 48; 302 int stripsOn2Pins = false; 303 uint32_t stripColor[25]; 304 int displayMiddleLed = false; 305 int splitStrip = true; 306 int middleOffset = 0; 307 int startupAnimationDelay = 5; 308 int orangeLimitAmount = 0; 309 int swapLeftRight = false; 310 311 int dropDelay = 2; 312 float dropFactor = .96; 313 314 int peakTimeNoDropDelay = 250; 315 int peakTimeFirstDropDelay = 100; 316 int peakTimeDropDelay = 10; 317 float peakDropFactor = .94; 318 int droppingPeakFade = false; 319 320 int bouncingPeaksNumOfLeds = 4; 321 int bouncingPeaksNumOfLedsMin = 2; 322 int bouncingPeakDelay = 4; 323 int bouncingPeakCounterInc = 9; 324 325#elif defined(led_2_rings_30) 326 //settings for 2 30 led rings 327 328 int stripNumOfLeds = 60; 329 int stripsOn2Pins = false; 330 uint32_t stripColor[31]; 331 int displayMiddleLed = false; 332 int splitStrip = true; 333 int middleOffset = 0; 334 int startupAnimationDelay = 5; 335 int orangeLimitAmount = 0; 336 int swapLeftRight = false; 337 338 int dropDelay = 2; 339 float dropFactor = .96; 340 341 int peakTimeNoDropDelay = 250; 342 int peakTimeFirstDropDelay = 100; 343 int peakTimeDropDelay = 10; 344 float peakDropFactor = .94; 345 int droppingPeakFade = false; 346 347 int bouncingPeaksNumOfLeds = 4; 348 int bouncingPeaksNumOfLedsMin = 2; 349 int bouncingPeakDelay = 4; 350 int bouncingPeakCounterInc = 9; 351 352#elif defined (led_strip_200) 353 //settings for a 200 led strip 354 355 int stripNumOfLeds = 200; 356 int stripsOn2Pins = false; 357 uint32_t stripColor[101]; 358 int displayMiddleLed = false; 359 int splitStrip = true; 360 int middleOffset = 0; 361 int startupAnimationDelay = 1; 362 int orangeLimitAmount = 0; 363 int swapLeftRight = false; 364 365 int dropDelay = 10; 366 float dropFactor = .96; 367 368 int peakTimeNoDropDelay = 250; 369 int peakTimeFirstDropDelay = 100; 370 int peakTimeDropDelay = 30; 371 float peakDropFactor = .99; 372 int droppingPeakFade = false; 373 374 int bouncingPeaksNumOfLeds = 8; 375 int bouncingPeaksNumOfLedsMin = 4; 376 int bouncingPeakDelay = 4; 377 int bouncingPeakCounterInc = 9; 378 379#elif defined (led_strip_144) 380 //settings for a 144 led strip 381 382 int stripNumOfLeds = 145; 383 int stripsOn2Pins = false; 384 uint32_t stripColor[73]; 385 int displayMiddleLed = true; 386 int splitStrip = false; 387 int middleOffset = 1; 388 int startupAnimationDelay = 1; 389 int orangeLimitAmount = 0; 390 int swapLeftRight = false; 391 392 int dropDelay = 4; 393 float dropFactor = .92; 394 395 int peakTimeNoDropDelay = 250; 396 int peakTimeFirstDropDelay = 100; 397 int peakTimeDropDelay = 5; 398 float peakDropFactor = .94; 399 int droppingPeakFade = false; 400 401 int bouncingPeaksNumOfLeds = 10; 402 int bouncingPeaksNumOfLedsMin = 4; 403 int bouncingPeakDelay = 2; 404 int bouncingPeakCounterInc = 10; 405 406#elif defined (led_2_strip_63) 407 //settings for 2 63 led strips 408 409 int stripNumOfLeds = 63; 410 int stripsOn2Pins = true; 411 uint32_t stripColor[64]; 412 int displayMiddleLed = true; 413 int splitStrip = true; 414 int middleOffset = 0; 415 int startupAnimationDelay = 1; 416 int orangeLimitAmount = 0; 417 int swapLeftRight = false; 418 419 int dropDelay = 5; 420 float dropFactor = .94; 421 422 int peakTimeNoDropDelay = 250; 423 int peakTimeFirstDropDelay = 70; 424 int peakTimeDropDelay = 7; 425 float peakDropFactor = .94; 426 int droppingPeakFade = false; 427 428 int bouncingPeaksNumOfLeds = 12; 429 int bouncingPeaksNumOfLedsMin = 4; 430 int bouncingPeakDelay = 4; 431 int bouncingPeakCounterInc = 10; 432 433#elif defined (led_2_strip_63_qr) 434 //settings for 2 63 led strips - quick response 435 436 int stripNumOfLeds = 63; 437 int stripsOn2Pins = true; 438 uint32_t stripColor[64]; 439 int displayMiddleLed = false; 440 int splitStrip = true; 441 int middleOffset = 0; 442 int startupAnimationDelay = 1; 443 int orangeLimitAmount = 0; 444 int swapLeftRight = false; 445 446 int dropDelay = 4; 447 float dropFactor = .92; 448 449 int peakTimeNoDropDelay = 200; 450 int peakTimeFirstDropDelay = 60; 451 int peakTimeDropDelay = 6; 452 float peakDropFactor = .92; 453 int droppingPeakFade = false; 454 455 int bouncingPeaksNumOfLeds = 12; 456 int bouncingPeaksNumOfLedsMin = 4; 457 int bouncingPeakDelay = 3; 458 int bouncingPeakCounterInc = 10; 459#endif 460 461// 462// setup other user variables 463// 464 465// basic settings 466int pulsing = true; // pulsing will display from the middle of each strip or ring @EB 467 468int spinCircle = true; // spin the animation. will not work with stripsOn2Pins @EB 469 470int animType = 0; // startup animation selection (1 looks nice for 1 ring) @EB 471int colorScheme = 10; // 0: green-red, 1: blue-green, 2: blue-red, 3: red-blue, 4: green-blue, 5: red-green, 6: blue-white-red 472 // 7: red-white-blue, 8: green-white-red, 9: green-white-blue, 10: color wheel, 11: spinning color wheel, 473 // 12: as 11 but spread with factor colorScheme12Factor @EB 474int maxColorScheme = 12; // used for looping through the color schemes with the switch button 475int colorScheme11SpinDelay = stripNumOfLeds / 4 ; // delay for spinning scheme 11 476int colorScheme12Factor = 3; // wheel spread factor for scheme 12 @EB 477 478int minValue = 10; // min analog input value 479int sensitivityValue = 110; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) 480 481#ifdef highLevelInput 482 int maxValue = 700; // max analog input value (0-1023 equals 0-5V). try 300 for low level input, 700 for high 483 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 484#else 485 int maxValue = 300; // max analog input value (0-1023 equals 0-5V). try 300 for low level input, 700 for high 486 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 487#endif 488 489int ledBrightness = 30; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) 490int sensorDeviationBrightness = 3; // eliminate fluctuating values 491int overflowDelay = 10; // overflow hold time 492 493// peak settings @EB 494int displayPeaks = false; // value will be set by the switch if useSensorValues = true 495int displayTopAsPeak = true; // always display the top LED in peak color 496int droppingPeak = true; // display dropping peaks or not. note: displayPeaks has to be true 497int bouncingPeaks = false; // display bouncing peaks or not. note: displayPeaks has to be true 498int dynamicBouncingPeaks = false; // bounce less with lower peaks. note: bouncingPeaks has to be true 499 500// 501// initialize other variables 502// 503 504int numOfSegments, halfNumOfSegments, stripMiddle, maxDisplaySegments; 505float sensitivityFactor; 506float nonLinearResponseFactor; 507 508int brightnessValue, prevBrightnessValue; 509float ledFactor, ledFactor_div_numOfSegments; 510 511uint32_t stripMiddleColor, stripOverflowColor, stripHoldColor; 512uint32_t colorValue; 513 514int leftValue = 0, rightValue = 0, maxReadValue = 0; 515int leftValueN = 0, rightValueN = 0; 516int leftAnalogValue = 0, rightAnalogValue = 0; 517float log10MaxDisplaySegments; 518 519int prevLeftValue = 0, prevRightValue = 0; 520int prevLeftAnalogValue = 0, prevRightAnalogValue = 0; 521 522int selectButton1PinState = 0, prevSelectButton1PinState = 0; 523int selectButton2PinState = 0, prevSelectButton2PinState = 0; 524 525int selectButton1PinSetting = colorScheme; 526int selectButton2PinSetting = 0; 527 528int i, j; 529int dropLeft, dropRight; 530int leftDropTime, rightDropTime; 531 532int leftPeak = 0, rightPeak = 0; 533int leftPeakTime = 0, rightPeakTime = 0; 534int leftFirstPeak = true, rightFirstPeak = true; 535int showPeaksPinSetting, prevShowPeaksPinSetting; 536 537int stripPulseMiddle = 0; 538int halfLeftValue, halfRightValue, halfPrevLeftValue, halfPrevRightValue; 539 540int leftPeakBouncing = false, rightPeakBouncing = false; 541int leftPeakBounce = 0, rightPeakBounce = 0; 542int prevLeftPeakBounce = 0, prevRightPeakBounce = 0; 543int leftPeakBounceCounter = 0, rightPeakBounceCounter = 0; 544int leftPeakBounceDelayCounter = 0, rightPeakBounceDelayCounter = 0; 545int leftBouncingPeaksNumOfLeds = 0, rightBouncingPeaksNumOfLeds = 0; 546float bounceFactor; 547 548int colorScheme11SpinValue = 0, colorScheme11SpinDelayValue = 0; 549int colorSchemeFactor = 1; 550long selectButton1Timer; 551 552int spinDelayCounter = 0, spinCounter = 0, spinTurnsCounter = 0, spinTurnsMax = 0, spinTurnsDelay = 0, spinTurnsDelayMax = 0; 553int spinCounterInc = 1; 554int spinDelay = 0; 555// 556// initialize the strip or rings 557// 558 559Adafruit_NeoPixel left_strip = Adafruit_NeoPixel(stripNumOfLeds, leftStripPin, NEO_GRB + NEO_KHZ800); 560Adafruit_NeoPixel right_strip = Adafruit_NeoPixel(stripNumOfLeds, rightStripPin, NEO_GRB + NEO_KHZ800); 561 562// 563// setup 564// 565 566void setup() { 567 #ifdef DEBUG 568 Serial.begin(9600); 569 #endif 570 571 randomSeed(analogRead(2)); 572 573 if (stripsOn2Pins) { 574 numOfSegments = stripNumOfLeds; 575 maxDisplaySegments = numOfSegments - 1; 576 577 stripMiddle = stripNumOfLeds; 578 stripPulseMiddle = stripMiddle / 2; 579 spinCircle = false; 580 } 581 else { 582 numOfSegments = stripNumOfLeds / 2; 583 stripMiddle = stripNumOfLeds / 2; 584 maxDisplaySegments = stripMiddle - 1; 585 586 stripPulseMiddle = stripMiddle / 2; 587 } 588 589 halfNumOfSegments = numOfSegments / 2; 590 bounceFactor = (float) bouncingPeaksNumOfLeds / (maxDisplaySegments - bouncingPeaksNumOfLeds); 591 nonLinearResponseFactor = 90 / (float) maxDisplaySegments; 592 log10MaxDisplaySegments = log10(maxDisplaySegments); 593 594 pinMode(showPeaksPin, INPUT); 595 596 if (useSelectButton1) 597 pinMode(selectButton1Pin, INPUT); 598 599 left_strip.begin(); 600 if (stripsOn2Pins) 601 right_strip.begin(); 602 603 if (useSensorValues) { 604 readSensorValues(); 605 setInitialDisplayPeaks(); 606 } 607 else { 608 setStripColors(); 609 setSensitivityFactor(); 610 } 611 612 #ifdef DEBUG_TEST_LEDS 613 displayTest(); 614 #endif 615 616 startupAnimation(); 617} 618 619// 620// main loop 621// 622 623void loop() { 624 #ifdef DEBUG_PRINT_LOOP_TIME 625 long time = millis(); 626 #endif 627 628 629 if (useSensorValues) 630 readSensorValues(); 631 632 readValues(); 633 634 #if defined (DEBUG_NO_PEAKS) 635 displayPeaks = false; 636 #endif 637 638 #if defined (DEBUG_PEAKS) 639 displayPeaks = true; 640 #endif 641 642 if (pulsing) { 643 drawPulsingValues(); 644 } 645 else { 646 drawValues(); 647 if (displayPeaks) { 648 getPeaks(); 649 drawPeaks(); 650 } 651 } 652 653 left_strip.show(); 654 if (stripsOn2Pins) 655 right_strip.show(); 656 657 storePrevValues(); 658 659 checkSpinCircle(); 660 661 #ifdef DEBUG_PRINT_LOOP_TIME 662 time = millis() - time; 663 Serial.println(time); 664 #endif 665} 666 667// 668// functions 669// 670 671void setInitialDisplayPeaks() { 672 #if !defined (DEBUG_NO_PEAK_SWITCH) 673 showPeaksPinSetting = digitalRead(showPeaksPin); 674 675 if (showPeaksPinSetting == HIGH) 676 displayPeaks = false; 677 #endif 678 679 if (reverseShowPeaks) { 680 if (!displayPeaks) 681 displayPeaks = true; 682 else 683 displayPeaks = false; 684 } 685 686 prevShowPeaksPinSetting = showPeaksPinSetting; 687} 688 689void readSensorValues() { 690 // 691 // peaks pin 692 // 693 694 #if !defined (DEBUG_NO_PEAK_SWITCH) 695 showPeaksPinSetting = digitalRead(showPeaksPin); 696 697 if (showPeaksMomentarySwitch) { 698 if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { 699 if (displayPeaks == true) { 700 displayPeaks = false; 701 clearLeftPeak(); 702 clearRightPeak(); 703 if (showPeaksMomentarySwitch) 704 while (digitalRead(showPeaksPin) == LOW) {} 705 } 706 else { 707 displayPeaks = true; 708 } 709 } 710 } 711 else { 712 if (reverseShowPeaks) { 713 if (showPeaksPinSetting == HIGH && prevShowPeaksPinSetting == LOW) 714 displayPeaks = true; 715 else if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { 716 displayPeaks = false; 717 clearLeftPeak(); 718 clearRightPeak(); 719 } 720 } 721 else { 722 if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) 723 displayPeaks = true; 724 else if (showPeaksPinSetting == HIGH && prevShowPeaksPinSetting == LOW) { 725 displayPeaks = false; 726 clearLeftPeak(); 727 clearRightPeak(); 728 } 729 } 730 } 731 if (pulsing) { 732 if (displayPeaks) 733 displayTopAsPeak = true; 734 else 735 displayTopAsPeak = false; 736 } 737 738 prevShowPeaksPinSetting = showPeaksPinSetting; 739 #endif 740 741 742 // 743 // selectButtonPin 1 and 2 744 // 745 if (useSelectButton1) { 746 selectButton1PinState = digitalRead(selectButton1Pin); 747 748 if (selectButton1PinState == HIGH && prevSelectButton1PinState == LOW) 749 selectButton1Timer = millis(); 750 751 if (selectButton1PinState == HIGH && prevSelectButton1PinState == HIGH) { 752 if ((millis() - selectButton1Timer) > 1000) { 753 pulsing = !pulsing; 754 setStripColors(); 755 displayNumber(colorScheme, 250); 756 757 while (digitalRead(selectButton1Pin) == HIGH) {} 758 selectButton1PinState = LOW; 759 clearValues(); 760 } 761 } 762 else if (selectButton1PinState == LOW && prevSelectButton1PinState == HIGH) { 763 selectButton1PinSetting++; 764 if (selectButton1PinSetting > maxColorScheme) { 765 selectButton1PinSetting = 0; 766 } 767 colorScheme = selectButton1PinSetting; 768 769 if (colorScheme == 12) 770 colorScheme11SpinValue = (colorScheme11SpinValue * colorScheme12Factor); 771 772 setStripColors(); 773 displayNumber(colorScheme, 250); 774 } 775 prevSelectButton1PinState = selectButton1PinState; 776 } 777 778 if (useSelectButton2) { 779 selectButton2PinState = digitalRead(selectButton2Pin); 780 781 if (selectButton2PinState == HIGH && prevSelectButton2PinState == LOW) { 782 selectButton2PinSetting++; 783 784 switch(selectButton2PinSetting) { 785 case 0: 786 case 3: { 787 pulsing = false; 788 spinCircle = false; 789 selectButton2PinSetting = 0; 790 break; 791 } 792 case 1: { 793 pulsing = true; 794 spinCircle= false; 795 break; 796 } 797 case 2: { 798 pulsing = true; 799 spinCircle = true; 800 break; 801 } 802 } 803 804 setStripColors(); 805 displayNumber(colorScheme, 250); 806 } 807 808 prevSelectButton2PinState = selectButton2PinState; 809 } 810 811 // 812 // brightness 813 // 814 brightnessValue = analogRead(brightnessPin); 815 brightnessValue = map(brightnessValue, 0, 1023, 0, 255); 816 817 if (abs(brightnessValue - prevBrightnessValue) > sensorDeviationBrightness) { 818 ledBrightness = brightnessValue; 819 setStripColors(); 820 prevBrightnessValue = brightnessValue; 821 } 822 823 // 824 // colorscheme 11 spinning wheel 825 // 826 827 if (colorScheme == 11 || colorScheme == 12) { 828 colorScheme11SpinDelayValue++; 829 if (colorScheme11SpinDelayValue == colorScheme11SpinDelay) { 830 colorScheme11SpinDelayValue = 0; 831 colorScheme11SpinValue++; 832 if (colorScheme11SpinValue > maxDisplaySegments * colorSchemeFactor) 833 colorScheme11SpinValue = 0; 834 setStripColors(); 835 } 836 } 837 838 // 839 // sensitivity 840 // 841 sensitivityValue = analogRead(sensitivityPin); 842 sensitivityValue = map(sensitivityValue, 0, 1023, 0, 255); 843 setSensitivityFactor(); 844} 845 846void setSensitivityFactor() { 847 //sensitivityValue_div_numOfSegments = sensitivityValue / numOfSegments; 848 sensitivityFactor = ((float) sensitivityValue / 255 * (float) maxSensitivity / 255); 849} 850 851void readValues() { 852 #ifdef averageReadings 853 leftAnalogValue = 0; 854 rightAnalogValue = 0; 855 856 for (i = 0; i <= averageNumOfReadings; i++) { 857 leftAnalogValue += analogRead(leftPin); 858 rightAnalogValue += analogRead(rightPin); 859 } 860 861 leftAnalogValue /= averageNumOfReadings; 862 rightAnalogValue /= averageNumOfReadings; 863 864 #else 865 leftAnalogValue = analogRead(leftPin); 866 rightAnalogValue = analogRead(rightPin); 867 #endif 868 869 if (swapLeftRight) { 870 int tempValue = leftAnalogValue; 871 leftAnalogValue = rightAnalogValue; 872 rightAnalogValue = tempValue; 873 } 874 875 if (leftAnalogValue < prevLeftAnalogValue) { 876 leftDropTime++; 877 if (leftDropTime > dropDelay) { 878 leftAnalogValue = prevLeftAnalogValue * dropFactor; 879 leftDropTime = 0; 880 } 881 else 882 leftAnalogValue = prevLeftAnalogValue; 883 } 884 885 if (rightAnalogValue < prevRightAnalogValue) { 886 rightDropTime++; 887 if (rightDropTime > dropDelay) { 888 rightAnalogValue = prevRightAnalogValue * dropFactor; 889 rightDropTime = 0; 890 } 891 else 892 rightAnalogValue = prevRightAnalogValue; 893 } 894 895 #ifdef DEBUG_PRINT_ANALOGVALUES 896 Serial.print(leftAnalogValue); 897 Serial.print(" "); 898 Serial.println(rightAnalogValue); 899 #endif 900 901 // map values 902 leftValue = map(leftAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); 903 rightValue = map(rightAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); 904 905 // if defined, convert to (reverse) non linear response 906 boolean flagNonLinear = false; 907 908 #if defined (nonLinearSinAudio) 909 flagNonLinear = true; 910 leftValueN = ((sin(((leftValue * nonLinearResponseFactor) + 270) * 0.0174533) + 1) * maxDisplaySegments); 911 rightValueN = ((sin(((rightValue * nonLinearResponseFactor) + 270) * 0.0174533) + 1) * maxDisplaySegments); 912 913 #elif defined (nonLinearReverseSinAudio) 914 flagNonLinear = true; 915 leftValueN = ((sin(((leftValue * nonLinearResponseFactor)) * 0.0174533)) * maxDisplaySegments); 916 rightValueN = ((sin(((rightValue * nonLinearResponseFactor)) * 0.0174533)) * maxDisplaySegments); 917 918 #elif defined (nonLinearLogAudio) 919 flagNonLinear = true; 920 leftValueN = ((log10(leftValue + 1) / log10MaxDisplaySegments * maxDisplaySegments)); 921 rightValueN = ((log10(rightValue + 1) / log10MaxDisplaySegments * maxDisplaySegments)); 922 923 #endif 924 925 if (flagNonLinear == true) { 926 #if defined (nonLinearAvr2) 927 leftValue = (leftValue + leftValueN) / 2; 928 rightValue = (rightValue + rightValueN) / 2; 929 #else 930 leftValue = leftValueN; 931 rightValue = rightValueN; 932 #endif 933 } 934 935// @EB_DEBUG 936 937 #ifdef displayOverflow 938 #ifdef compressOverflowPeaks 939 for (i = 1; i <= compressOverflowNumOfTimes; i++) { 940 if (leftValue > maxDisplaySegments) { 941// Serial.print(i); 942// Serial.print(" "); 943// Serial.print(leftValue); 944// Serial.print(" "); 945 leftValue = leftValue - leftValue * compressOverflowFactor * i; 946// Serial.print(leftValue); 947// Serial.print(" "); 948// Serial.println(maxDisplaySegments); 949 } 950 } 951 #endif 952 #endif 953 954 if (leftValue > maxDisplaySegments) { 955 leftValue = maxDisplaySegments; 956 #ifdef displayOverflow 957 drawOverflow(); 958 #endif 959 } 960 961 #ifdef displayOverflow 962 #ifdef compressOverflowPeaks 963 if (rightValue > maxDisplaySegments) 964 rightValue = rightValue - rightValue * compressOverflowFactor; 965 #endif 966 #endif 967 968 if (rightValue > maxDisplaySegments) { 969 rightValue = maxDisplaySegments; 970 #ifdef displayOverflow 971 drawOverflow(); 972 #endif 973 } 974} 975 976void storePrevValues() { 977 prevLeftAnalogValue = leftAnalogValue; 978 prevRightAnalogValue = rightAnalogValue; 979 980 prevLeftValue = leftValue; 981 prevRightValue = rightValue; 982} 983 984void getPeaks() { 985 if (leftValue > leftPeak) { 986 if (dynamicBouncingPeaks || prevLeftPeakBounce > 0) 987 clearLeftBouncePeak(); 988 989 leftPeak = leftValue; 990 leftPeakTime = 0; 991 leftFirstPeak = true; 992 993 if (bouncingPeaks) { 994 leftPeakBouncing = true; 995 leftPeakBounceCounter = 0; 996 leftPeakBounceDelayCounter = 0; 997 998 if (dynamicBouncingPeaks) 999 leftBouncingPeaksNumOfLeds = max(bouncingPeaksNumOfLedsMin, (leftPeak * bounceFactor)); 1000 else 1001 leftBouncingPeaksNumOfLeds = bouncingPeaksNumOfLeds; 1002 } 1003 } 1004 else { 1005 leftPeakTime++; 1006 if (droppingPeak) { 1007 if (leftFirstPeak) { 1008 if (leftPeakTime > peakTimeFirstDropDelay) { 1009 clearLeftPeak(); 1010 leftFirstPeak = false; 1011 } 1012 } 1013 else { 1014 if (leftPeakTime > peakTimeDropDelay) { 1015 clearLeftPeak(); 1016 } 1017 } 1018 } 1019 else { 1020 if (leftPeakTime > peakTimeNoDropDelay) { 1021 clearLeftPeak(); 1022 } 1023 } 1024 } 1025 1026 if (leftPeakBouncing) { 1027 if (leftFirstPeak) { 1028 leftPeakBounceDelayCounter++; 1029 if (leftPeakBounceDelayCounter >= bouncingPeakDelay) { 1030 leftPeakBounceDelayCounter = 0; 1031 leftPeakBounceCounter += bouncingPeakCounterInc; 1032 if (leftPeakBounceCounter >= 180) { 1033 clearLeftBouncePeak(); 1034 clearLeftBounce(); 1035 } 1036 else { 1037 leftPeakBounce = min((sin(leftPeakBounceCounter * 0.0174533) * leftBouncingPeaksNumOfLeds), (maxDisplaySegments - leftPeak)); 1038 if (leftPeakBounce != prevLeftPeakBounce) { 1039 clearLeftBouncePeak(); 1040 } 1041 prevLeftPeakBounce = leftPeakBounce; 1042 } 1043 } 1044 } 1045 } 1046 1047 if (rightValue > rightPeak) { 1048 if (dynamicBouncingPeaks || prevRightPeakBounce > 0) 1049 clearRightBouncePeak(); 1050 1051 rightPeak = rightValue; 1052 rightPeakTime = 0; 1053 rightFirstPeak = true; 1054 1055 if (bouncingPeaks) { 1056 rightPeakBouncing = true; 1057 rightPeakBounceCounter = 0; 1058 rightPeakBounceDelayCounter = 0; 1059 1060 if (dynamicBouncingPeaks) 1061 rightBouncingPeaksNumOfLeds = max(bouncingPeaksNumOfLedsMin, (rightPeak * bounceFactor)); 1062 else 1063 rightBouncingPeaksNumOfLeds = bouncingPeaksNumOfLeds; 1064 } 1065 } 1066 else { 1067 rightPeakTime++; 1068 if (droppingPeak) { 1069 if (rightFirstPeak) { 1070 if (rightPeakTime > peakTimeFirstDropDelay) { 1071 clearRightPeak(); 1072 rightFirstPeak = false; 1073 } 1074 } 1075 else { 1076 if (rightPeakTime > peakTimeDropDelay) 1077 clearRightPeak(); 1078 } 1079 } 1080 else { 1081 if (rightPeakTime > peakTimeNoDropDelay) 1082 clearRightPeak(); 1083 } 1084 } 1085 1086 if (rightPeakBouncing) { 1087 if (rightFirstPeak) { 1088 rightPeakBounceDelayCounter++; 1089 if (rightPeakBounceDelayCounter >= bouncingPeakDelay) { 1090 rightPeakBounceDelayCounter = 0; 1091 rightPeakBounceCounter += bouncingPeakCounterInc; 1092 1093 if (rightPeakBounceCounter >= 180) { 1094 clearRightBouncePeak(); 1095 clearRightBounce(); 1096 } 1097 else { 1098 rightPeakBounce = min((sin(rightPeakBounceCounter * 0.0174533) * rightBouncingPeaksNumOfLeds), (maxDisplaySegments - rightPeak)); 1099 if (rightPeakBounce != prevRightPeakBounce) { 1100 clearRightBouncePeak(); 1101 } 1102 prevRightPeakBounce = rightPeakBounce; 1103 } 1104 } 1105 } 1106 } 1107} 1108 1109void checkSpinCircle () { 1110 if (spinCircle) { 1111 if (spinTurnsMax == 0) { 1112 spinTurnsMax = random(stripNumOfLeds / 4, stripNumOfLeds * 3); // spin at least a quarter turn, max 3 turns 1113 1114 if (random(10) > 4) 1115 spinCounterInc = -spinCounterInc; 1116 1117 spinTurnsDelayMax = random(100, 1000); // @EB_DEBUG 1118 1119 spinDelay = random(20, 75); // @EB_DEBUG 1120 } 1121 1122 if (spinTurnsCounter == spinTurnsMax) { 1123 spinTurnsDelay++; 1124 if (spinTurnsDelay == spinTurnsDelayMax) { 1125 spinTurnsDelay = 0; 1126 spinTurnsCounter = 0; 1127 spinTurnsMax = 0; 1128 } 1129 } 1130 else { 1131 spinDelayCounter++; 1132 1133 if (spinDelayCounter > spinDelay) { 1134 clearZeroAndPeaks(); 1135 1136 spinCounter += spinCounterInc; 1137 if (spinCounter > stripNumOfLeds) 1138 spinCounter = 0; 1139 else if (spinCounter < 0) 1140 spinCounter = stripNumOfLeds; 1141 1142 spinTurnsCounter++; 1143 spinDelayCounter = 0; 1144 } 1145 } 1146 } 1147} 1148 1149int getSpinCircleValue(int value) { 1150 if (!spinCircle) 1151 return value; 1152 else { 1153 int calcValue = value + spinCounter; 1154 if (calcValue >= stripNumOfLeds) 1155 calcValue -= stripNumOfLeds; 1156 return calcValue; 1157 } 1158} 1159 1160void drawValues() { 1161 if (splitStrip) { 1162 for (i = middleOffset; i < leftValue; i++) 1163 left_strip.setPixelColor(getSpinCircleValue(i), stripColor[i]); 1164 1165 if (!displayPeaks && displayTopAsPeak) 1166 left_strip.setPixelColor(getSpinCircleValue(leftValue), stripHoldColor); 1167 1168 for (i = prevLeftValue; i >= leftValue; i--) 1169 left_strip.setPixelColor(getSpinCircleValue(i), 0); 1170 1171 if (stripsOn2Pins) { 1172 for (i = middleOffset; i < rightValue; i++) 1173 right_strip.setPixelColor(i, stripColor[i]); 1174 1175 if (!displayPeaks && displayTopAsPeak) 1176 right_strip.setPixelColor(rightValue, stripHoldColor); 1177 1178 for (i = prevRightValue; i >= rightValue; i--) 1179 right_strip.setPixelColor(i, 0); 1180 } 1181 else { 1182 for (i = middleOffset; i < rightValue; i++) 1183 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), stripColor[i]); 1184 1185 if (!displayPeaks && displayTopAsPeak) 1186 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + rightValue), stripHoldColor); 1187 1188 for (i = prevRightValue; i >= rightValue; i--) 1189 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), 0); 1190 } 1191 } 1192 else { 1193 for (i = middleOffset; i < leftValue; i++) 1194 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), stripColor[i]); 1195 1196 if (!displayPeaks && displayTopAsPeak) 1197 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + leftValue), stripHoldColor); 1198 1199 for (i = prevLeftValue; i >= leftValue; i--) 1200 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), 0); 1201 1202 for (i = middleOffset; i < rightValue; i++) 1203 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - i), stripColor[i]); 1204 1205 if (!displayPeaks && displayTopAsPeak) 1206 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - rightValue), stripHoldColor); 1207 1208 for (i = prevRightValue; i >= rightValue; i--) 1209 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - i), 0); 1210 } 1211 1212 if (displayMiddleLed) 1213 left_strip.setPixelColor(getSpinCircleValue(stripMiddle), stripMiddleColor); 1214} 1215 1216void drawPulsingValues() { 1217 halfLeftValue = (leftValue + 1) / 2; 1218 halfRightValue = (rightValue + 1) / 2; 1219 halfPrevLeftValue = (prevLeftValue + 1)/ 2; 1220 halfPrevRightValue = (prevRightValue + 1) / 2; 1221 1222 if (splitStrip) { 1223 for (i = 0; i < halfLeftValue; i++) { 1224 colorValue = stripColor[i * 2]; 1225 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + i), colorValue); 1226 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle - i), colorValue); 1227 } 1228 1229 if (displayTopAsPeak) { 1230 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + halfLeftValue), stripHoldColor); 1231 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle - halfLeftValue), stripHoldColor); 1232 } 1233 1234 for (i = halfPrevLeftValue; i >= halfLeftValue; i--) { 1235 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + i), 0); 1236 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle - i), 0); 1237 } 1238 1239 if (stripsOn2Pins) { 1240 for (i = 0; i < halfRightValue; i++) { 1241 colorValue = stripColor[i * 2]; 1242 right_strip.setPixelColor((stripPulseMiddle + i), colorValue); 1243 right_strip.setPixelColor((stripPulseMiddle - i), colorValue); 1244 } 1245 1246 if (displayTopAsPeak) { 1247 right_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + halfRightValue), stripHoldColor); 1248 right_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle - halfRightValue), stripHoldColor); 1249 } 1250 1251 for (i = halfPrevRightValue; i >= halfRightValue; i--) { 1252 right_strip.setPixelColor((stripPulseMiddle + i), 0); 1253 right_strip.setPixelColor((stripPulseMiddle - i), 0); 1254 } 1255 } 1256 else { 1257 for (i = 0; i < halfRightValue; i++) { 1258 colorValue = colorValue = stripColor[i * 2]; 1259 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + i), colorValue); 1260 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - i), colorValue); 1261 } 1262 1263 if (displayTopAsPeak) { 1264 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + halfRightValue), stripHoldColor); 1265 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - halfRightValue), stripHoldColor); 1266 } 1267 1268 for (i = halfPrevRightValue; i >= halfRightValue; i--) { 1269 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + i), 0); 1270 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - i), 0); 1271 } 1272 } 1273 } 1274 else { 1275 for (i = 0; i < halfLeftValue; i++) { 1276 colorValue = colorValue = stripColor[i * 2]; 1277 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + i), colorValue); 1278 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - i), colorValue); 1279 } 1280 1281 if (displayTopAsPeak) { 1282 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + halfLeftValue), stripHoldColor); 1283 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - halfLeftValue), stripHoldColor); 1284 } 1285 1286 for (i = halfPrevLeftValue; i >= halfLeftValue; i--) { 1287 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + i), 0); 1288 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - i), 0); 1289 } 1290 1291 for (i = 0; i < halfRightValue; i++) { 1292 colorValue = colorValue = stripColor[i * 2]; 1293 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle + i)), colorValue); 1294 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle - i)), colorValue); 1295 } 1296 1297 if (displayTopAsPeak) { 1298 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle + halfRightValue)), stripHoldColor); 1299 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle - halfRightValue)), stripHoldColor); 1300 } 1301 1302 for (i = halfPrevRightValue; i >= halfRightValue; i--) { 1303 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle + i)), 0); 1304 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle - i)), 0); 1305 } 1306 } 1307 1308 if (displayMiddleLed) { 1309 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - stripPulseMiddle), stripMiddleColor); 1310 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle), stripMiddleColor); 1311 } 1312} 1313 1314void clearZeroAndPeaks() { 1315 left_strip.setPixelColor(getSpinCircleValue(middleOffset), 0); 1316 left_strip.setPixelColor(getSpinCircleValue(stripMiddle), 0); 1317 1318 if (displayTopAsPeak) { 1319 if (splitStrip) { 1320 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + halfLeftValue), 0); 1321 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle - halfLeftValue), 0); 1322 1323 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + halfRightValue), 0); 1324 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - halfRightValue), 0); 1325 } 1326 else { 1327 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle + halfLeftValue), 0); 1328 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle - halfLeftValue), 0); 1329 1330 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle + halfRightValue)), 0); 1331 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle - halfRightValue)), 0); 1332 } 1333 } 1334} 1335 1336void drawPeaks() { 1337 if (leftPeak > 0) { 1338 if (droppingPeakFade && leftPeakBouncing == false) 1339 stripHoldColor = left_strip.Color(max(1, (255 * leftPeak * ledFactor_div_numOfSegments)), 0, 0); 1340 else 1341 stripHoldColor = stripColor[numOfSegments]; 1342 1343 if (splitStrip) 1344 left_strip.setPixelColor(getSpinCircleValue(leftPeak + leftPeakBounce), stripHoldColor); 1345 else 1346 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + (leftPeak + leftPeakBounce)), stripHoldColor); 1347 } 1348 1349 if (rightPeak > 0) { 1350 if (droppingPeakFade && rightPeakBouncing == false) 1351 stripHoldColor = left_strip.Color(max(1, (255 * rightPeak * ledFactor_div_numOfSegments)), 0, 0); 1352 else 1353 stripHoldColor = stripColor[numOfSegments]; 1354 1355 if (splitStrip) { 1356 if (stripsOn2Pins) { 1357 right_strip.setPixelColor(getSpinCircleValue(rightPeak + rightPeakBounce), stripHoldColor); 1358 } 1359 else { 1360 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + rightPeak + rightPeakBounce), stripHoldColor); 1361 } 1362 } 1363 else { 1364 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (rightPeak + rightPeakBounce)), stripHoldColor); 1365 } 1366 } 1367 1368 //if (leftPeak > 0 || rightPeak > 0) 1369 // left_strip.show(); 1370} 1371 1372void clearLeftPeak() { 1373 if (splitStrip) 1374 left_strip.setPixelColor(getSpinCircleValue(leftPeak + prevLeftPeakBounce), 0); 1375 else 1376 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + (leftPeak + prevLeftPeakBounce)), 0); 1377 1378 if (droppingPeak) 1379 leftPeak = leftPeak * peakDropFactor; 1380 else 1381 leftPeak = 0; 1382 leftPeakTime = 0; 1383} 1384 1385void clearRightPeak() { 1386 if (splitStrip) { 1387 if( stripsOn2Pins) { 1388 right_strip.setPixelColor(getSpinCircleValue(rightPeak + prevRightPeakBounce), 0); 1389 } 1390 else { 1391 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + rightPeak + prevRightPeakBounce), 0); 1392 } 1393 } 1394 else { 1395 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (rightPeak + prevRightPeakBounce)), 0); 1396 } 1397 1398 if (droppingPeak) 1399 rightPeak = rightPeak * peakDropFactor; 1400 else 1401 rightPeak = 0; 1402 rightPeakTime = 0; 1403} 1404 1405void clearLeftBouncePeak() { 1406 if (splitStrip) 1407 left_strip.setPixelColor(getSpinCircleValue(leftPeak + prevLeftPeakBounce), 0); 1408 else 1409 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + (leftPeak + prevLeftPeakBounce)), 0); 1410} 1411 1412void clearRightBouncePeak() { 1413 if (splitStrip) { 1414 if (stripsOn2Pins) { 1415 right_strip.setPixelColor(getSpinCircleValue(rightPeak + prevRightPeakBounce), 0); 1416 } 1417 else { 1418 left_strip.setPixelColor(getSpinCircleValue((stripMiddle + rightPeak + prevRightPeakBounce)), 0); 1419 } 1420 } 1421 else { 1422 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (rightPeak + prevRightPeakBounce)), 0); 1423 } 1424} 1425 1426void clearLeftBounce() { 1427 leftPeakBouncing = false; 1428 leftPeakBounceCounter = 0; 1429 leftPeakBounce = 0; 1430 prevLeftPeakBounce = 0; 1431 leftBouncingPeaksNumOfLeds = 0; 1432} 1433 1434void clearRightBounce() { 1435 rightPeakBouncing = false; 1436 rightPeakBounceCounter = 0; 1437 rightPeakBounce = 0; 1438 prevRightPeakBounce = 0; 1439 leftBouncingPeaksNumOfLeds = 0; 1440} 1441 1442void clearValues() { 1443 leftAnalogValue = 0; 1444 rightAnalogValue = 0; 1445 prevLeftAnalogValue = 0; 1446 prevRightAnalogValue = 0; 1447 leftPeak = 0; 1448 rightPeak = 0; 1449} 1450 1451 1452void drawOverflow() { 1453 for (i = 0; i <= numOfSegments; i++) { 1454 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - i), stripOverflowColor); 1455 if (stripsOn2Pins) { 1456 right_strip.setPixelColor(i, stripOverflowColor); 1457 } 1458 else { 1459 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), stripOverflowColor); 1460 } 1461 } 1462 left_strip.show(); 1463 if (stripsOn2Pins) 1464 right_strip.show(); 1465 1466 delay(overflowDelay); 1467 1468 for (i = 0; i <= numOfSegments; i++) { 1469 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - i), 0); 1470 if (stripsOn2Pins) { 1471 right_strip.setPixelColor(i, 0); 1472 } 1473 else { 1474 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), 0); 1475 } 1476 } 1477 left_strip.show(); 1478 if (stripsOn2Pins) 1479 right_strip.show(); 1480} 1481 1482void setStripColors() { 1483 int r, g, b; 1484 int p1, p2; 1485 1486 ledFactor = (float) ledBrightness / 255; 1487 ledFactor_div_numOfSegments = (float) ledFactor / (float) numOfSegments; 1488 stripMiddleColor = left_strip.Color(0, 0, 255 * ledFactor); 1489 1490 switch (colorScheme) { 1491 case 0: { 1492 int orangeLimit; 1493 float orangeFactor = orangeLimitAmount / halfNumOfSegments; 1494 1495 for (i = 0; i <= numOfSegments; i++) { 1496 if (i <= halfNumOfSegments) 1497 orangeLimit = (i * orangeFactor); 1498 else 1499 orangeLimit = ((numOfSegments - i) * orangeFactor); 1500 1501 stripColor[i] = left_strip.Color((255 * i * ledFactor_div_numOfSegments), ((255 - orangeLimit) * (numOfSegments - i) * ledFactor_div_numOfSegments), 0); 1502 } 1503 break; 1504 } 1505 1506 case 1: { 1507 for (i = 0; i <= numOfSegments; i++) { 1508 stripColor[i] = left_strip.Color(0, (255 * i * ledFactor_div_numOfSegments), (255 * (numOfSegments - i) * ledFactor_div_numOfSegments)); 1509 } 1510 break; 1511 } 1512 1513 case 2: { 1514 for (i = 0; i <= numOfSegments; i++) { 1515 stripColor[i] = left_strip.Color((255 * i * ledFactor_div_numOfSegments), 0, (255 * (numOfSegments - i) * ledFactor_div_numOfSegments)); 1516 } 1517 break; 1518 } 1519 1520 case 3: { 1521 for (i = 0; i <= numOfSegments; i++) { 1522 stripColor[i] = left_strip.Color((255 * (numOfSegments - i) * ledFactor_div_numOfSegments), 0, (255 * i * ledFactor_div_numOfSegments)); 1523 } 1524 break; 1525 } 1526 1527 case 4: { 1528 for (i = 0; i <= numOfSegments; i++) { 1529 stripColor[i] = left_strip.Color(0, (255 * (numOfSegments - i) * ledFactor_div_numOfSegments), (255 * i * ledFactor_div_numOfSegments)); 1530 } 1531 break; 1532 } 1533 1534 case 5: { 1535 for (i = 0; i <= numOfSegments; i++) { 1536 stripColor[i] = left_strip.Color((255 * (numOfSegments - i) * ledFactor_div_numOfSegments), (255 * i * ledFactor_div_numOfSegments), 0); 1537 } 1538 break; 1539 } 1540 1541 case 6: { 1542 for (i = 0; i <= numOfSegments; i++) { 1543 r = (255 * i * ledFactor_div_numOfSegments); 1544 g = (255 * min(i, numOfSegments - i) * ledFactor_div_numOfSegments); 1545 b = (200 * (numOfSegments - i) * ledFactor_div_numOfSegments); 1546 stripColor[i] = left_strip.Color(r, g, b); 1547 } 1548 break; 1549 } 1550 1551 case 7: { 1552 for (i = 0; i <= numOfSegments; i++) { 1553 b = (255 * i * ledFactor_div_numOfSegments); 1554 g = (255 * min(i, numOfSegments - i) * ledFactor_div_numOfSegments); 1555 r = (255 * (numOfSegments - i) * ledFactor_div_numOfSegments); 1556 stripColor[i] = left_strip.Color(r, g, b); 1557 } 1558 break; 1559 } 1560 1561 case 8: { 1562 for (i = 0; i <= numOfSegments; i++) { 1563 r = (255 * i * ledFactor_div_numOfSegments); 1564 b = (255 * min(i, numOfSegments - i) * ledFactor_div_numOfSegments); 1565 g = (255 * (numOfSegments - i) * ledFactor_div_numOfSegments); 1566 stripColor[i] = left_strip.Color(r, g, b); 1567 } 1568 break; 1569 } 1570 1571 case 9: { 1572 for (i = 0; i <= numOfSegments; i++) { 1573 b = (255 * i * ledFactor_div_numOfSegments); 1574 r = (255 * min(i, numOfSegments - i) * ledFactor_div_numOfSegments); 1575 g = (255 * (numOfSegments - i) * ledFactor_div_numOfSegments); 1576 stripColor[i] = left_strip.Color(r, g, b); 1577 } 1578 break; 1579 } 1580 1581 case 10: 1582 colorScheme11SpinValue = 0; 1583 1584 case 11: 1585 1586 case 12: { 1587 p1 = (85 * numOfSegments / 255); 1588 p2 = (170 * numOfSegments / 255); 1589 int wheel; 1590 1591 if (colorScheme == 12) 1592 colorSchemeFactor = colorScheme12Factor; 1593 else 1594 colorSchemeFactor = 1; 1595 1596 for (i = 0; i <= numOfSegments; i++) { 1597 //wheel = int(float(i + colorScheme11SpinValue) / colorSchemeFactor) % numOfSegments; // reverse wheel 1598 1599 wheel = int(float(i - colorScheme11SpinValue) / colorSchemeFactor + numOfSegments) % numOfSegments; 1600 1601 if (wheel < p1) { 1602 wheel = map(wheel, 0, p1, 0, 255); 1603 r = (wheel * ledFactor); 1604 g = ((255 - wheel) * ledFactor); 1605 b = 0; 1606 } 1607 else if (wheel < p2) { 1608 wheel = map(wheel, p1, p2, 0, 255); 1609 r = ((255 - wheel) * ledFactor); 1610 g = 0; 1611 b = (wheel * ledFactor); 1612 } 1613 else { 1614 wheel = map(wheel, p2, numOfSegments, 0, 255); 1615 r = 0; 1616 g = (wheel * ledFactor); 1617 b = ((255 - wheel) * ledFactor); 1618 } 1619 1620 stripColor[i] = left_strip.Color(r, g, b); 1621 } 1622 break; 1623 } 1624 } 1625 1626 if (colorScheme >= 10) 1627 stripHoldColor = left_strip.Color(255 * ledFactor, 0, 0); // set to red for the color wheels 1628 else 1629 stripHoldColor = stripColor[numOfSegments]; 1630 1631 stripOverflowColor = stripHoldColor; // left_strip.Color(min(255, 255 * ledFactor * 1.5), 0, 0); 1632} 1633 1634void startupAnimation() { 1635 for (j = 0; j < 2; j++) { 1636 for (i = 0; i <= numOfSegments; i++) { 1637 if (animType == 1) 1638 left_strip.setPixelColor(stripMiddle - (numOfSegments - i), stripColor[i]); 1639 else 1640 left_strip.setPixelColor(stripMiddle - i, stripColor[i]); 1641 1642 if (stripsOn2Pins) 1643 right_strip.setPixelColor(i, stripColor[i]); 1644 else 1645 left_strip.setPixelColor(stripMiddle + i, stripColor[i]); 1646 1647 left_strip.show(); 1648 if (stripsOn2Pins) 1649 right_strip.show(); 1650 1651 delay(startupAnimationDelay); 1652 } 1653 1654 for (i = 0; i <= numOfSegments; i++) { 1655 if (animType == 1) 1656 left_strip.setPixelColor(stripMiddle - (numOfSegments - i), 0); 1657 else 1658 left_strip.setPixelColor(stripMiddle - i, 0); 1659 1660 if (stripsOn2Pins) 1661 right_strip.setPixelColor(i, 0); 1662 else 1663 left_strip.setPixelColor(stripMiddle + i, 0); 1664 1665 left_strip.show(); 1666 if (stripsOn2Pins) 1667 right_strip.show(); 1668 1669 delay(startupAnimationDelay); 1670 } 1671 } 1672} 1673 1674void displayNumber (int number, int displayDelay) { 1675 left_strip.clear(); 1676 if (stripsOn2Pins) 1677 right_strip.clear(); 1678 1679 number++; // @EB_DEBUG : display value 0 at led 1 1680 1681 for (i = 0; i <= number; i++) { 1682 if (i % 5 == 0) 1683 colorValue = stripMiddleColor; 1684 else 1685 colorValue = stripColor[0]; 1686 1687 left_strip.setPixelColor(middleOffset + i, colorValue); 1688 1689 if (stripsOn2Pins) 1690 right_strip.setPixelColor(middleOffset + i, colorValue); 1691 else 1692 left_strip.setPixelColor(stripMiddle + middleOffset + i, colorValue); 1693 1694 delay(45 - number * 3); // @EB_DEBUG 1695 1696 left_strip.show(); 1697 if (stripsOn2Pins) 1698 right_strip.show(); 1699 } 1700 1701 if (pulsing) { 1702 left_strip.setPixelColor(middleOffset + maxDisplaySegments, stripMiddleColor); 1703 1704 if (stripsOn2Pins) 1705 right_strip.setPixelColor(maxDisplaySegments, stripMiddleColor); 1706 else 1707 left_strip.setPixelColor(stripMiddle + maxDisplaySegments, stripMiddleColor); 1708 1709 left_strip.show(); 1710 if (stripsOn2Pins) 1711 right_strip.show(); 1712 } 1713 1714 delay(displayDelay); 1715 1716 left_strip.clear(); 1717 if (stripsOn2Pins) 1718 right_strip.clear(); 1719} 1720 1721 1722// 1723// for debugging 1724// 1725 1726#ifdef DEBUG_TEST_LEDS 1727 void displayTest() { 1728 for (i = 0; i <= numOfSegments; i++) { 1729 left_strip.setPixelColor(stripMiddle - i, stripColor[i]); 1730 1731 if (stripsOn2Pins) 1732 right_strip.setPixelColor(i, stripColor[i]); 1733 else 1734 left_strip.setPixelColor(stripMiddle + i, stripColor[i]); 1735 1736 left_strip.show(); 1737 if (stripsOn2Pins) 1738 right_strip.show(); 1739 1740 delay(50); 1741 } 1742 delay(5000); 1743 1744 for (i = 0; i <= numOfSegments; i++) { 1745 left_strip.setPixelColor(stripMiddle - i, 0); 1746 1747 if (stripsOn2Pins) 1748 right_strip.setPixelColor(i, 0); 1749 else 1750 left_strip.setPixelColor(stripMiddle + i, 0); 1751 1752 left_strip.show(); 1753 if (stripsOn2Pins) 1754 right_strip.show(); 1755 } 1756 } 1757 1758 void serialDisplayRGB(int r, int g, int b) { 1759 Serial.print(i); 1760 Serial.print(" "); 1761 Serial.print(r); 1762 Serial.print(" "); 1763 Serial.print(g); 1764 Serial.print(" "); 1765 Serial.println(b); 1766 } 1767#endif 1768
20171105 VU meter
arduino
Updated code with optional dropping and bouncing peaks and several other settings.
1/* 2********************************************************************** 3* Stereo VU Meter for 1 or 2 LED rings or strips build by ericBcreator 4* Designed to be used with an Arduino UNO, Nano or compatible device. 5********************************************************************** 6* Last updated 20171105 by ericBcreator 7* 8* This code is free for personal use, not for commercial purposes. 9* Please leave this header intact. 10* 11* contact: ericBcreator@gmail.com 12********************************************************************** 13*/ 14 15//#define DEBUG // for debugging 16 17// 18// include the NeoPixel library: 19// 20 21#include <Adafruit_NeoPixel.h> 22 23// 24// uncomment the definition for the connected strip or ring(s) 25// 26 27//#define led_ring_60 28//#define led_strip_60 29//#define led_strip_30 30#define led_2_rings_24 31//#define led_2_rings_30 32//#define led_strip_200 33//#define led_strip_144 34 35// 36// important setting: using potentiometer sensor values or not 37// This setting has to be set right or the script will not work correctly: 38// - set this to true if using potentiometers 39// - set this to false if not using potentiometers 40// 41 42const int useSensorValues = true; 43 44// 45// setup pins 46// 47 48int leftPin = A0, rightPin = A1; // left audio in on analog 0, right on analog 1 49int brightnessPin = A4, sensitivityPin = A5; // potentiometers for brightness and sensitivity on analog 4 and 5 50int stripPin = 6; // DIN of leds on digital pin 6 51int showPeaksPin = 7; // switch to toggle peaks on or off on digital pin 7 52int momentarySwitch = false; // set false for an on/off toggle switch 53 54// 55// setup variables for the number of leds and led strip or 2 rings 56// 57 58#if defined (led_ring_60) 59 //settings for a 60 led ring 60 61 int stripNumOfLeds = 60; // the total number of leds 62 uint32_t stripColor[31]; // half of the number of leds + 1 63 int displayMiddleLed = false; // display the middle led (blue). set to true for one strip, false for two strips or rings 64 int splitStrip = true; // set to true when using 2 strips or rings, false for one strip 65 int middleOffset = 0; // offset for the middle led when using one strip 66 int startupAnimationDelay = 6; // delay for the startup animation 67 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 68 int swapLeftRight = false; // swap the left and right input values or not 69 70 int dropDelay = 4; // hold time before dropping the leds 71 float dropFactor = .92; // value for dropping the leds 72 73 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) 74 int peakTimeFirstDropDelay = 130; // peak hold time when dropping the first peak 75 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 76 float peakDropFactor = .93; // value for dropping the peaks 77 int droppingPeakFade = false; // display the dropping peak fading to black or not 78 79 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 80 int bouncingPeakDelay = 4; // delay between peak bounce updates 81 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 82 83#elif defined (led_strip_60) 84 //settings for a 60 led ring 85 86 int stripNumOfLeds = 60; // the total number of leds 87 uint32_t stripColor[31]; // half of the number of leds + 1 88 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 89 int splitStrip = false; // set to true when using 2 strips or rings, false for one strip 90 int middleOffset = 1; // offset for the middle led when using one strip 91 int startupAnimationDelay = 6; // delay for the startup animation 92 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 93 int swapLeftRight = false; // swap the left and right input values or not 94 95 int dropDelay = 4; // hold time before dropping the leds 96 float dropFactor = .92; // value for dropping the leds 97 98 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) 99 int peakTimeFirstDropDelay = 130; // peak hold time when dropping the first peak 100 int peakTimeDropDelay = 7; // peak hold time when dropping the rest of the peaks 101 float peakDropFactor = .93; // value for dropping the peaks 102 int droppingPeakFade = false; // display the dropping peak fading to black or not 103 104 int bouncingPeaksNumOfLeds = 6; // how many leds to bounce up (max) 105 int bouncingPeakDelay = 4; // delay between peak bounce updates 106 int bouncingPeakCounterInc = 10; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 107 108#elif defined (led_strip_30) 109 //settings for a 30 led strip 110 111 int stripNumOfLeds = 30; // the total number of leds 112 uint32_t stripColor[16]; // half of the number of leds + 1 113 int displayMiddleLed = true; // display the middle led (blue). set to true for one strip, false for two strips or rings 114 int splitStrip = false; // set to true when using 2 strips or rings, false for one strip 115 int middleOffset = 1; // offset for the middle led when using one strip 116 int startupAnimationDelay = 10; // delay for the startup animation 117 int orangeLimitAmount = 0; // limit the amount of green of the middle LEDs to make them more orange 118 int swapLeftRight = false; // swap the left and right input values or not 119 120 int dropDelay = 10; // hold time before dropping the leds 121 float dropFactor = .9; // value for dropping the leds 122 123 int peakTimeNoDropDelay = 250; // peak hold time when not dropping the peaks (set droppingPeak true or false) 124 int peakTimeFirstDropDelay = 150; // peak hold time when dropping the first peak 125 int peakTimeDropDelay = 15; // peak hold time when dropping the rest of the peaks 126 float peakDropFactor = .94; // value for dropping the peaks 127 int droppingPeakFade = false; // display the dropping peak fading to black or not 128 129 int bouncingPeaksNumOfLeds = 3; // how many leds to bounce up (max) 130 int bouncingPeakDelay = 4; // delay between peak bounce updates 131 int bouncingPeakCounterInc = 9; // increase counter for each bounce update. note: it uses a 0-180 sin function for the bouncing 132 133#elif defined (led_2_rings_24) 134 //settings for 2 24 led rings 135 136 int stripNumOfLeds = 48; 137 uint32_t stripColor[25]; 138 int displayMiddleLed = false; 139 int splitStrip = true; 140 int middleOffset = 0; 141 int startupAnimationDelay = 5; 142 int orangeLimitAmount = 0; 143 int swapLeftRight = false; 144 145 int dropDelay = 2; 146 float dropFactor = .96; 147 148 int peakTimeNoDropDelay = 250; 149 int peakTimeFirstDropDelay = 100; 150 int peakTimeDropDelay = 10; 151 float peakDropFactor = .94; 152 int droppingPeakFade = false; 153 154 int bouncingPeaksNumOfLeds = 3; 155 int bouncingPeakDelay = 4; 156 int bouncingPeakCounterInc = 9; 157 158#elif defined(led_2_rings_30) 159 //settings for 2 30 led rings 160 161 int stripNumOfLeds = 60; 162 uint32_t stripColor[31]; 163 int displayMiddleLed = false; 164 int splitStrip = true; 165 int middleOffset = 0; 166 int startupAnimationDelay = 5; 167 int orangeLimitAmount = 0; 168 int swapLeftRight = false; 169 170 int dropDelay = 2; 171 float dropFactor = .96; 172 173 int peakTimeNoDropDelay = 250; 174 int peakTimeFirstDropDelay = 100; 175 int peakTimeDropDelay = 10; 176 float peakDropFactor = .94; 177 int droppingPeakFade = false; 178 179 int bouncingPeaksNumOfLeds = 3; 180 int bouncingPeakDelay = 4; 181 int bouncingPeakCounterInc = 9; 182 183#elif defined (led_strip_200) 184 //settings for a 200 led strip 185 186 int stripNumOfLeds = 200; 187 uint32_t stripColor[101]; 188 int displayMiddleLed = false; 189 int splitStrip = true; 190 int middleOffset = 0; 191 int startupAnimationDelay = 1; 192 int orangeLimitAmount = 0; 193 int swapLeftRight = false; 194 195 int dropDelay = 10; 196 float dropFactor = .96; 197 198 int peakTimeNoDropDelay = 250; 199 int peakTimeFirstDropDelay = 100; 200 int peakTimeDropDelay = 30; 201 float peakDropFactor = .99; 202 int droppingPeakFade = false; 203 204 int bouncingPeaksNumOfLeds = 8; 205 int bouncingPeakDelay = 4; 206 int bouncingPeakCounterInc = 9; 207 208#elif defined (led_strip_144) 209 //settings for a 200 led strip 210 211 int stripNumOfLeds = 145; 212 uint32_t stripColor[73]; 213 int displayMiddleLed = true; 214 int splitStrip = false; 215 int middleOffset = 1; 216 int startupAnimationDelay = 1; 217 int orangeLimitAmount = 0; 218 int swapLeftRight = false; 219 220 int dropDelay = 10; 221 float dropFactor = .85; 222 223 int peakTimeNoDropDelay = 250; 224 int peakTimeFirstDropDelay = 100; 225 int peakTimeDropDelay = 5; 226 float peakDropFactor = .94; 227 int droppingPeakFade = false; 228 229 int bouncingPeaksNumOfLeds = 10; 230 int bouncingPeakDelay = 2; 231 int bouncingPeakCounterInc = 10; 232#endif 233 234// 235// setup other variables, user editable 236// 237 238// basic settings 239int minValue = 10; // min analog input value 240int maxValue = 350; // max analog input value (0-1023 equals 0-5V) 241int sensitivityValue = 128; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) 242int maxSensitivity = 2 * 255; // let the 'volume' go up to 200%! 243int ledBrightness = 30; // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true) 244int sensorDeviationBrightness = 1; // eliminate fluctuating values 245int overflowDelay = 20; // overflow hold time 246 247// peak settings 248int displayPeaks = true; // value will be set by the switch if useSensorValues = true 249int droppingPeak = true; // display dropping peaks or not. note: displayPeaks has to be true 250int bouncingPeaks = true; // display bouncing peaks or not. note: displayPeaks has to be true 251 252// 253// initialize other variables needed for the sketch 254// 255 256int numOfSegments = stripNumOfLeds / 2; 257int halfNumOfSegments = numOfSegments / 2; 258int stripMiddle = stripNumOfLeds / 2; 259int maxDisplaySegments = stripMiddle - 1; 260float sensitivityFactor; 261 262int brightnessValue, prevBrightnessValue; 263float ledFactor, ledFactor_div_numOfSegments; 264 265int leftValue = 0, rightValue = 0, maxReadValue = 0; 266int leftAnalogValue = 0, rightAnalogValue = 0; 267 268int prevLeftValue = 0, prevRightValue = 0; 269int prevLeftAnalogValue = 0, prevRightAnalogValue = 0; 270 271int i, j; 272int dropLeft, dropRight; 273int leftDropTime, rightDropTime; 274 275int leftPeak = 0, rightPeak = 0; 276int leftPeakTime = 0, rightPeakTime = 0; 277int leftFirstPeak = true, rightFirstPeak = true; 278int readShowPeaksPin, prevReadShowPeaksPin; 279 280uint32_t stripMiddleColor, stripOverflowColor, stripHoldColor; 281 282int leftPeakBouncing = false, rightPeakBouncing = false; 283int leftPeakBounce = 0, rightPeakBounce = 0; 284int prevLeftPeakBounce = 0, prevRightPeakBounce = 0; 285int leftPeakBounceCounter = 0, rightPeakBounceCounter = 0; 286int leftPeakBounceDelayCounter = 0, rightPeakBounceDelayCounter = 0; 287 288// 289// initialize the strip or rings 290// 291 292Adafruit_NeoPixel strip = Adafruit_NeoPixel(stripNumOfLeds, stripPin, NEO_GRB + NEO_KHZ800); 293 294// 295// setup 296// 297 298void setup() { 299 #ifdef DEBUG 300 Serial.begin(9600); 301 #endif 302 303 pinMode(showPeaksPin, INPUT); 304 305 strip.begin(); 306 307 setStripColors(); 308 startupAnimation(); 309 310 if (useSensorValues) 311 setInitialDisplayPeaks(); 312 else 313 setSensitivityFactor(); 314} 315 316// 317// main loop 318// 319 320void loop() { 321 if (useSensorValues) 322 readSensorValues(); 323 324 readValues(); 325 drawValues(); 326 327 if (displayPeaks) { 328 getPeaks(); 329 drawPeaks(); 330 } 331 332 storePrevValues(); 333} 334 335// 336// functions 337// 338 339void setInitialDisplayPeaks() { 340 readShowPeaksPin = digitalRead(showPeaksPin); 341 if (readShowPeaksPin == HIGH) 342 displayPeaks = false; 343 else 344 displayPeaks = true; 345 prevReadShowPeaksPin = readShowPeaksPin; 346} 347 348void readSensorValues() { 349 readShowPeaksPin = digitalRead(showPeaksPin); 350 351 if (momentarySwitch) { 352 if (readShowPeaksPin == LOW && prevReadShowPeaksPin == HIGH) { 353 if (displayPeaks == true) { 354 displayPeaks = false; 355 clearLeftPeak(); 356 clearRightPeak(); 357 if (momentarySwitch) 358 while (digitalRead(showPeaksPin) == LOW) {} 359 } 360 else { 361 displayPeaks = true; 362 } 363 } 364 } 365 else { 366 if (readShowPeaksPin == LOW && prevReadShowPeaksPin == HIGH) 367 displayPeaks = true; 368 else if (readShowPeaksPin == HIGH && prevReadShowPeaksPin == LOW) { 369 displayPeaks = false; 370 clearLeftPeak(); 371 clearRightPeak(); 372 } 373 } 374 prevReadShowPeaksPin = readShowPeaksPin; 375 376 brightnessValue = analogRead(brightnessPin); 377 brightnessValue = map(brightnessValue, 0, 1023, 0, 255); 378 379 if (abs(brightnessValue - prevBrightnessValue) > sensorDeviationBrightness) { 380 ledBrightness = brightnessValue; 381 setStripColors(); 382 prevBrightnessValue = brightnessValue; 383 } 384 385 sensitivityValue = analogRead(sensitivityPin); 386 sensitivityValue = map(sensitivityValue, 0, 1023, 0, 255); 387 setSensitivityFactor(); 388} 389 390void setSensitivityFactor() { 391 //sensitivityValue_div_numOfSegments = sensitivityValue / numOfSegments; 392 sensitivityFactor = ((float) sensitivityValue / 255 * (float) maxSensitivity / 255); 393} 394 395void readValues() { 396 leftAnalogValue = analogRead(leftPin); 397 rightAnalogValue = analogRead(rightPin); 398 399 if (swapLeftRight) { 400 int tempValue = leftAnalogValue; 401 leftAnalogValue = rightAnalogValue; 402 rightAnalogValue = tempValue; 403 } 404 405 if (leftAnalogValue < prevLeftAnalogValue) { 406 leftDropTime++; 407 if (leftDropTime > dropDelay) { 408 leftAnalogValue = prevLeftAnalogValue * dropFactor; 409 leftDropTime = 0; 410 } 411 else 412 leftAnalogValue = prevLeftAnalogValue; 413 } 414 415 if (rightAnalogValue < prevRightAnalogValue) { 416 rightDropTime++; 417 if (rightDropTime > dropDelay) { 418 rightAnalogValue = prevRightAnalogValue * dropFactor; 419 rightDropTime = 0; 420 } 421 else 422 rightAnalogValue = prevRightAnalogValue; 423 } 424 425 #ifdef DEBUG 426 Serial.print(leftAnalogValue); 427 Serial.print(" "); 428 Serial.println(rightAnalogValue); 429 #endif 430 431 // map values 432 leftValue = map(leftAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); 433 rightValue = map(rightAnalogValue * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); 434 435 if (leftValue > maxDisplaySegments) { 436 leftValue = maxDisplaySegments; 437 drawOverflow(); 438 } 439 440 if (rightValue > maxDisplaySegments) { 441 rightValue = maxDisplaySegments; 442 drawOverflow(); 443 } 444} 445 446void storePrevValues() { 447 prevLeftAnalogValue = leftAnalogValue; 448 prevRightAnalogValue = rightAnalogValue; 449 450 prevLeftValue = leftValue; 451 prevRightValue = rightValue; 452} 453 454void getPeaks() { 455 if (leftValue > leftPeak) { 456 leftPeak = leftValue; 457 leftPeakTime = 0; 458 leftFirstPeak = true; 459 460 if (bouncingPeaks) { 461 leftPeakBouncing = true; 462 leftPeakBounceCounter = 0; 463 leftPeakBounceDelayCounter = 0; 464 } 465 } 466 else { 467 leftPeakTime++; 468 if (droppingPeak) { 469 if (leftFirstPeak) { 470 if (leftPeakTime > peakTimeFirstDropDelay) { 471 clearLeftPeak(); 472 leftFirstPeak = false; 473 } 474 } 475 else { 476 if (leftPeakTime > peakTimeDropDelay) { 477 clearLeftPeak(); 478 } 479 } 480 } 481 else { 482 if (leftPeakTime > peakTimeNoDropDelay) { 483 clearLeftPeak(); 484 } 485 } 486 } 487 488 if (leftPeakBouncing) { 489 if (leftFirstPeak) { 490 leftPeakBounceDelayCounter++; 491 if (leftPeakBounceDelayCounter >= bouncingPeakDelay) { 492 leftPeakBounceDelayCounter = 0; 493 leftPeakBounceCounter += bouncingPeakCounterInc; 494 495 if (leftPeakBounceCounter >= 180) { 496 clearLeftBouncePeak(); 497 clearLeftBounce(); 498 } 499 else { 500 leftPeakBounce = min((sin(leftPeakBounceCounter * 0.0174532925) * bouncingPeaksNumOfLeds), (maxDisplaySegments - leftPeak)); 501 if (leftPeakBounce != prevLeftPeakBounce) { 502 clearLeftBouncePeak(); 503 } 504 prevLeftPeakBounce = leftPeakBounce; 505 } 506 } 507 } 508 } 509 510 if (rightValue > rightPeak) { 511 rightPeak = rightValue; 512 rightPeakTime = 0; 513 rightFirstPeak = true; 514 515 if (bouncingPeaks) { 516 rightPeakBouncing = true; 517 rightPeakBounceCounter = 0; 518 rightPeakBounceDelayCounter = 0; 519 } 520 } 521 else { 522 rightPeakTime++; 523 if (droppingPeak) { 524 if (rightFirstPeak) { 525 if (rightPeakTime > peakTimeFirstDropDelay) { 526 clearRightPeak(); 527 rightFirstPeak = false; 528 } 529 } 530 else { 531 if (rightPeakTime > peakTimeDropDelay) 532 clearRightPeak(); 533 } 534 } 535 else { 536 if (rightPeakTime > peakTimeNoDropDelay) 537 clearRightPeak(); 538 } 539 } 540 541 if (rightPeakBouncing) { 542 if (rightFirstPeak) { 543 rightPeakBounceDelayCounter++; 544 if (rightPeakBounceDelayCounter >= bouncingPeakDelay) { 545 rightPeakBounceDelayCounter = 0; 546 rightPeakBounceCounter += bouncingPeakCounterInc; 547 548 if (rightPeakBounceCounter >= 180) { 549 clearRightBouncePeak(); 550 clearRightBounce(); 551 } 552 else { 553 rightPeakBounce = min((sin(rightPeakBounceCounter * 0.0174532925) * bouncingPeaksNumOfLeds), (maxDisplaySegments - rightPeak)); 554 if (rightPeakBounce != prevRightPeakBounce) { 555 clearRightBouncePeak(); 556 } 557 prevRightPeakBounce = rightPeakBounce; 558 } 559 } 560 } 561 } 562} 563 564void drawValues() { 565 if (splitStrip) { 566 for (i = middleOffset; i < leftValue; i++) 567 strip.setPixelColor(i, stripColor[i]); 568 569 for (i = prevLeftValue; i > leftValue; i--) 570 strip.setPixelColor(i, 0); 571 572 for (i = middleOffset; i < rightValue; i++) 573 strip.setPixelColor(stripMiddle + i, stripColor[i]); 574 575 for (i = prevRightValue; i > rightValue; i--) 576 strip.setPixelColor(stripMiddle + i, 0); 577 } 578 else { 579 for (i = middleOffset; i < leftValue; i++) 580 strip.setPixelColor(stripMiddle + i, stripColor[i]); 581 582 for (i = prevLeftValue; i > leftValue; i--) 583 strip.setPixelColor(stripMiddle + i, 0); 584 585 for (i = middleOffset; i < rightValue; i++) 586 strip.setPixelColor(stripMiddle - i, stripColor[i]); 587 588 for (i = prevRightValue; i > rightValue; i--) 589 strip.setPixelColor(stripMiddle - i, 0); 590 } 591 592 if (displayMiddleLed) strip.setPixelColor(stripMiddle, stripMiddleColor); 593 strip.show(); 594} 595 596void drawPeaks() { 597 if (leftPeak > 0) { 598 if (droppingPeakFade && leftPeakBouncing == false) 599 stripHoldColor = strip.Color(max(1, (255 * leftPeak * ledFactor_div_numOfSegments)), 0, 0); 600 else 601 stripHoldColor = stripColor[numOfSegments]; 602 603 if (splitStrip) 604 strip.setPixelColor((leftPeak + leftPeakBounce), stripHoldColor); 605 else 606 strip.setPixelColor(stripMiddle + (leftPeak + leftPeakBounce), stripHoldColor); 607 } 608 609 if (rightPeak > 0) { 610 if (droppingPeakFade && rightPeakBouncing == false) 611 stripHoldColor = strip.Color(max(1, (255 * rightPeak * ledFactor_div_numOfSegments)), 0, 0); 612 else 613 stripHoldColor = stripColor[numOfSegments]; 614 615 if (splitStrip) 616 strip.setPixelColor(stripMiddle + rightPeak + prevRightPeakBounce, stripHoldColor); 617 else 618 strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), stripHoldColor); 619 } 620 621 if (leftPeak > 0 || rightPeak > 0) 622 strip.show(); 623} 624 625void clearLeftPeak() { 626 if (splitStrip) 627 strip.setPixelColor((leftPeak + prevLeftPeakBounce), 0); 628 else 629 strip.setPixelColor(stripMiddle + (leftPeak + prevLeftPeakBounce), 0); 630 631 if (droppingPeak) 632 leftPeak = leftPeak * peakDropFactor; 633 else 634 leftPeak = 0; 635 leftPeakTime = 0; 636} 637 638void clearLeftBounce() { 639 leftPeakBouncing = false; 640 leftPeakBounceCounter = 0; 641 leftPeakBounce = 0; 642 prevLeftPeakBounce = 0; 643} 644 645void clearLeftBouncePeak() { 646 if (splitStrip) 647 strip.setPixelColor((leftPeak + prevLeftPeakBounce), 0); 648 else 649 strip.setPixelColor(stripMiddle + (leftPeak + prevLeftPeakBounce), 0); 650} 651 652void clearRightPeak() { 653 if (splitStrip) 654 strip.setPixelColor(stripMiddle + rightPeak + prevRightPeakBounce, 0); 655 else 656 strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), 0); 657 658 if (droppingPeak) 659 rightPeak = rightPeak * peakDropFactor; 660 else 661 rightPeak = 0; 662 rightPeakTime = 0; 663} 664 665void clearRightBounce() { 666 rightPeakBouncing = false; 667 rightPeakBounceCounter = 0; 668 rightPeakBounce = 0; 669 prevRightPeakBounce = 0; 670} 671 672void clearRightBouncePeak() { 673 if (splitStrip) 674 strip.setPixelColor((stripMiddle + rightPeak + prevRightPeakBounce), 0); 675 else 676 strip.setPixelColor(stripMiddle - (rightPeak + prevRightPeakBounce), 0); 677} 678 679void drawOverflow() { 680 for (i = 0; i <= numOfSegments; i++) { 681 strip.setPixelColor(stripMiddle + i, stripOverflowColor); 682 strip.setPixelColor(stripMiddle - i, stripOverflowColor); 683 } 684 strip.show(); 685 delay(overflowDelay); 686 687 for (i = 0; i <= numOfSegments; i++) { 688 strip.setPixelColor(stripMiddle + i, 0); 689 strip.setPixelColor(stripMiddle - i, 0); 690 } 691 strip.show(); 692} 693 694void setStripColors() { 695 int orangeLimit; 696 ledFactor = (float)ledBrightness / 255; 697 698 float orangeFactor = orangeLimitAmount / halfNumOfSegments; 699 ledFactor_div_numOfSegments = ledFactor / numOfSegments; 700 701 stripOverflowColor = strip.Color(min(255, 255 * ledFactor * 1.5), 0, 0); 702 stripMiddleColor = strip.Color(0, 0, 255 * ledFactor); 703 704 stripColor[0] = strip.Color(0, 255 * ledFactor, 0); 705 for (i = 1; i <= numOfSegments; i++) { 706 if (i <= halfNumOfSegments) 707 orangeLimit = (i * orangeFactor); 708 else 709 orangeLimit = ((numOfSegments - i) * orangeFactor); 710 711 stripColor[i] = strip.Color((255 * i * ledFactor_div_numOfSegments), ((255 - orangeLimit) * (numOfSegments - i) * ledFactor_div_numOfSegments), 0); 712 } 713 714 stripHoldColor = stripColor[numOfSegments]; 715} 716 717void startupAnimation() { 718 for (j = 0; j < 2; j++) { 719 for (i = 0; i <= numOfSegments; i++) { 720 strip.setPixelColor(stripMiddle - i, stripColor[i]); 721 strip.setPixelColor(stripMiddle + i, stripColor[i]); 722 strip.show(); 723 delay(startupAnimationDelay); 724 } 725 726 for (i = 0; i <= numOfSegments; i++) { 727 strip.setPixelColor(stripMiddle + i, 0); 728 strip.setPixelColor(stripMiddle - i, 0); 729 strip.show(); 730 delay(startupAnimationDelay); 731 } 732 } 733} 734 735
Old LCD, LED and LED strip test version.
arduino
1// include the library code: 2#include <LiquidCrystal.h> 3#include "LedControlMS.h" 4#include <Adafruit_NeoPixel.h> 5 6/* 7 LiquidCrystal Library 8 9 The circuit: 10 * LCD RS pin to digital pin 12 11 * LCD Enable pin to digital pin 11 12 * LCD D4 pin to digital pin 5 13 * LCD D5 pin to digital pin 4 14 * LCD D6 pin to digital pin 3 15 * LCD D7 pin to digital pin 2 16 * LCD R/W pin to ground 17 * LCD VSS pin to ground 18 * LCD VCC pin to 5V 19 * 10K resistor: 20 * ends to +5V and ground 21 * wiper to LCD VO pin (pin 3) 22 23*/ 24// initialize the library by associating any needed LCD interface pin 25// with the arduino pin number it is connected to 26const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; 27LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 28 29// led matrix 30// DataIN, CLK, LOAD, # of matrix 31#define NUMOFMATRIX 4 32LedControl led = LedControl(10,8,9,NUMOFMATRIX); 33 34// led strip 35#define PIN 6 36#define NUMOFLEDS 31 37Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMOFLEDS, PIN, NEO_GRB + NEO_KHZ800); 38 39// setup values 40const int outputType = 1; // lcd = 0, led = 1, led strip = 2 41 42const int minValue = 20; 43const int maxValue = 500; 44 45int updateDelay, maxSegments; 46float ledIntensity; 47 48int leftPin = A0, rightPin = A1; 49int leftValue = 0, rightValue = 0, averageValue = 0, maxReadValue = 0; 50int leftAnalogValue = 0, rightAnalogValue = 0, averageAnalogValue = 0; 51 52int prevLeftValue = 0, prevRightValue = 0, prevAverageValue = 0; 53int prevLeftAnalogValue = 0, prevRightAnalogValue = 0, prevAverageAnalogValue = 0; 54 55float dropFactor; 56 57int i, j; 58 59int holdShow = true; 60int holdZeroShow = true; 61int leftHold = 0, rightHold = 0; 62int leftHoldTime = 0, rightHoldTime = 0; 63int holdTime; 64 65int stripMiddle; 66uint32_t stripRed, stripGreen, stripBlue; 67uint32_t stripColor[16]; 68 69void setup() { 70 switch (outputType) { 71 case 0: 72 // set up the LCD's number of columns and rows: 73 updateDelay = 50; 74 maxSegments = 16; 75 holdTime = 20; 76 dropFactor = .9; 77 78 lcd.begin(16, 2); 79 break; 80 81 case 1: 82 // set up led 83 updateDelay = 0; 84 maxSegments = 7; 85 holdTime = 50; 86 ledIntensity = 1.2; 87 dropFactor = .7; 88 89 for (i = 0; i < NUMOFMATRIX; i++) { 90 led.shutdown(i, false); 91 //led.setIntensity(i, 4); 92 led.clearDisplay(i); 93 } 94 95 // setup: identify left & right matrix 96 led.writeString(0,"#1 - LEFT"); 97 led.writeString(1,"#2 - RIGHT"); 98 led.writeString(2,"#3 - THREE"); 99 led.writeString(3,"#4 - FOUR"); 100 break; 101 102 case 2: 103 // set up led strip 104 updateDelay = 0; 105 maxSegments = NUMOFLEDS / 2; 106 stripMiddle = maxSegments; 107 holdTime = 250; 108 ledIntensity = .1; 109 dropFactor = .991; 110 111 strip.begin(); 112 113 stripRed = strip.Color(255 * ledIntensity, 0, 0); 114 stripGreen = strip.Color(0, 255 * ledIntensity, 0); 115 stripBlue = strip.Color(0, 0, 255 * ledIntensity); 116 117 // set colors green to fellow to red 118 for (i = 0; i <= maxSegments; i++) 119 stripColor[i] = strip.Color(255 / maxSegments * i * ledIntensity , 255 / maxSegments * (maxSegments - i) * ledIntensity, 0); 120 121 // startup animation 122 for (j = 0; j < 2; j ++) { 123 for (i = 0; i <= maxSegments; i++) { 124 strip.setPixelColor(stripMiddle - i, stripColor[i]); 125 strip.setPixelColor(stripMiddle + i, stripColor[i]); 126 strip.show(); 127 delay(20); 128 } 129 130 for (i = 0; i <= maxSegments; i++) { 131 strip.setPixelColor(stripMiddle + i, 0); 132 strip.setPixelColor(stripMiddle - i, 0); 133 strip.show(); 134 delay(20); 135 } 136 } 137 } 138 139 // for debugging 140 Serial.begin(9600); 141} 142 143void loop() { 144 readValues(); 145 if (holdShow) getHolds(); 146 147 switch(outputType) { 148 case 0: 149 LCDdisplayValues(); 150 if (holdShow) LCDdisplayHolds(); 151 break; 152 153 case 1: 154 LEDdisplayValues(); 155 if (holdShow) LEDdisplayHolds(); 156 break; 157 158 case 2: 159 STRIPdisplayValues(); 160 if (holdShow) STRIPdisplayHolds(); 161 } 162 163 storePrevValues(); 164 delay(updateDelay); 165} 166 167void readValues() { 168 leftAnalogValue = analogRead(leftPin); 169 rightAnalogValue = analogRead(rightPin); 170 //averageAnalogValue = (leftValue + rightValue) / 2; 171 172 if (leftAnalogValue < prevLeftAnalogValue) 173 leftAnalogValue = prevLeftAnalogValue * dropFactor; 174 175 if (rightAnalogValue < prevRightAnalogValue) 176 rightAnalogValue = prevRightAnalogValue * dropFactor; 177 178 //if (averageAnalogValue < prevAverageAnalogValue) 179 // averageAnalogValue = averageAnalogValue * dropFactor; 180 181 // debug to check read values 182 /* 183 if (leftValue > maxReadValue) maxReadValue = leftValue; 184 if (rightValue > maxReadValue) maxReadValue = rightValue; 185 Serial.print(maxReadValue); 186 Serial.print(" "); 187 Serial.print(leftValue); 188 Serial.print(" "); 189 Serial.println(rightValue); 190 */ 191 192 // check if left or right value exceeds max and print the value 193 if (leftValue > maxValue) Serial.println (leftValue); 194 if (rightValue > maxValue) Serial.println (rightValue); 195 196 // map values 197 leftValue = map(leftAnalogValue, minValue, maxValue, 0, maxSegments); 198 rightValue = map(rightAnalogValue, minValue, maxValue, 0, maxSegments); 199 //averageValue = map(averageAnalogValue, minValue, maxValue, 0, maxSegments); 200 201} 202 203void storePrevValues() { 204 prevLeftAnalogValue = leftAnalogValue; 205 prevRightAnalogValue = rightAnalogValue; 206 //prevAverageValue = averageAnalogValue; 207 208 prevLeftValue = leftValue; 209 prevRightValue = rightValue; 210 //prevAverageValue = averageValue; 211} 212 213void LEDdisplayValues() { 214 led.clearDisplay(0); 215 led.clearDisplay(1); 216 217 for (i=0; i <= leftValue; i++) 218 for (j=0; j <= leftValue; j++) 219 led.setLed(0, maxSegments - i, j, true); 220 led.setIntensity(0, leftValue * ledIntensity); 221 222 for (i=0; i <= rightValue; i++) 223 for (j=0; j <= rightValue; j++) 224 led.setLed(1, i, j, true); 225 led.setIntensity(1, rightValue * ledIntensity); 226} 227 228void LEDdisplayHolds() { 229 if (leftHold || holdZeroShow) { 230 //for (j=0; j <= leftHold; j++) 231 // led.setLed(0, leftHold, j, true); 232 233 //for (j=0; j <= leftHold; j++) 234 // led.setLed(0, j, leftHold, true); 235 led.setLed(0, maxSegments - leftHold, leftHold, true); 236 } 237 238 if (rightHold || holdZeroShow) { 239 //for (j=0; j < rightHold; j++) 240 // led.setLed(1, rightHold, j, true); 241 242 //for (j=0; j <= rightHold; j++) 243 // led.setLed(1, j, rightHold, true); 244 led.setLed(1, rightHold, rightHold, true); 245 } 246} 247 248void LCDdisplayValues() { 249 lcd.clear(); 250 for (i=0; i < leftValue; i++) 251 lcd.print("="); 252 253 lcd.setCursor(0, 1); 254 for (i=0; i < rightValue; i++) 255 lcd.print("="); 256} 257 258void LCDdisplayHolds() { 259 if (leftHold || holdZeroShow) { 260 lcd.setCursor(leftHold, 0); 261 lcd.print(">"); 262 } 263 264 if (rightHold || holdZeroShow) { 265 lcd.setCursor(rightHold, 1); 266 lcd.print(">"); 267 } 268} 269 270void STRIPdisplayValues() { 271 272 strip.clear(); 273 //for (i=0; i <= averageValue; i++) 274 // strip.setPixelColor(i, stripGreen); 275 //strip.show(); 276 277 for (i=0; i <= leftValue; i++) 278 strip.setPixelColor(stripMiddle + i, stripColor[i]); 279 280 for (i=0; i <= rightValue; i++) 281 strip.setPixelColor(stripMiddle - i, stripColor[i]); 282 283/* 284 for (i=prevLeftValue; i <= leftValue; i++) 285 strip.setPixelColor(stripMiddle + i, stripGreen); 286 287 for (i=prevLeftValue; i > leftValue; i--) 288 strip.setPixelColor(stripMiddle + i, 0); 289 290 for (i=prevRightValue; i <= rightValue; i++) 291 strip.setPixelColor(stripMiddle - i, stripGreen); 292 293 for (i=prevRightValue; i > rightValue; i--) 294 strip.setPixelColor(stripMiddle - i, 0); 295*/ 296 297 strip.show(); 298} 299 300void STRIPdisplayHolds() { 301 //strip.setPixelColor((leftHold + rightHold) / 2, stripRed); 302 303 strip.setPixelColor(stripMiddle + leftHold, stripColor[maxSegments]); 304 strip.setPixelColor(stripMiddle - rightHold, stripColor[maxSegments]); 305 strip.setPixelColor(stripMiddle, stripBlue); 306 strip.show(); 307} 308 309void getHolds() { 310 if (leftValue > leftHold) { 311 leftHold = leftValue; 312 leftHoldTime = 0; 313 } 314 else { 315 leftHoldTime++; 316 if (leftHoldTime > holdTime) { 317 //if (outputType == 2) 318 // strip.setPixelColor(stripMiddle + leftHold, 0); 319 leftHold = 0; 320 leftHoldTime = 0; 321 } 322 } 323 324 if (rightValue > rightHold) { 325 rightHold = rightValue; 326 rightHoldTime = 0; 327 } 328 else { 329 rightHoldTime++; 330 if (rightHoldTime > holdTime) { 331 //if (outputType == 2) 332 // strip.setPixelColor(stripMiddle - rightHold, 0); 333 rightHold = 0; 334 rightHoldTime = 0; 335 } 336 } 337} 338
20180202 VU meter update
arduino
Updated code with a lot of changes. Note: use the updated diagram, there are some changes in pin connections - multiple color schemes - option to connect 2 strip or rings on 2 output pins of the Arduino (the previous code used 1) - option for pulsing and spinning - option for logarithmic audio response
1/* 2********************************************************************** 3* 4 Stereo VU Meter for 1 or 2 LED rings or strips build by ericBcreator 5* Designed 6 to be used with an Arduino UNO, Nano or compatible device. 7********************************************************************** 8* 9 Notice: search for @EB in the Sketch for important variables to set 10* for the 11 Sketch to work with your setup. 12********************************************************************** 13* 14 Last updated 20180202 by ericBcreator 15* 16* This code is free for personal use, 17 not for commercial purposes. 18* Please leave this header intact. 19* 20* contact: 21 ericBcreator@gmail.com 22********************************************************************** 23*/ 24 25// 26// 27 include the NeoPixel library: 28// 29 30#include <Adafruit_NeoPixel.h> 31 32// 33// 34 debugging settings 35// 36//#define DEBUG // 37 debug: enable serial.print 38//#define DEBUG_NO_PEAK_SWITCH // 39 debug: no peak switch connected @EB 40//#define DEBUG_TEST_LEDS // 41 debug: display each led (color) slowly at startup 42//#define DEBUG_PRINT_LOOP_TIME 43 // debug: serial.print the looptime in ms 44//#define 45 DEBUG_PRINT_ANALOGVALUES // debug: serial.print analog 46 input values 47//#define DEBUG_NO_PEAKS // debug: 48 display no peaks, ignoring other settings 49//#define DEBUG_PEAKS // 50 debug: display peaks, ignoring other settings 51 52// 53// uncomment to average 54 the input levels to the number defined by averageNumOfReadings. increasing the value 55 will make the script less responsive 56// 57//#define averageReadings // 58 average input levels or not 59//#define averageNumOfReadings 3 // 60 num of readings for averaging 61 62// 63// uncomment to map the linear audio 64 input to non-linear response 65// 66//#define nonLinearSinAudio // 67 uncomment to map the linear audio input signal to a non-linear, reverse audio-taper 68 response (sin wave) 69//#define nonLinearReverseSinAudio // 70 uncomment to map the linear audio input signal to a non-linear, audio-taper response 71 (reverse sin wave) 72#define nonLinearLogAudio // 73 uncomment to map the linear audio input signal to a log response 74//#define nonLinearAvr2 75 // uncomment to average the original input 76 with the non-linear response 77 78// 79// overflow settings 80// 81//#define 82 displayOverflow // display overflow or not 83//#define 84 compressOverflowPeaks // compress overflow peaks or 85 not 86//#define compressOverflowNumOfTimes 2 // num of times 87 to apply the compressOverflowFactor 88//float compressOverflowFactor = .05; // 89 factor for compression 90 91// 92// uncomment when using high level (non-consumer) 93 inputs 94// 95//#define highLevelInput // 96 @EB define for high level inputs 97 98// 99// uncomment the definition for the 100 connected strip or ring(s) @EB 101// 102 103//#define led_matrix_40 104//#define 105 led_ring_60 106//#define led_ring_60_ps 107#define led_rhombus_160_ps 108//#define 109 led_strip_60 110//#define led_strip_60_qr 111//#define led_strip_30 112//#define 113 led_2_rings_24 114//#define led_2_rings_30 115//#define led_strip_200 116//#define 117 led_strip_144 118//#define led_2_strip_63 119//#define led_2_strip_63_qr 120 121// 122// 123 important setting: using potentiometer sensor values or not 124// This setting has 125 to be set right or the script will not work correctly: 126// - set to true if using 127 potentiometers 128// - set to false if not using potentiometers 129// 130 131const 132 int useSensorValues = true; // @EB 133 134// 135// setup 136 pins 137// 138 139int leftPin = A0, rightPin = A1; // left 140 audio in on analog 0, right on analog 1 141int brightnessPin = A4, sensitivityPin 142 = A5; // potentiometers for brightness and sensitivity on analog 4 143 and 5 144int leftStripPin = 5; // DIN of left 145 led strip on digital pin 5 146int rightStripPin = 6; // 147 DIN of right led strip on digital pin 6 148int showPeaksPin = 7; // 149 switch to toggle peaks on or off on digital pin 7 (7, 9 for box version) 150int 151 showPeaksMomentarySwitch = false; // set false for an on/off 152 toggle switch 153int reverseShowPeaks = true; // reverses 154 the on/off setting in case you made a wiring mistake ;-) @EB 155int selectButton1Pin 156 = 8; // push button for changing settings on digital 157 pin 8 158int useSelectButton1 = true; // set to false 159 if no push button1 for selecting the color scheme is connected @EB 160int selectButton2Pin 161 = 9; // push button for changing settings on digital 162 pin 9 163int useSelectButton2 = true; // set to false 164 if no push button2 is connected @EB 165 166// 167// setup variables for the number 168 of leds and led strip or 2 rings 169// 170 171#if defined (led_matrix_40) 172 //settings 173 for a 40 led matrix 174 175 int stripNumOfLeds = 40; // 176 the total number of leds 177 int stripsOn2Pins = false; // 178 set to true if the LED strips or rings are connected to 2 input pins 179 uint32_t 180 stripColor[21]; // half of the number of leds + 181 1 182 int displayMiddleLed = false; // display the 183 middle led (blue). set to true for one strip, false for two strips or rings 184 185 int splitStrip = true; // set to true when 186 using 2 strips or rings, false for one strip 187 int middleOffset = 0; // 188 offset for the middle led when using one strip 189 int startupAnimationDelay = 190 6; // delay for the startup animation 191 int orangeLimitAmount 192 = 0; // limit the amount of green of the middle LEDs 193 to make them more orange 194 int swapLeftRight = false; // 195 swap the left and right input values or not 196 197 int dropDelay = 5; // 198 hold time before dropping the leds 199 float dropFactor = .94; // 200 value for dropping the leds 201 202 int peakTimeNoDropDelay = 250; // 203 peak hold time when not dropping the peaks (when droppingPeak is false) 204 int 205 peakTimeFirstDropDelay = 150; // peak hold time when dropping 206 the first peak 207 int peakTimeDropDelay = 7; // 208 peak hold time when dropping the rest of the peaks 209 float peakDropFactor = .94; 210 // value for dropping the peaks 211 int droppingPeakFade 212 = false; // display the dropping peak fading to black 213 or not 214 215 int bouncingPeaksNumOfLeds = 6; // how 216 many leds to bounce up (max) 217 int bouncingPeaksNumOfLedsMin = 3; // 218 how many leds to bounce up (min) when using dynamicBouncingPeaks 219 int bouncingPeakDelay 220 = 6; // delay between peak bounce updates 221 int 222 bouncingPeakCounterInc = 10; // increase counter for each 223 bounce update. note: it uses a 0-180 sin function for the bouncing 224 225#elif 226 defined (led_ring_60) 227 //settings for a 60 led ring 228 229 int stripNumOfLeds 230 = 60; // the total number of leds 231 int stripsOn2Pins 232 = false; // set to true if the LED strips or rings 233 are connected to 2 input pins 234 uint32_t stripColor[31]; // 235 half of the number of leds + 1 236 int displayMiddleLed = false; // 237 display the middle led (blue). set to true for one strip, false for two strips or 238 rings 239 int splitStrip = true; // set to true 240 when using 2 strips or rings, false for one strip 241 int middleOffset = 0; // 242 offset for the middle led when using one strip 243 int startupAnimationDelay = 244 6; // delay for the startup animation 245 int orangeLimitAmount 246 = 0; // limit the amount of green of the middle LEDs 247 to make them more orange 248 int swapLeftRight = false; // 249 swap the left and right input values or not 250 251 int dropDelay = 5; // 252 hold time before dropping the leds 253 float dropFactor = .94; // 254 value for dropping the leds 255 256 int peakTimeNoDropDelay = 250; // 257 peak hold time when not dropping the peaks (when droppingPeak is false) 258 int 259 peakTimeFirstDropDelay = 70; // peak hold time when dropping 260 the first peak 261 int peakTimeDropDelay = 7; // 262 peak hold time when dropping the rest of the peaks 263 float peakDropFactor = .94; 264 // value for dropping the peaks 265 int droppingPeakFade 266 = false; // display the dropping peak fading to black 267 or not 268 269 int bouncingPeaksNumOfLeds = 6; // how 270 many leds to bounce up (max) 271 int bouncingPeaksNumOfLedsMin = 3; // 272 how many leds to bounce up (min) when using dynamicBouncingPeaks 273 int bouncingPeakDelay 274 = 4; // delay between peak bounce updates 275 int 276 bouncingPeakCounterInc = 10; // increase counter for each 277 bounce update. note: it uses a 0-180 sin function for the bouncing 278 279#elif 280 defined (led_ring_60_ps) 281 //settings for a 60 led ring - pulsating and spinning 282 settings 283 284 int stripNumOfLeds = 60; // the 285 total number of leds 286 int stripsOn2Pins = false; // 287 set to true if the LED strips or rings are connected to 2 input pins 288 uint32_t 289 stripColor[31]; // half of the number of leds + 290 1 291 int displayMiddleLed = true; // display the 292 middle led (blue). set to true for one strip, false for two strips or rings 293 294 int splitStrip = true; // set to true when 295 using 2 strips or rings, false for one strip 296 int middleOffset = 0; // 297 offset for the middle led when using one strip 298 int startupAnimationDelay = 299 6; // delay for the startup animation 300 int orangeLimitAmount 301 = 0; // limit the amount of green of the middle LEDs 302 to make them more orange 303 int swapLeftRight = false; // 304 swap the left and right input values or not 305 306 int dropDelay = 5; // 307 hold time before dropping the leds 308 float dropFactor = .93; // 309 value for dropping the leds 310 311 int peakTimeNoDropDelay = 10; // 312 peak hold time when not dropping the peaks (when droppingPeak is false) 313 int 314 peakTimeFirstDropDelay = 10; // peak hold time when dropping 315 the first peak 316 int peakTimeDropDelay = 6; // 317 peak hold time when dropping the rest of the peaks 318 float peakDropFactor = .92; 319 // value for dropping the peaks 320 int droppingPeakFade 321 = false; // display the dropping peak fading to black 322 or not 323 324 int bouncingPeaksNumOfLeds = 6; // how 325 many leds to bounce up (max) 326 int bouncingPeaksNumOfLedsMin = 3; // 327 how many leds to bounce up (min) when using dynamicBouncingPeaks 328 int bouncingPeakDelay 329 = 4; // delay between peak bounce updates 330 int 331 bouncingPeakCounterInc = 10; // increase counter for each 332 bounce update. note: it uses a 0-180 sin function for the bouncing 333 334#elif 335 defined (led_rhombus_160_ps) 336 //settings for a 160 led rhombus - pulsating and 337 spinning settings 338 339 int stripNumOfLeds = 160; // 340 the total number of leds 341 int stripsOn2Pins = false; // 342 set to true if the LED strips or rings are connected to 2 input pins 343 uint32_t 344 stripColor[81]; // half of the number of leds + 345 1 346 int displayMiddleLed = true; // display the 347 middle led (blue). set to true for one strip, false for two strips or rings 348 349 int splitStrip = true; // set to true when 350 using 2 strips or rings, false for one strip 351 int middleOffset = 0; // 352 offset for the middle led when using one strip 353 int startupAnimationDelay = 354 0; // delay for the startup animation 355 int orangeLimitAmount 356 = 0; // limit the amount of green of the middle LEDs 357 to make them more orange 358 int swapLeftRight = false; // 359 swap the left and right input values or not 360 361 int dropDelay = 4; // 362 hold time before dropping the leds 363 float dropFactor = .92; // 364 value for dropping the leds 365 366 int peakTimeNoDropDelay = 150; // 367 peak hold time when not dropping the peaks (when droppingPeak is false) 368 int 369 peakTimeFirstDropDelay = 70; // peak hold time when dropping 370 the first peak 371 int peakTimeDropDelay = 7; // 372 peak hold time when dropping the rest of the peaks 373 float peakDropFactor = .94; 374 // value for dropping the peaks 375 int droppingPeakFade 376 = false; // display the dropping peak fading to black 377 or not 378 379 int bouncingPeaksNumOfLeds = 10; // how 380 many leds to bounce up (max) 381 int bouncingPeaksNumOfLedsMin = 5; // 382 how many leds to bounce up (min) when using dynamicBouncingPeaks 383 int bouncingPeakDelay 384 = 4; // delay between peak bounce updates 385 int 386 bouncingPeakCounterInc = 6; // increase counter for each 387 bounce update. note: it uses a 0-180 sin function for the bouncing 388 389#elif 390 defined (led_strip_60) 391 //settings for a 60 led strip 392 393 int stripNumOfLeds 394 = 60; // the total number of leds 395 int stripsOn2Pins 396 = false; // set to true if the LED strips or rings 397 are connected to 2 input pins 398 uint32_t stripColor[31]; // 399 half of the number of leds + 1 400 int displayMiddleLed = true; // 401 display the middle led (blue). set to true for one strip, false for two strips or 402 rings 403 int splitStrip = false; // set to true 404 when using 2 strips or rings, false for one strip 405 int middleOffset = 1; // 406 offset for the middle led when using one strip 407 int startupAnimationDelay = 408 6; // delay for the startup animation 409 int orangeLimitAmount 410 = 0; // limit the amount of green of the middle LEDs 411 to make them more orange 412 int swapLeftRight = false; // 413 swap the left and right input values or not 414 415 int dropDelay = 5; // 416 hold time before dropping the leds 417 float dropFactor = .94; // 418 value for dropping the leds 419 420 int peakTimeNoDropDelay = 250; // 421 peak hold time when not dropping the peaks (when droppingPeak is false) 422 int 423 peakTimeFirstDropDelay = 70; // peak hold time when dropping 424 the first peak 425 int peakTimeDropDelay = 7; // 426 peak hold time when dropping the rest of the peaks 427 float peakDropFactor = .94; 428 // value for dropping the peaks 429 int droppingPeakFade 430 = false; // display the dropping peak fading to black 431 or not 432 433 int bouncingPeaksNumOfLeds = 6; // how 434 many leds to bounce up (max) 435 int bouncingPeaksNumOfLedsMin = 3; // 436 how many leds to bounce up (min) when using dynamicBouncingPeaks 437 int bouncingPeakDelay 438 = 4; // delay between peak bounce updates 439 int 440 bouncingPeakCounterInc = 10; // increase counter for each 441 bounce update. note: it uses a 0-180 sin function for the bouncing 442 443#elif 444 defined (led_strip_60_qr) 445 //settings for a 60 led strip - quick response 446 447 448 int stripNumOfLeds = 60; // the total number 449 of leds 450 int stripsOn2Pins = false; // set to 451 true if the LED strips or rings are connected to 2 input pins 452 uint32_t stripColor[31]; 453 // half of the number of leds + 1 454 int displayMiddleLed 455 = true; // display the middle led (blue). set to true 456 for one strip, false for two strips or rings 457 int splitStrip = false; // 458 set to true when using 2 strips or rings, false for one strip 459 int middleOffset 460 = 1; // offset for the middle led when using 461 one strip 462 int startupAnimationDelay = 6; // delay 463 for the startup animation 464 int orangeLimitAmount = 0; // 465 limit the amount of green of the middle LEDs to make them more orange 466 int swapLeftRight 467 = false; // swap the left and right input values 468 or not 469 470 int dropDelay = 4; // hold 471 time before dropping the leds 472 float dropFactor = .92; // 473 value for dropping the leds 474 475 int peakTimeNoDropDelay = 0; // 476 peak hold time when not dropping the peaks (when droppingPeak is false) 477 int 478 peakTimeFirstDropDelay = 0; // peak hold time when dropping 479 the first peak 480 int peakTimeDropDelay = 0; // 481 peak hold time when dropping the rest of the peaks 482 float peakDropFactor = 1; 483 // value for dropping the peaks 484 int droppingPeakFade 485 = false; // display the dropping peak fading to black 486 or not 487 488 int bouncingPeaksNumOfLeds = 0; // how 489 many leds to bounce up (max) 490 int bouncingPeaksNumOfLedsMin = 0; // 491 how many leds to bounce up (min) when using dynamicBouncingPeaks 492 int bouncingPeakDelay 493 = 0; // delay between peak bounce updates 494 int 495 bouncingPeakCounterInc = 180; // increase counter for each 496 bounce update. note: it uses a 0-180 sin function for the bouncing 497 498#elif 499 defined (led_strip_30) 500 //settings for a 30 led strip 501 502 int stripNumOfLeds 503 = 30; // the total number of leds 504 int stripsOn2Pins 505 = false; // set to true if the LED strips or rings 506 are connected to 2 input pins 507 uint32_t stripColor[16]; // 508 half of the number of leds + 1 509 int displayMiddleLed = true; // 510 display the middle led (blue). set to true for one strip, false for two strips or 511 rings 512 int splitStrip = false; // set to true 513 when using 2 strips or rings, false for one strip 514 int middleOffset = 1; // 515 offset for the middle led when using one strip 516 int startupAnimationDelay = 517 10; // delay for the startup animation 518 int orangeLimitAmount 519 = 0; // limit the amount of green of the middle LEDs 520 to make them more orange 521 int swapLeftRight = false; // 522 swap the left and right input values or not 523 524 int dropDelay = 10; // 525 hold time before dropping the leds 526 float dropFactor = .9; // 527 value for dropping the leds 528 529 int peakTimeNoDropDelay = 250; // 530 peak hold time when not dropping the peaks (set droppingPeak true or false) 531 532 int peakTimeFirstDropDelay = 150; // peak hold time when 533 dropping the first peak 534 int peakTimeDropDelay = 15; // 535 peak hold time when dropping the rest of the peaks 536 float peakDropFactor = .94; 537 // value for dropping the peaks 538 int droppingPeakFade 539 = false; // display the dropping peak fading to black 540 or not 541 542 int bouncingPeaksNumOfLeds = 4; // how 543 many leds to bounce up (max) 544 int bouncingPeaksNumOfLedsMin = 2; // 545 how many leds to bounce up (min) when using dynamicBouncingPeaks 546 int bouncingPeakDelay 547 = 4; // delay between peak bounce updates 548 int 549 bouncingPeakCounterInc = 9; // increase counter for each 550 bounce update. note: it uses a 0-180 sin function for the bouncing 551 552#elif 553 defined (led_2_rings_24) 554 //settings for 2 24 led rings 555 556 int stripNumOfLeds 557 = 48; 558 int stripsOn2Pins = false; 559 uint32_t stripColor[25]; 560 int displayMiddleLed 561 = false; 562 int splitStrip = true; 563 int middleOffset = 0; 564 int startupAnimationDelay 565 = 5; 566 int orangeLimitAmount = 0; 567 int swapLeftRight = false; 568 569 int 570 dropDelay = 2; 571 float dropFactor = .96; 572 573 int peakTimeNoDropDelay = 574 250; 575 int peakTimeFirstDropDelay = 100; 576 int peakTimeDropDelay = 10; 577 578 float peakDropFactor = .94; 579 int droppingPeakFade = false; 580 581 int 582 bouncingPeaksNumOfLeds = 4; 583 int bouncingPeaksNumOfLedsMin = 2; 584 int bouncingPeakDelay 585 = 4; 586 int bouncingPeakCounterInc = 9; 587 588#elif defined(led_2_rings_30) 589 590 //settings for 2 30 led rings 591 592 int stripNumOfLeds = 60; 593 int stripsOn2Pins 594 = false; 595 uint32_t stripColor[31]; 596 int displayMiddleLed = false; 597 int 598 splitStrip = true; 599 int middleOffset = 0; 600 int startupAnimationDelay = 5; 601 602 int orangeLimitAmount = 0; 603 int swapLeftRight = false; 604 605 int dropDelay 606 = 2; 607 float dropFactor = .96; 608 609 int peakTimeNoDropDelay = 250; 610 611 int peakTimeFirstDropDelay = 100; 612 int peakTimeDropDelay = 10; 613 float 614 peakDropFactor = .94; 615 int droppingPeakFade = false; 616 617 int bouncingPeaksNumOfLeds 618 = 4; 619 int bouncingPeaksNumOfLedsMin = 2; 620 int bouncingPeakDelay = 4; 621 622 int bouncingPeakCounterInc = 9; 623 624#elif defined (led_strip_200) 625 //settings 626 for a 200 led strip 627 628 int stripNumOfLeds = 200; 629 int stripsOn2Pins = 630 false; 631 uint32_t stripColor[101]; 632 int displayMiddleLed = false; 633 int 634 splitStrip = true; 635 int middleOffset = 0; 636 int startupAnimationDelay = 1; 637 638 int orangeLimitAmount = 0; 639 int swapLeftRight = false; 640 641 int dropDelay 642 = 10; 643 float dropFactor = .96; 644 645 int peakTimeNoDropDelay = 250; 646 647 int peakTimeFirstDropDelay = 100; 648 int peakTimeDropDelay = 30; 649 float 650 peakDropFactor = .99; 651 int droppingPeakFade = false; 652 653 int bouncingPeaksNumOfLeds 654 = 8; 655 int bouncingPeaksNumOfLedsMin = 4; 656 int bouncingPeakDelay = 4; 657 658 int bouncingPeakCounterInc = 9; 659 660#elif defined (led_strip_144) 661 //settings 662 for a 144 led strip 663 664 int stripNumOfLeds = 145; 665 int stripsOn2Pins = 666 false; 667 uint32_t stripColor[73]; 668 int displayMiddleLed = true; 669 int 670 splitStrip = false; 671 int middleOffset = 1; 672 int startupAnimationDelay = 673 1; 674 int orangeLimitAmount = 0; 675 int swapLeftRight = false; 676 677 int 678 dropDelay = 4; 679 float dropFactor = .92; 680 681 int peakTimeNoDropDelay = 682 250; 683 int peakTimeFirstDropDelay = 100; 684 int peakTimeDropDelay = 5; 685 686 float peakDropFactor = .94; 687 int droppingPeakFade = false; 688 689 int 690 bouncingPeaksNumOfLeds = 10; 691 int bouncingPeaksNumOfLedsMin = 4; 692 int bouncingPeakDelay 693 = 2; 694 int bouncingPeakCounterInc = 10; 695 696#elif defined (led_2_strip_63) 697 698 //settings for 2 63 led strips 699 700 int stripNumOfLeds = 63; 701 int stripsOn2Pins 702 = true; 703 uint32_t stripColor[64]; 704 int displayMiddleLed = true; 705 int 706 splitStrip = true; 707 int middleOffset = 0; 708 int startupAnimationDelay = 1; 709 710 int orangeLimitAmount = 0; 711 int swapLeftRight = false; 712 713 int dropDelay 714 = 5; 715 float dropFactor = .94; 716 717 int peakTimeNoDropDelay = 250; 718 719 int peakTimeFirstDropDelay = 70; 720 int peakTimeDropDelay = 7; 721 float peakDropFactor 722 = .94; 723 int droppingPeakFade = false; 724 725 int bouncingPeaksNumOfLeds 726 = 12; 727 int bouncingPeaksNumOfLedsMin = 4; 728 int bouncingPeakDelay = 4; 729 730 int bouncingPeakCounterInc = 10; 731 732#elif defined (led_2_strip_63_qr) 733 734 //settings for 2 63 led strips - quick response 735 736 int stripNumOfLeds = 737 63; 738 int stripsOn2Pins = true; 739 uint32_t stripColor[64]; 740 int displayMiddleLed 741 = false; 742 int splitStrip = true; 743 int middleOffset = 0; 744 int startupAnimationDelay 745 = 1; 746 int orangeLimitAmount = 0; 747 int swapLeftRight = false; 748 749 int 750 dropDelay = 4; 751 float dropFactor = .92; 752 753 int peakTimeNoDropDelay = 754 200; 755 int peakTimeFirstDropDelay = 60; 756 int peakTimeDropDelay = 6; 757 float 758 peakDropFactor = .92; 759 int droppingPeakFade = false; 760 761 int bouncingPeaksNumOfLeds 762 = 12; 763 int bouncingPeaksNumOfLedsMin = 4; 764 int bouncingPeakDelay = 3; 765 766 int bouncingPeakCounterInc = 10; 767#endif 768 769// 770// setup other user variables 771// 772 773// 774 basic settings 775int pulsing = true; // pulsing 776 will display from the middle of each strip or ring @EB 777 778int spinCircle = 779 true; // spin the animation. will not work with 780 stripsOn2Pins @EB 781 782int animType = 0; // 783 startup animation selection (1 looks nice for 1 ring) @EB 784int colorScheme = 785 10; // 0: green-red, 1: blue-green, 2: blue-red, 786 3: red-blue, 4: green-blue, 5: red-green, 6: blue-white-red 787 // 788 7: red-white-blue, 8: green-white-red, 9: green-white-blue, 10: color wheel, 11: 789 spinning color wheel, 790 // 12: as 11 791 but spread with factor colorScheme12Factor @EB 792int maxColorScheme = 12; // 793 used for looping through the color schemes with the switch button 794int colorScheme11SpinDelay 795 = stripNumOfLeds / 4 ; // delay for spinning scheme 11 796int colorScheme12Factor 797 = 3; // wheel spread factor for scheme 12 @EB 798 799int 800 minValue = 10; // min analog input value 801int 802 sensitivityValue = 110; // 0 - 255, initial value 803 (value read from the potentiometer if useSensorValues = true) 804 805#ifdef highLevelInput 806 807 int maxValue = 700; // max analog input value 808 (0-1023 equals 0-5V). try 300 for low level input, 700 for high 809 int maxSensitivity 810 = 2 * 255; // set to a higher setting to amplify low input 811 levels. try 4 * 255 for low level input, 2 * 255 for high 812#else 813 int maxValue 814 = 300; // max analog input value (0-1023 equals 815 0-5V). try 300 for low level input, 700 for high 816 int maxSensitivity = 4 * 255; 817 // set to a higher setting to amplify low input levels. 818 try 4 * 255 for low level input, 2 * 255 for high 819#endif 820 821int ledBrightness 822 = 30; // 0 - 255, initial value (value read from 823 the potentiometer if useSensorValues = true) 824int sensorDeviationBrightness = 825 3; // eliminate fluctuating values 826int overflowDelay = 827 10; // overflow hold time 828 829// peak settings 830 @EB 831int displayPeaks = false; // value will be 832 set by the switch if useSensorValues = true 833int displayTopAsPeak = true; // 834 always display the top LED in peak color 835int droppingPeak = true; // 836 display dropping peaks or not. note: displayPeaks has to be true 837int bouncingPeaks 838 = false; // display bouncing peaks or not. note: 839 displayPeaks has to be true 840int dynamicBouncingPeaks = false; // 841 bounce less with lower peaks. note: bouncingPeaks has to be true 842 843// 844// 845 initialize other variables 846// 847 848int numOfSegments, halfNumOfSegments, stripMiddle, 849 maxDisplaySegments; 850float sensitivityFactor; 851float nonLinearResponseFactor; 852 853int 854 brightnessValue, prevBrightnessValue; 855float ledFactor, ledFactor_div_numOfSegments; 856 857uint32_t 858 stripMiddleColor, stripOverflowColor, stripHoldColor; 859uint32_t colorValue; 860 861int 862 leftValue = 0, rightValue = 0, maxReadValue = 0; 863int leftValueN = 0, rightValueN 864 = 0; 865int leftAnalogValue = 0, rightAnalogValue = 0; 866float log10MaxDisplaySegments; 867 868int 869 prevLeftValue = 0, prevRightValue = 0; 870int prevLeftAnalogValue = 0, prevRightAnalogValue 871 = 0; 872 873int selectButton1PinState = 0, prevSelectButton1PinState = 0; 874int 875 selectButton2PinState = 0, prevSelectButton2PinState = 0; 876 877int selectButton1PinSetting 878 = colorScheme; 879int selectButton2PinSetting = 0; 880 881int i, j; 882int dropLeft, 883 dropRight; 884int leftDropTime, rightDropTime; 885 886int leftPeak = 0, rightPeak 887 = 0; 888int leftPeakTime = 0, rightPeakTime = 0; 889int leftFirstPeak = true, rightFirstPeak 890 = true; 891int showPeaksPinSetting, prevShowPeaksPinSetting; 892 893int stripPulseMiddle 894 = 0; 895int halfLeftValue, halfRightValue, halfPrevLeftValue, halfPrevRightValue; 896 897int 898 leftPeakBouncing = false, rightPeakBouncing = false; 899int leftPeakBounce = 0, 900 rightPeakBounce = 0; 901int prevLeftPeakBounce = 0, prevRightPeakBounce = 0; 902int 903 leftPeakBounceCounter = 0, rightPeakBounceCounter = 0; 904int leftPeakBounceDelayCounter 905 = 0, rightPeakBounceDelayCounter = 0; 906int leftBouncingPeaksNumOfLeds = 0, rightBouncingPeaksNumOfLeds 907 = 0; 908float bounceFactor; 909 910int colorScheme11SpinValue = 0, colorScheme11SpinDelayValue 911 = 0; 912int colorSchemeFactor = 1; 913long selectButton1Timer; 914 915int spinDelayCounter 916 = 0, spinCounter = 0, spinTurnsCounter = 0, spinTurnsMax = 0, spinTurnsDelay = 0, 917 spinTurnsDelayMax = 0; 918int spinCounterInc = 1; 919int spinDelay = 0; 920// 921// 922 initialize the strip or rings 923// 924 925Adafruit_NeoPixel left_strip = Adafruit_NeoPixel(stripNumOfLeds, 926 leftStripPin, NEO_GRB + NEO_KHZ800); 927Adafruit_NeoPixel right_strip = Adafruit_NeoPixel(stripNumOfLeds, 928 rightStripPin, NEO_GRB + NEO_KHZ800); 929 930// 931// setup 932// 933 934void setup() 935 { 936 #ifdef DEBUG 937 Serial.begin(9600); 938 #endif 939 940 randomSeed(analogRead(2)); 941 942 943 if (stripsOn2Pins) { 944 numOfSegments = stripNumOfLeds; 945 maxDisplaySegments 946 = numOfSegments - 1; 947 948 stripMiddle = stripNumOfLeds; 949 stripPulseMiddle 950 = stripMiddle / 2; 951 spinCircle = false; 952 } 953 else { 954 numOfSegments 955 = stripNumOfLeds / 2; 956 stripMiddle = stripNumOfLeds / 2; 957 maxDisplaySegments 958 = stripMiddle - 1; 959 960 stripPulseMiddle = stripMiddle / 2; 961 } 962 963 964 halfNumOfSegments = numOfSegments / 2; 965 bounceFactor = (float) bouncingPeaksNumOfLeds 966 / (maxDisplaySegments - bouncingPeaksNumOfLeds); 967 nonLinearResponseFactor = 968 90 / (float) maxDisplaySegments; 969 log10MaxDisplaySegments = log10(maxDisplaySegments); 970 971 972 pinMode(showPeaksPin, INPUT); 973 974 if (useSelectButton1) 975 pinMode(selectButton1Pin, 976 INPUT); 977 978 left_strip.begin(); 979 if (stripsOn2Pins) 980 right_strip.begin(); 981 982 983 if (useSensorValues) { 984 readSensorValues(); 985 setInitialDisplayPeaks(); 986 987 } 988 else { 989 setStripColors(); 990 setSensitivityFactor(); 991 } 992 993 994 #ifdef DEBUG_TEST_LEDS 995 displayTest(); 996 #endif 997 998 startupAnimation(); 999} 1000 1001// 1002// 1003 main loop 1004// 1005 1006void loop() { 1007 #ifdef DEBUG_PRINT_LOOP_TIME 1008 long 1009 time = millis(); 1010 #endif 1011 1012 1013 if (useSensorValues) 1014 readSensorValues(); 1015 1016 1017 readValues(); 1018 1019 #if defined (DEBUG_NO_PEAKS) 1020 displayPeaks 1021 = false; 1022 #endif 1023 1024 #if defined (DEBUG_PEAKS) 1025 displayPeaks = true; 1026 1027 #endif 1028 1029 if (pulsing) { 1030 drawPulsingValues(); 1031 } 1032 else { 1033 1034 drawValues(); 1035 if (displayPeaks) { 1036 getPeaks(); 1037 drawPeaks(); 1038 1039 } 1040 } 1041 1042 left_strip.show(); 1043 if (stripsOn2Pins) 1044 right_strip.show(); 1045 1046 1047 storePrevValues(); 1048 1049 checkSpinCircle(); 1050 1051 #ifdef DEBUG_PRINT_LOOP_TIME 1052 1053 time = millis() - time; 1054 Serial.println(time); 1055 #endif 1056} 1057 1058// 1059 1060// functions 1061// 1062 1063void setInitialDisplayPeaks() { 1064 #if !defined 1065 (DEBUG_NO_PEAK_SWITCH) 1066 showPeaksPinSetting = digitalRead(showPeaksPin); 1067 1068 1069 if (showPeaksPinSetting == HIGH) 1070 displayPeaks = false; 1071 #endif 1072 1073 1074 if (reverseShowPeaks) { 1075 if (!displayPeaks) 1076 displayPeaks 1077 = true; 1078 else 1079 displayPeaks = false; 1080 } 1081 1082 prevShowPeaksPinSetting 1083 = showPeaksPinSetting; 1084} 1085 1086void readSensorValues() { 1087 // 1088 // peaks 1089 pin 1090 // 1091 1092 #if !defined (DEBUG_NO_PEAK_SWITCH) 1093 showPeaksPinSetting 1094 = digitalRead(showPeaksPin); 1095 1096 if (showPeaksMomentarySwitch) { 1097 if 1098 (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { 1099 if 1100 (displayPeaks == true) { 1101 displayPeaks = false; 1102 clearLeftPeak(); 1103 1104 clearRightPeak(); 1105 if (showPeaksMomentarySwitch) 1106 1107 while (digitalRead(showPeaksPin) == LOW) {} 1108 } 1109 else 1110 { 1111 displayPeaks = true; 1112 } 1113 } 1114 } 1115 else 1116 { 1117 if (reverseShowPeaks) { 1118 if (showPeaksPinSetting == HIGH && 1119 prevShowPeaksPinSetting == LOW) 1120 displayPeaks = true; 1121 else 1122 if (showPeaksPinSetting == LOW && prevShowPeaksPinSetting == HIGH) { 1123 displayPeaks 1124 = false; 1125 clearLeftPeak(); 1126 clearRightPeak(); 1127 1128 } 1129 } 1130 else { 1131 if (showPeaksPinSetting == LOW 1132 && prevShowPeaksPinSetting == HIGH) 1133 displayPeaks = true; 1134 else 1135 if (showPeaksPinSetting == HIGH && prevShowPeaksPinSetting == LOW) { 1136 displayPeaks 1137 = false; 1138 clearLeftPeak(); 1139 clearRightPeak(); 1140 1141 } 1142 } 1143 } 1144 if (pulsing) { 1145 if (displayPeaks) 1146 1147 displayTopAsPeak = true; 1148 else 1149 displayTopAsPeak = false; 1150 1151 } 1152 1153 prevShowPeaksPinSetting = showPeaksPinSetting; 1154 #endif 1155 1156 1157 1158 // 1159 // selectButtonPin 1 and 2 1160 // 1161 if (useSelectButton1) 1162 { 1163 selectButton1PinState = digitalRead(selectButton1Pin); 1164 1165 1166 if (selectButton1PinState == HIGH && prevSelectButton1PinState == LOW) 1167 selectButton1Timer 1168 = millis(); 1169 1170 if (selectButton1PinState == HIGH && prevSelectButton1PinState 1171 == HIGH) { 1172 if ((millis() - selectButton1Timer) > 1000) { 1173 pulsing 1174 = !pulsing; 1175 setStripColors(); 1176 displayNumber(colorScheme, 250); 1177 1178 1179 while (digitalRead(selectButton1Pin) == HIGH) {} 1180 selectButton1PinState 1181 = LOW; 1182 clearValues(); 1183 } 1184 } 1185 else if (selectButton1PinState 1186 == LOW && prevSelectButton1PinState == HIGH) { 1187 selectButton1PinSetting++; 1188 1189 if (selectButton1PinSetting > maxColorScheme) { 1190 selectButton1PinSetting 1191 = 0; 1192 } 1193 colorScheme = selectButton1PinSetting; 1194 1195 if 1196 (colorScheme == 12) 1197 colorScheme11SpinValue = (colorScheme11SpinValue 1198 * colorScheme12Factor); 1199 1200 setStripColors(); 1201 displayNumber(colorScheme, 1202 250); 1203 } 1204 prevSelectButton1PinState = selectButton1PinState; 1205 } 1206 1207 1208 if (useSelectButton2) { 1209 selectButton2PinState = digitalRead(selectButton2Pin); 1210 1211 1212 if (selectButton2PinState == HIGH && prevSelectButton2PinState == LOW) 1213 { 1214 selectButton2PinSetting++; 1215 1216 switch(selectButton2PinSetting) 1217 { 1218 case 0: 1219 case 3: { 1220 pulsing = false; 1221 spinCircle 1222 = false; 1223 selectButton2PinSetting = 0; 1224 break; 1225 } 1226 1227 case 1: { 1228 pulsing = true; 1229 spinCircle= false; 1230 1231 break; 1232 } 1233 case 2: { 1234 pulsing = true; 1235 1236 spinCircle = true; 1237 break; 1238 } 1239 } 1240 1241 1242 setStripColors(); 1243 displayNumber(colorScheme, 250); 1244 } 1245 1246 1247 prevSelectButton2PinState = selectButton2PinState; 1248 } 1249 1250 // 1251 1252 // brightness 1253 // 1254 brightnessValue = analogRead(brightnessPin); 1255 brightnessValue 1256 = map(brightnessValue, 0, 1023, 0, 255); 1257 1258 if (abs(brightnessValue - prevBrightnessValue) 1259 > sensorDeviationBrightness) { 1260 ledBrightness = brightnessValue; 1261 setStripColors(); 1262 1263 prevBrightnessValue = brightnessValue; 1264 } 1265 1266 // 1267 // colorscheme 1268 11 spinning wheel 1269 // 1270 1271 if (colorScheme == 11 || colorScheme == 12) { 1272 1273 colorScheme11SpinDelayValue++; 1274 if (colorScheme11SpinDelayValue == colorScheme11SpinDelay) 1275 { 1276 colorScheme11SpinDelayValue = 0; 1277 colorScheme11SpinValue++; 1278 1279 if (colorScheme11SpinValue > maxDisplaySegments * colorSchemeFactor) 1280 colorScheme11SpinValue 1281 = 0; 1282 setStripColors(); 1283 } 1284 } 1285 1286 // 1287 // sensitivity 1288 1289 // 1290 sensitivityValue = analogRead(sensitivityPin); 1291 sensitivityValue = 1292 map(sensitivityValue, 0, 1023, 0, 255); 1293 setSensitivityFactor(); 1294} 1295 1296void 1297 setSensitivityFactor() { 1298 //sensitivityValue_div_numOfSegments = sensitivityValue 1299 / numOfSegments; 1300 sensitivityFactor = ((float) sensitivityValue / 255 * (float) 1301 maxSensitivity / 255); 1302} 1303 1304void readValues() { 1305 #ifdef averageReadings 1306 1307 leftAnalogValue = 0; 1308 rightAnalogValue = 0; 1309 1310 for (i = 0; 1311 i <= averageNumOfReadings; i++) { 1312 leftAnalogValue += analogRead(leftPin); 1313 1314 rightAnalogValue += analogRead(rightPin); 1315 } 1316 1317 leftAnalogValue 1318 /= averageNumOfReadings; 1319 rightAnalogValue /= averageNumOfReadings; 1320 1321 1322 #else 1323 leftAnalogValue = analogRead(leftPin); 1324 rightAnalogValue 1325 = analogRead(rightPin); 1326 #endif 1327 1328 if (swapLeftRight) { 1329 int tempValue 1330 = leftAnalogValue; 1331 leftAnalogValue = rightAnalogValue; 1332 rightAnalogValue 1333 = tempValue; 1334 } 1335 1336 if (leftAnalogValue < prevLeftAnalogValue) { 1337 leftDropTime++; 1338 1339 if (leftDropTime > dropDelay) { 1340 leftAnalogValue = prevLeftAnalogValue 1341 * dropFactor; 1342 leftDropTime = 0; 1343 } 1344 else 1345 leftAnalogValue 1346 = prevLeftAnalogValue; 1347 } 1348 1349 if (rightAnalogValue < prevRightAnalogValue) 1350 { 1351 rightDropTime++; 1352 if (rightDropTime > dropDelay) { 1353 rightAnalogValue 1354 = prevRightAnalogValue * dropFactor; 1355 rightDropTime = 0; 1356 } 1357 else 1358 1359 rightAnalogValue = prevRightAnalogValue; 1360 } 1361 1362 #ifdef DEBUG_PRINT_ANALOGVALUES 1363 1364 Serial.print(leftAnalogValue); 1365 Serial.print(" "); 1366 Serial.println(rightAnalogValue); 1367 1368 #endif 1369 1370 // map values 1371 leftValue = map(leftAnalogValue * sensitivityFactor, 1372 minValue, maxValue, 0, maxDisplaySegments); 1373 rightValue = map(rightAnalogValue 1374 * sensitivityFactor, minValue, maxValue, 0, maxDisplaySegments); 1375 1376 // if 1377 defined, convert to (reverse) non linear response 1378 boolean flagNonLinear = false; 1379 1380 1381 #if defined (nonLinearSinAudio) 1382 flagNonLinear = true; 1383 leftValueN 1384 = ((sin(((leftValue * nonLinearResponseFactor) + 270) * 0.0174533) + 1) * maxDisplaySegments); 1385 1386 rightValueN = ((sin(((rightValue * nonLinearResponseFactor) + 270) * 0.0174533) 1387 + 1) * maxDisplaySegments); 1388 1389 #elif defined (nonLinearReverseSinAudio) 1390 1391 flagNonLinear = true; 1392 leftValueN = ((sin(((leftValue * nonLinearResponseFactor)) 1393 * 0.0174533)) * maxDisplaySegments); 1394 rightValueN = ((sin(((rightValue * nonLinearResponseFactor)) 1395 * 0.0174533)) * maxDisplaySegments); 1396 1397 #elif defined (nonLinearLogAudio) 1398 1399 flagNonLinear = true; 1400 leftValueN = ((log10(leftValue + 1) / log10MaxDisplaySegments 1401 * maxDisplaySegments)); 1402 rightValueN = ((log10(rightValue + 1) / log10MaxDisplaySegments 1403 * maxDisplaySegments)); 1404 1405 #endif 1406 1407 if (flagNonLinear == true) 1408 { 1409 #if defined (nonLinearAvr2) 1410 leftValue = (leftValue + leftValueN) 1411 / 2; 1412 rightValue = (rightValue + rightValueN) / 2; 1413 #else 1414 leftValue 1415 = leftValueN; 1416 rightValue = rightValueN; 1417 #endif 1418 } 1419 1420// 1421 @EB_DEBUG 1422 1423 #ifdef displayOverflow 1424 #ifdef compressOverflowPeaks 1425 1426 for (i = 1; i <= compressOverflowNumOfTimes; i++) { 1427 if (leftValue 1428 > maxDisplaySegments) { 1429// Serial.print(i); 1430// Serial.print(" 1431 "); 1432// Serial.print(leftValue); 1433// Serial.print(" 1434 "); 1435 leftValue = leftValue - leftValue * compressOverflowFactor * 1436 i; 1437// Serial.print(leftValue); 1438// Serial.print(" "); 1439// 1440 Serial.println(maxDisplaySegments); 1441 } 1442 } 1443 #endif 1444 1445 #endif 1446 1447 if (leftValue > maxDisplaySegments) { 1448 leftValue 1449 = maxDisplaySegments; 1450 #ifdef displayOverflow 1451 drawOverflow(); 1452 1453 #endif 1454 } 1455 1456 #ifdef displayOverflow 1457 #ifdef compressOverflowPeaks 1458 1459 if (rightValue > maxDisplaySegments) 1460 rightValue = rightValue - 1461 rightValue * compressOverflowFactor; 1462 #endif 1463 #endif 1464 1465 if (rightValue 1466 > maxDisplaySegments) { 1467 rightValue = maxDisplaySegments; 1468 #ifdef displayOverflow 1469 1470 drawOverflow(); 1471 #endif 1472 } 1473} 1474 1475void storePrevValues() { 1476 1477 prevLeftAnalogValue = leftAnalogValue; 1478 prevRightAnalogValue = rightAnalogValue; 1479 1480 1481 prevLeftValue = leftValue; 1482 prevRightValue = rightValue; 1483} 1484 1485void 1486 getPeaks() { 1487 if (leftValue > leftPeak) { 1488 if (dynamicBouncingPeaks || 1489 prevLeftPeakBounce > 0) 1490 clearLeftBouncePeak(); 1491 1492 leftPeak 1493 = leftValue; 1494 leftPeakTime = 0; 1495 leftFirstPeak = true; 1496 1497 if 1498 (bouncingPeaks) { 1499 leftPeakBouncing = true; 1500 leftPeakBounceCounter 1501 = 0; 1502 leftPeakBounceDelayCounter = 0; 1503 1504 if (dynamicBouncingPeaks) 1505 1506 leftBouncingPeaksNumOfLeds = max(bouncingPeaksNumOfLedsMin, (leftPeak * 1507 bounceFactor)); 1508 else 1509 leftBouncingPeaksNumOfLeds = bouncingPeaksNumOfLeds; 1510 1511 } 1512 } 1513 else { 1514 leftPeakTime++; 1515 if (droppingPeak) { 1516 if 1517 (leftFirstPeak) { 1518 if (leftPeakTime > peakTimeFirstDropDelay) { 1519 clearLeftPeak(); 1520 1521 leftFirstPeak = false; 1522 } 1523 } 1524 else { 1525 if 1526 (leftPeakTime > peakTimeDropDelay) { 1527 clearLeftPeak(); 1528 } 1529 1530 } 1531 } 1532 else { 1533 if (leftPeakTime > peakTimeNoDropDelay) 1534 { 1535 clearLeftPeak(); 1536 } 1537 } 1538 } 1539 1540 if (leftPeakBouncing) 1541 { 1542 if (leftFirstPeak) { 1543 leftPeakBounceDelayCounter++; 1544 if 1545 (leftPeakBounceDelayCounter >= bouncingPeakDelay) { 1546 leftPeakBounceDelayCounter 1547 = 0; 1548 leftPeakBounceCounter += bouncingPeakCounterInc; 1549 if (leftPeakBounceCounter 1550 >= 180) { 1551 clearLeftBouncePeak(); 1552 clearLeftBounce(); 1553 1554 } 1555 else { 1556 leftPeakBounce = min((sin(leftPeakBounceCounter 1557 * 0.0174533) * leftBouncingPeaksNumOfLeds), (maxDisplaySegments - leftPeak)); 1558 1559 if (leftPeakBounce != prevLeftPeakBounce) { 1560 clearLeftBouncePeak(); 1561 1562 } 1563 prevLeftPeakBounce = leftPeakBounce; 1564 } 1565 } 1566 1567 } 1568 } 1569 1570 if (rightValue > rightPeak) { 1571 if (dynamicBouncingPeaks 1572 || prevRightPeakBounce > 0) 1573 clearRightBouncePeak(); 1574 1575 rightPeak 1576 = rightValue; 1577 rightPeakTime = 0; 1578 rightFirstPeak = true; 1579 1580 if 1581 (bouncingPeaks) { 1582 rightPeakBouncing = true; 1583 rightPeakBounceCounter 1584 = 0; 1585 rightPeakBounceDelayCounter = 0; 1586 1587 if (dynamicBouncingPeaks) 1588 1589 rightBouncingPeaksNumOfLeds = max(bouncingPeaksNumOfLedsMin, (rightPeak 1590 * bounceFactor)); 1591 else 1592 rightBouncingPeaksNumOfLeds = bouncingPeaksNumOfLeds; 1593 1594 } 1595 } 1596 else { 1597 rightPeakTime++; 1598 if (droppingPeak) { 1599 1600 if (rightFirstPeak) { 1601 if (rightPeakTime > peakTimeFirstDropDelay) 1602 { 1603 clearRightPeak(); 1604 rightFirstPeak = false; 1605 } 1606 1607 } 1608 else { 1609 if (rightPeakTime > peakTimeDropDelay) 1610 clearRightPeak(); 1611 1612 } 1613 } 1614 else { 1615 if (rightPeakTime > peakTimeNoDropDelay) 1616 1617 clearRightPeak(); 1618 } 1619 } 1620 1621 if (rightPeakBouncing) { 1622 1623 if (rightFirstPeak) { 1624 rightPeakBounceDelayCounter++; 1625 if (rightPeakBounceDelayCounter 1626 >= bouncingPeakDelay) { 1627 rightPeakBounceDelayCounter = 0; 1628 rightPeakBounceCounter 1629 += bouncingPeakCounterInc; 1630 1631 if (rightPeakBounceCounter >= 180) { 1632 1633 clearRightBouncePeak(); 1634 clearRightBounce(); 1635 } 1636 1637 else { 1638 rightPeakBounce = min((sin(rightPeakBounceCounter 1639 * 0.0174533) * rightBouncingPeaksNumOfLeds), (maxDisplaySegments - rightPeak)); 1640 1641 if (rightPeakBounce != prevRightPeakBounce) { 1642 clearRightBouncePeak(); 1643 1644 } 1645 prevRightPeakBounce = rightPeakBounce; 1646 } 1647 1648 } 1649 } 1650 } 1651} 1652 1653void checkSpinCircle () { 1654 if (spinCircle) 1655 { 1656 if (spinTurnsMax == 0) { 1657 spinTurnsMax = random(stripNumOfLeds 1658 / 4, stripNumOfLeds * 3); // spin at least a quarter turn, max 3 turns 1659 1660 1661 if (random(10) > 4) 1662 spinCounterInc = -spinCounterInc; 1663 1664 1665 spinTurnsDelayMax = random(100, 1000); // @EB_DEBUG 1666 1667 spinDelay 1668 = random(20, 75); // @EB_DEBUG 1669 } 1670 1671 if (spinTurnsCounter == spinTurnsMax) 1672 { 1673 spinTurnsDelay++; 1674 if (spinTurnsDelay == spinTurnsDelayMax) { 1675 1676 spinTurnsDelay = 0; 1677 spinTurnsCounter = 0; 1678 spinTurnsMax 1679 = 0; 1680 } 1681 } 1682 else { 1683 spinDelayCounter++; 1684 1685 if 1686 (spinDelayCounter > spinDelay) { 1687 clearZeroAndPeaks(); 1688 1689 spinCounter 1690 += spinCounterInc; 1691 if (spinCounter > stripNumOfLeds) 1692 spinCounter 1693 = 0; 1694 else if (spinCounter < 0) 1695 spinCounter = stripNumOfLeds; 1696 1697 1698 spinTurnsCounter++; 1699 spinDelayCounter = 0; 1700 } 1701 } 1702 1703 } 1704} 1705 1706int getSpinCircleValue(int value) { 1707 if (!spinCircle) 1708 return 1709 value; 1710 else { 1711 int calcValue = value + spinCounter; 1712 if (calcValue 1713 >= stripNumOfLeds) 1714 calcValue -= stripNumOfLeds; 1715 return calcValue; 1716 1717 } 1718} 1719 1720void drawValues() { 1721 if (splitStrip) { 1722 for (i = middleOffset; 1723 i < leftValue; i++) 1724 left_strip.setPixelColor(getSpinCircleValue(i), stripColor[i]); 1725 1726 1727 if (!displayPeaks && displayTopAsPeak) 1728 left_strip.setPixelColor(getSpinCircleValue(leftValue), 1729 stripHoldColor); 1730 1731 for (i = prevLeftValue; i >= leftValue; i--) 1732 left_strip.setPixelColor(getSpinCircleValue(i), 1733 0); 1734 1735 if (stripsOn2Pins) { 1736 for (i = middleOffset; i < rightValue; 1737 i++) 1738 right_strip.setPixelColor(i, stripColor[i]); 1739 1740 if (!displayPeaks 1741 && displayTopAsPeak) 1742 right_strip.setPixelColor(rightValue, stripHoldColor); 1743 1744 1745 for (i = prevRightValue; i >= rightValue; i--) 1746 right_strip.setPixelColor(i, 1747 0); 1748 } 1749 else { 1750 for (i = middleOffset; i < rightValue; i++) 1751 1752 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), stripColor[i]); 1753 1754 1755 if (!displayPeaks && displayTopAsPeak) 1756 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1757 + rightValue), stripHoldColor); 1758 1759 for (i = prevRightValue; i 1760 >= rightValue; i--) 1761 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1762 + i), 0); 1763 } 1764 } 1765 else { 1766 for (i = middleOffset; i < leftValue; 1767 i++) 1768 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), stripColor[i]); 1769 1770 1771 if (!displayPeaks && displayTopAsPeak) 1772 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1773 + leftValue), stripHoldColor); 1774 1775 for (i = prevLeftValue; i >= leftValue; 1776 i--) 1777 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + i), 0); 1778 1779 1780 for (i = middleOffset; i < rightValue; i++) 1781 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1782 - i), stripColor[i]); 1783 1784 if (!displayPeaks && displayTopAsPeak) 1785 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1786 - rightValue), stripHoldColor); 1787 1788 for (i = prevRightValue; i >= rightValue; 1789 i--) 1790 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - i), 0); 1791 1792 } 1793 1794 if (displayMiddleLed) 1795 left_strip.setPixelColor(getSpinCircleValue(stripMiddle), 1796 stripMiddleColor); 1797} 1798 1799void drawPulsingValues() { 1800 halfLeftValue = (leftValue 1801 + 1) / 2; 1802 halfRightValue = (rightValue + 1) / 2; 1803 halfPrevLeftValue = (prevLeftValue 1804 + 1)/ 2; 1805 halfPrevRightValue = (prevRightValue + 1) / 2; 1806 1807 if (splitStrip) 1808 { 1809 for (i = 0; i < halfLeftValue; i++) { 1810 colorValue = stripColor[i 1811 * 2]; 1812 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle + i), 1813 colorValue); 1814 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1815 - i), colorValue); 1816 } 1817 1818 if (displayTopAsPeak) { 1819 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1820 + halfLeftValue), stripHoldColor); 1821 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1822 - halfLeftValue), stripHoldColor); 1823 } 1824 1825 for (i = halfPrevLeftValue; 1826 i >= halfLeftValue; i--) { 1827 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1828 + i), 0); 1829 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1830 - i), 0); 1831 } 1832 1833 if (stripsOn2Pins) { 1834 for (i = 0; i < halfRightValue; 1835 i++) { 1836 colorValue = stripColor[i * 2]; 1837 right_strip.setPixelColor((stripPulseMiddle 1838 + i), colorValue); 1839 right_strip.setPixelColor((stripPulseMiddle - i), 1840 colorValue); 1841 } 1842 1843 if (displayTopAsPeak) { 1844 right_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1845 + halfRightValue), stripHoldColor); 1846 right_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1847 - halfRightValue), stripHoldColor); 1848 } 1849 1850 for (i = halfPrevRightValue; 1851 i >= halfRightValue; i--) { 1852 right_strip.setPixelColor((stripPulseMiddle 1853 + i), 0); 1854 right_strip.setPixelColor((stripPulseMiddle - i), 0); 1855 } 1856 1857 } 1858 else { 1859 for (i = 0; i < halfRightValue; i++) { 1860 colorValue 1861 = colorValue = stripColor[i * 2]; 1862 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1863 + stripPulseMiddle + i), colorValue); 1864 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1865 + stripPulseMiddle - i), colorValue); 1866 } 1867 1868 if (displayTopAsPeak) 1869 { 1870 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle 1871 + halfRightValue), stripHoldColor); 1872 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1873 + stripPulseMiddle - halfRightValue), stripHoldColor); 1874 } 1875 1876 for 1877 (i = halfPrevRightValue; i >= halfRightValue; i--) { 1878 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1879 + stripPulseMiddle + i), 0); 1880 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1881 + stripPulseMiddle - i), 0); 1882 } 1883 } 1884 } 1885 else { 1886 for (i 1887 = 0; i < halfLeftValue; i++) { 1888 colorValue = colorValue = stripColor[i * 1889 2]; 1890 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle 1891 + i), colorValue); 1892 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1893 + stripPulseMiddle - i), colorValue); 1894 } 1895 1896 if (displayTopAsPeak) 1897 { 1898 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + stripPulseMiddle 1899 + halfLeftValue), stripHoldColor); 1900 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1901 + stripPulseMiddle - halfLeftValue), stripHoldColor); 1902 } 1903 1904 for 1905 (i = halfPrevLeftValue; i >= halfLeftValue; i--) { 1906 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1907 + stripPulseMiddle + i), 0); 1908 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1909 + stripPulseMiddle - i), 0); 1910 } 1911 1912 for (i = 0; i < halfRightValue; 1913 i++) { 1914 colorValue = colorValue = stripColor[i * 2]; 1915 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1916 - (stripPulseMiddle + i)), colorValue); 1917 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1918 - (stripPulseMiddle - i)), colorValue); 1919 } 1920 1921 if (displayTopAsPeak) 1922 { 1923 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (stripPulseMiddle 1924 + halfRightValue)), stripHoldColor); 1925 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1926 - (stripPulseMiddle - halfRightValue)), stripHoldColor); 1927 } 1928 1929 for 1930 (i = halfPrevRightValue; i >= halfRightValue; i--) { 1931 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1932 - (stripPulseMiddle + i)), 0); 1933 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1934 - (stripPulseMiddle - i)), 0); 1935 } 1936 } 1937 1938 if (displayMiddleLed) { 1939 1940 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - stripPulseMiddle), 1941 stripMiddleColor); 1942 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1943 + stripPulseMiddle), stripMiddleColor); 1944 } 1945} 1946 1947void clearZeroAndPeaks() 1948 { 1949 left_strip.setPixelColor(getSpinCircleValue(middleOffset), 0); 1950 left_strip.setPixelColor(getSpinCircleValue(stripMiddle), 1951 0); 1952 1953 if (displayTopAsPeak) { 1954 if (splitStrip) { 1955 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1956 + halfLeftValue), 0); 1957 left_strip.setPixelColor(getSpinCircleValue(stripPulseMiddle 1958 - halfLeftValue), 0); 1959 1960 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1961 + stripPulseMiddle + halfRightValue), 0); 1962 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1963 + stripPulseMiddle - halfRightValue), 0); 1964 } 1965 else { 1966 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1967 + stripPulseMiddle + halfLeftValue), 0); 1968 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1969 + stripPulseMiddle - halfLeftValue), 0); 1970 1971 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1972 - (stripPulseMiddle + halfRightValue)), 0); 1973 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1974 - (stripPulseMiddle - halfRightValue)), 0); 1975 } 1976 } 1977} 1978 1979void drawPeaks() 1980 { 1981 if (leftPeak > 0) { 1982 if (droppingPeakFade && leftPeakBouncing == 1983 false) 1984 stripHoldColor = left_strip.Color(max(1, (255 * leftPeak * ledFactor_div_numOfSegments)), 1985 0, 0); 1986 else 1987 stripHoldColor = stripColor[numOfSegments]; 1988 1989 1990 if (splitStrip) 1991 left_strip.setPixelColor(getSpinCircleValue(leftPeak 1992 + leftPeakBounce), stripHoldColor); 1993 else 1994 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 1995 + (leftPeak + leftPeakBounce)), stripHoldColor); 1996 } 1997 1998 if (rightPeak 1999 > 0) { 2000 if (droppingPeakFade && rightPeakBouncing == false) 2001 stripHoldColor 2002 = left_strip.Color(max(1, (255 * rightPeak * ledFactor_div_numOfSegments)), 0, 0); 2003 2004 else 2005 stripHoldColor = stripColor[numOfSegments]; 2006 2007 if 2008 (splitStrip) { 2009 if (stripsOn2Pins) { 2010 right_strip.setPixelColor(getSpinCircleValue(rightPeak 2011 + rightPeakBounce), stripHoldColor); 2012 } 2013 else { 2014 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2015 + rightPeak + rightPeakBounce), stripHoldColor); 2016 } 2017 } 2018 else 2019 { 2020 left_strip.setPixelColor(getSpinCircleValue(stripMiddle - (rightPeak 2021 + rightPeakBounce)), stripHoldColor); 2022 } 2023 } 2024 2025 //if (leftPeak > 2026 0 || rightPeak > 0) 2027 // left_strip.show(); 2028} 2029 2030void clearLeftPeak() 2031 { 2032 if (splitStrip) 2033 left_strip.setPixelColor(getSpinCircleValue(leftPeak 2034 + prevLeftPeakBounce), 0); 2035 else 2036 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2037 + (leftPeak + prevLeftPeakBounce)), 0); 2038 2039 if (droppingPeak) 2040 leftPeak 2041 = leftPeak * peakDropFactor; 2042 else 2043 leftPeak = 0; 2044 leftPeakTime = 2045 0; 2046} 2047 2048void clearRightPeak() { 2049 if (splitStrip) { 2050 if( stripsOn2Pins) 2051 { 2052 right_strip.setPixelColor(getSpinCircleValue(rightPeak + prevRightPeakBounce), 2053 0); 2054 } 2055 else { 2056 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2057 + rightPeak + prevRightPeakBounce), 0); 2058 } 2059 } 2060 else { 2061 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2062 - (rightPeak + prevRightPeakBounce)), 0); 2063 } 2064 2065 if (droppingPeak) 2066 2067 rightPeak = rightPeak * peakDropFactor; 2068 else 2069 rightPeak = 0; 2070 2071 rightPeakTime = 0; 2072} 2073 2074void clearLeftBouncePeak() { 2075 if (splitStrip) 2076 2077 left_strip.setPixelColor(getSpinCircleValue(leftPeak + prevLeftPeakBounce), 2078 0); 2079 else 2080 left_strip.setPixelColor(getSpinCircleValue(stripMiddle + (leftPeak 2081 + prevLeftPeakBounce)), 0); 2082} 2083 2084void clearRightBouncePeak() { 2085 if (splitStrip) 2086 { 2087 if (stripsOn2Pins) { 2088 right_strip.setPixelColor(getSpinCircleValue(rightPeak 2089 + prevRightPeakBounce), 0); 2090 } 2091 else { 2092 left_strip.setPixelColor(getSpinCircleValue((stripMiddle 2093 + rightPeak + prevRightPeakBounce)), 0); 2094 } 2095 } 2096 else { 2097 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2098 - (rightPeak + prevRightPeakBounce)), 0); 2099 } 2100} 2101 2102void clearLeftBounce() 2103 { 2104 leftPeakBouncing = false; 2105 leftPeakBounceCounter = 0; 2106 leftPeakBounce 2107 = 0; 2108 prevLeftPeakBounce = 0; 2109 leftBouncingPeaksNumOfLeds = 0; 2110} 2111 2112void 2113 clearRightBounce() { 2114 rightPeakBouncing = false; 2115 rightPeakBounceCounter 2116 = 0; 2117 rightPeakBounce = 0; 2118 prevRightPeakBounce = 0; 2119 leftBouncingPeaksNumOfLeds 2120 = 0; 2121} 2122 2123void clearValues() { 2124 leftAnalogValue = 0; 2125 rightAnalogValue 2126 = 0; 2127 prevLeftAnalogValue = 0; 2128 prevRightAnalogValue = 0; 2129 leftPeak 2130 = 0; 2131 rightPeak = 0; 2132} 2133 2134 2135void drawOverflow() { 2136 for (i = 0; 2137 i <= numOfSegments; i++) { 2138 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2139 - i), stripOverflowColor); 2140 if (stripsOn2Pins) { 2141 right_strip.setPixelColor(i, 2142 stripOverflowColor); 2143 } 2144 else { 2145 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2146 + i), stripOverflowColor); 2147 } 2148 } 2149 left_strip.show(); 2150 if (stripsOn2Pins) 2151 2152 right_strip.show(); 2153 2154 delay(overflowDelay); 2155 2156 for (i = 0; i 2157 <= numOfSegments; i++) { 2158 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2159 - i), 0); 2160 if (stripsOn2Pins) { 2161 right_strip.setPixelColor(i, 0); 2162 2163 } 2164 else { 2165 left_strip.setPixelColor(getSpinCircleValue(stripMiddle 2166 + i), 0); 2167 } 2168 } 2169 left_strip.show(); 2170 if (stripsOn2Pins) 2171 right_strip.show(); 2172} 2173 2174void 2175 setStripColors() { 2176 int r, g, b; 2177 int p1, p2; 2178 2179 ledFactor = (float) 2180 ledBrightness / 255; 2181 ledFactor_div_numOfSegments = (float) ledFactor / (float) 2182 numOfSegments; 2183 stripMiddleColor = left_strip.Color(0, 0, 255 * ledFactor); 2184