I’m working with a NeoPixel LED strip that supports 0-255 RGB values. I’m programming some animations for it with an Arduino Yun. I wanted a noise function, which unfortunately does not come natively with Arduino. So I decided I’d find one.

I found the following noise function, which seems optimized for Arduinos specifically. Unfortunately, I don’t fully understand it, as it has no comments and is doing some fancy bit-shifting for most of its math. It’s also programmed in a very unreadable way, but I suppose that’s to be expected with optimized code…

Relevant code here:

```
// returns a value between 0 - 255 for lights
int renderNoise(unsigned long x, unsigned long y, unsigned long z)
{ //return constrain(137 + inoise(x, y, z)>>9, 0, 255); // 127 + 10 zie Processing
return 137 + (inoise(x, y, z)>>9);
//return 137 + (inoise(x, y, z)/512);
}
long inoise(unsigned long x, unsigned long y, unsigned long z)
{
long X = x>>16 & 255, Y = y>>16 & 255, Z = z>>16 & 255, N = 1L<<16;
x &= N-1; y &= N-1; z &= N-1;
long u=fade(x),v=fade(y),w=fade(z), A=P(X)+Y, AA=P(A)+Z, AB=P(A+1)+Z, B=P(X+1)+Y, BA=P(B)+Z, BB=P(B+1)+Z;
return lerp(w, lerp(v, lerp(u, grad(P(AA), x , y , z ),
grad(P(BA), x-N , y , z )),
lerp(u, grad(P(AB), x , y-N , z ),
grad(P(BB), x-N , y-N , z ))),
lerp(v, lerp(u, grad(P(AA+1), x , y , z-N ),
grad(P(BA+1), x-N , y , z-N )),
lerp(u, grad(P(AB+1), x , y-N , z-N ),
grad(P(BB+1), x-N , y-N , z-N ))));
}
long lerp(long t, long a, long b) { return a + (t * (b - a) >> 12); }
long grad(long hash, long x, long y, long z)
{ long h = hash&15, u = h<8?x:y, v = h<4?y:h==12||h==14?x:z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
long fade(long t)
{
long m = (t >> 8) + 1;
m = m > 255 ? 255 : m;
long t0 = F(t >> 8), t1 = F(m);
return t0 + ( (t & 255) * (t1 - t0) >> 8 );
}
```

My problem is, I need the LED strip values to match on either end, as I’m going to be making a circular configuration. Thus, I need to know the period of my noise function, or be able to define it, so the first pixel value blends right into the last pixel value.

I tried modifying the “p” table to be unshuffled (0-255, in order) in the hopes that that would show the noise period, but it still showed up noisy, so I obviously don’t quite understand how this is working…

Is there another noise implementation out there that might be of more use? Or can someone tell me where in this code I can define the period?

I would like to avoid a computationally heavy solution. I would also love it if I could input the period as an argument to the function. I also only need 1D noise (just an led strip).

Thanks!