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