FastLED 3.9.7
Loading...
Searching...
No Matches
Noise.ino
Go to the documentation of this file.
1
4
5#include <FastLED.h>
6
7//
8// Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
9//
10
11// Params for width and height
12#if defined(__AVR_ATtinyxy4__)
13// Tiny 4x4 matrix for this memory constrained device.
14const uint8_t kMatrixWidth = 8;
15const uint8_t kMatrixHeight = 8;
16#else
17const uint8_t kMatrixWidth = 16;
18const uint8_t kMatrixHeight = 16;
19#endif
20
21#define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
22#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
23
24// Param for different pixel layouts
25const bool kMatrixSerpentineLayout = true;
26
27
28uint16_t XY( uint8_t x, uint8_t y)
29{
30 uint16_t i;
31
32 if( kMatrixSerpentineLayout == false) {
33 i = (y * kMatrixWidth) + x;
34 }
35
36 if( kMatrixSerpentineLayout == true) {
37 if( y & 0x01) {
38 // Odd rows run backwards
39 uint8_t reverseX = (kMatrixWidth - 1) - x;
40 i = (y * kMatrixWidth) + reverseX;
41 } else {
42 // Even rows run forwards
43 i = (y * kMatrixWidth) + x;
44 }
45 }
46
47 return i;
48}
49
50// The leds
51CRGB leds[kMatrixWidth * kMatrixHeight];
52
53// The 32bit version of our coordinates
54static uint16_t x;
55static uint16_t y;
56static uint16_t z;
57
58// We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
59// use the z-axis for "time". speed determines how fast time moves forward. Try
60// 1 for a very slow moving effect, or 60 for something that ends up looking like
61// water.
62// uint16_t speed = 1; // almost looks like a painting, moves very slowly
63uint16_t speed = 20; // a nice starting speed, mixes well with a scale of 100
64// uint16_t speed = 33;
65// uint16_t speed = 100; // wicked fast!
66
67// Scale determines how far apart the pixels in our noise matrix are. Try
68// changing these values around to see how it affects the motion of the display. The
69// higher the value of scale, the more "zoomed out" the noise iwll be. A value
70// of 1 will be so zoomed in, you'll mostly see solid colors.
71
72// uint16_t scale = 1; // mostly just solid colors
73// uint16_t scale = 4011; // very zoomed out and shimmery
74uint16_t scale = 311;
75
76// This is the array that we keep our computed noise values in
77uint16_t noise[MAX_DIMENSION][MAX_DIMENSION];
78
79void setup() {
80 // uncomment the following lines if you want to see FPS count information
81 // Serial.begin(38400);
82 // Serial.println("resetting!");
83 delay(3000);
84 FastLED.addLeds<WS2811,2,RGB>(leds,NUM_LEDS);
86
87 // Initialize our coordinates to some random values
88 x = random16();
89 y = random16();
90 z = random16();
91}
92
93// Fill the x/y array of 8-bit noise values using the inoise8 function.
94void fillnoise8() {
95 for(int i = 0; i < MAX_DIMENSION; i++) {
96 int ioffset = scale * i;
97 for(int j = 0; j < MAX_DIMENSION; j++) {
98 int joffset = scale * j;
99 noise[i][j] = inoise8(x + ioffset,y + joffset,z);
100 }
101 }
102 z += speed;
103}
104
105
106void loop() {
107 static uint8_t ihue=0;
108 fillnoise8();
109 for(int i = 0; i < kMatrixWidth; i++) {
110 for(int j = 0; j < kMatrixHeight; j++) {
111 // We use the value at the (i,j) coordinate in the noise
112 // array for our brightness, and the flipped value from (j,i)
113 // for our pixel's hue.
114 leds[XY(i,j)] = CHSV(noise[j][i],255,noise[i][j]);
115
116 // You can also explore other ways to constrain the hue used, like below
117 // leds[XY(i,j)] = CHSV(ihue + (noise[j][i]>>2),255,noise[i][j]);
118 }
119 }
120 ihue+=1;
121
122 FastLED.show();
123 // delay(10);
124}
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:45
central include file for FastLED, defines the CFastLED class/object
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:723
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:94
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:79
WS2811 controller class.
Definition FastLED.h:234
@ RGB
Red, Green, Blue (0012)
Definition eorder.h:15
uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z)
8-Bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:512
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:50
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54