|
| 1 | +/* |
| 2 | + * Plasma |
| 3 | + * |
| 4 | + * This is a demonstration of the plasma algorithm displayed on an RGB LED matrix. |
| 5 | + * The COLOR_SCHEME global variable allows you to easily change between some pre-made |
| 6 | + * color schemes. Using the patterm of the demonstration color schemes, it should be |
| 7 | + * easy to add others. |
| 8 | + * |
| 9 | + * The algorithm for this demo was sourced from here: |
| 10 | + * |
| 11 | + * https://www.bidouille.org/prog/plasma |
| 12 | + * |
| 13 | + */ |
| 14 | +#include <RGBLEDMatrix.h> |
| 15 | +#include <RGBColor.h> |
| 16 | +#include <RGBImage.h> |
| 17 | + |
| 18 | +/* Color Schemes |
| 19 | + * |
| 20 | + * 1 - Full rainbow gradients in RGB |
| 21 | + * 2 - Red, Orange, white gradients |
| 22 | + * 3 - gray scale |
| 23 | + * |
| 24 | + */ |
| 25 | +const int COLOR_SCHEME = 1; |
| 26 | + |
| 27 | +const float SPACE_STRETCH_FACTOR = 5.0; |
| 28 | +const float TIME_DILATION = 5.0; |
| 29 | + |
| 30 | +RGBLEDMatrix leds(10,10); |
| 31 | + |
| 32 | +int mapSineToRange( float sineValue, int rangeMax ) { |
| 33 | + return rangeMax*(sineValue+1.0)/2.0; |
| 34 | +} |
| 35 | +void drawPlasma( unsigned long counter ) { |
| 36 | + float utime = float(counter)/TIME_DILATION; |
| 37 | + |
| 38 | + leds.startDrawing(); |
| 39 | + for (int col = 0; col < leds.columns(); col++ ) { |
| 40 | + float x = ((float)col/((float)leds.columns()*SPACE_STRETCH_FACTOR)) - 0.5; |
| 41 | + |
| 42 | + for (int row = 0; row < leds.rows(); row++ ) { |
| 43 | + float y = ((float)row/((float)leds.rows()*SPACE_STRETCH_FACTOR)) - 0.5; |
| 44 | + |
| 45 | + float v1 = sin(x*10.0+utime); |
| 46 | + float v2 = sin(10.0*(x*sin(utime/2.0) + y*cos(utime/3.0)) + utime); |
| 47 | + |
| 48 | + float cx = x + 0.5*sin(utime/5.0); |
| 49 | + float cy = y + 0.5*cos(utime/3.0); |
| 50 | + float v3 = sin( sqrt(100.0*(cx*cx + cy*cy) + 1.0) + utime ); |
| 51 | + |
| 52 | + float v = v1+v2+v3; |
| 53 | + |
| 54 | + int r, g, b; |
| 55 | + switch (COLOR_SCHEME) { |
| 56 | + default: |
| 57 | + case 1: |
| 58 | + r = mapSineToRange(sin(v*PI), 255); |
| 59 | + g = mapSineToRange(sin(v*PI + 2.0*PI/3.0), 255); |
| 60 | + b = mapSineToRange(sin(v*PI + 4.0*PI/3.0), 255); |
| 61 | + break; |
| 62 | + case 2: |
| 63 | + r = 255; |
| 64 | + g = mapSineToRange(cos(v*PI), 255); |
| 65 | + b = mapSineToRange(sin(v*PI), 255); |
| 66 | + break; |
| 67 | + case 3: |
| 68 | + r = g = b = mapSineToRange(sin(v*5.0*PI), 255); |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + RGBColorType color = RGBColor::fromRGB(r, g, b); |
| 73 | + |
| 74 | + leds.image().pixel(row, col) = color; |
| 75 | + } |
| 76 | + } |
| 77 | + leds.stopDrawing(); |
| 78 | +} |
| 79 | + |
| 80 | +void setup() { |
| 81 | + leds.setup(); |
| 82 | + drawPlasma(0); |
| 83 | + leds.startScanning(); |
| 84 | +} |
| 85 | + |
| 86 | +unsigned long loopCounter = 0; |
| 87 | +unsigned long timeCount = 0; |
| 88 | + |
| 89 | +const unsigned long loopMod = 2000; |
| 90 | + |
| 91 | +void loop() { |
| 92 | + leds.loop(); |
| 93 | + loopCounter++; |
| 94 | + |
| 95 | + if (loopCounter == loopMod) { |
| 96 | + timeCount++; |
| 97 | + drawPlasma(timeCount); |
| 98 | + loopCounter = 0; |
| 99 | + } |
| 100 | +} |
0 commit comments