#define PIN_LED_R 16 ////////////////////// NEOPIXELS ////////////////////// #include #define pin_NEO 8 #define ledCount 217 Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledCount, pin_NEO, NEO_GRB + NEO_KHZ800); //Conversion between the xyz coordinate of the hex grid, and the NeoPixel index int x_grid[ledCount] = {-8,-8,-8,-8,-8,-8,-8,-8,-8,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8}; int y_grid[ledCount] = {8,7,6,5,4,3,2,1,0,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,0,-1,-2,-3,-4,-5,-6,-7,-8}; int z_grid[ledCount] = {0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-8,-7,-6,-5,-4,-3,-2,-1,0}; const int hex_direction[6][3] = { {1, -1, 0}, {1, 0, -1}, {0, 1, -1}, {-1, 1, 0}, {-1, 0, 1}, {0, -1, 1} }; void setup() { //Be able to print statements - only use for testing Serial.begin(9600); pinMode(PIN_LED_R, OUTPUT); digitalWrite(PIN_LED_R, HIGH); //Neopixels strip.begin(); strip.setBrightness(map(25, 0, 100, 0, 255)); //Set the overall brightness 0 - 255 strip.show(); // Initialize all pixels to 'off' randomSeed(analogRead(0)); //EXTRA RANDOMNESS!!!!! }//void setup void randomCell(int cell[]) { int index = random(ledCount); cell[0] = x_grid[index]; //random(-8,9); cell[1] = y_grid[index]; //random(-8,9); cell[2] = z_grid[index]; //-(cell[0] + cell[1]); } void loop() { byte radius; int center[3] = {0, 0, 0}; randomCell(center); // int cube_direction[3]; // hexDirection(4, cube_direction); // Serial.print("Hex direction 4: "); // for(int i = 0; i < 3; i++) { Serial.print(cube_direction[i]); Serial.print(" "); } // Serial.println(""); int del = 100; offLights(); strip.setPixelColor(hexToIndex(center[0], center[1], center[2]), 255, 0, 0); strip.show(); delay(del); for(int rad = 1; rad < 9; rad++) { offLights(); radius = rad; byte r; byte g; byte b; int Hue = round((float)rad * 360. / 9.) ; HSVtoRGB(r, g, b, Hue, 255, 255); int cube_all[6*radius][3]; cubeRing(center, radius, cube_all); for(int j = 0; j < 6*radius; j++) { int i = hexToIndex(cube_all[j][0], cube_all[j][1], cube_all[j][2]); strip.setPixelColor(i, r, g, b); }//for j strip.show(); delay(del); }//for r offLights(); // //offLights(); // for(int i = 0; i < ledCount; i++) { // strip.setPixelColor(i, 0, 0, 0); // }//for i // radius = 8; // int cube_all[6*radius][3]; // int ring8[] = {8,9,29,30,54,55,83,84,116,117,147,148,174,175,197,198,216,215,214,213,212,211,210,209,208,207,187,186,162,161,133,132,100,99,69,68,42,41,19,18,0,1,2,3,4,5,6,7}; // cubeRing(center, radius, cube_all); // for(int j = 0; j < 6*radius; j++) { // int index = hexToIndex(cube_all[j][0], cube_all[j][1], cube_all[j][2]); // ring8[j] = (int)index; // }//for j // for(int i = 0; i < 6*radius; i++) { // strip.setPixelColor(ring8[i], 255, 255, 255); // }//for i // strip.show(); //rainbowLights(); // //whiteLights(); // //everyNLights(5); delay(100); }//void loop // serial print variable type void types(String a) { Serial.println("it's a String"); } void types(int a) { Serial.println("it's an int"); } void types(char *a) { Serial.println("it's a char*"); } void types(float a) { Serial.println("it's a float"); } void types(bool a) { Serial.println("it's a bool"); } int hexToIndex(int x, int y, int z) { //Not very efficient, but loop over all indices //and return when x, y and z are all correct for(int j = 0; j < ledCount; j++) { //if(x_grid[j] == x) { //and y_grid[j] == y) { //and z_grid[j] == z) { if(x_grid[j] == x and y_grid[j] == y) { //Somehow the strip.SetPixelColor doesn't work if I also add && z_grid[j] == z to this conditional... return j; }//if }//for j //if nothing found return -1; }//hexToIndex //Distance between two index positions int cubeDistance(int a, int b) { return (abs(x_grid[a] - x_grid[b]) + abs(y_grid[a] - y_grid[b]) + abs(z_grid[a] - z_grid[b])) / 2; }//byte cubeDistance void cubeRing(int center[], int radius, int cube_all[][3]) { int cube_direction[3]; hexDirection(4, cube_direction); int cube_scaled[3]; cubeScale(cube_direction, radius, cube_scaled); int cube[3]; cubeAdd(center, cube_scaled, cube); byte counter = 0; for(byte i = 0; i < 6; i++) { for (byte j = 0; j < radius; j++) { cube_all[counter][0] = cube[0]; cube_all[counter][1] = cube[1]; cube_all[counter][2] = cube[2]; cubeNeighbor(cube, i, cube); counter++; }//for j }//for i }//cubeRing void cubeAdd(int a[], int b[], int coord[]) { coord[0] = a[0] + b[0]; coord[1] = a[1] + b[1]; coord[2] = a[2] + b[2]; }//cubeAdd void cubeScale(int a[], int f, int coord[]) { coord[0] = a[0] * f; coord[1] = a[1] * f; coord[2] = a[2] * f; }//cubeScale void cubeNeighbor(int a[], int direction, int coord[]) { int cube_direction[3]; hexDirection(direction, cube_direction); cubeAdd(a, cube_direction, coord); }//cubeNeighbor void hexDirection(int direction, int coord[]) { coord[0] = hex_direction[direction][0]; coord[1] = hex_direction[direction][1]; coord[2] = hex_direction[direction][2]; }//hexDirection ////////////////////// WHITE LIGHTS ///////////////////// // void everyNLights(int num) { // for(int i = 0; i < ledCount; i++) { // if(i % num == 0) strip.setPixelColor(i, 255, 255, 255); // else strip.setPixelColor(i, 0, 0, 0); // }//for i // strip.show(); // }//void everyNLights void offLights() { for(int i = 0; i < ledCount; i++) { strip.setPixelColor(i, 0, 0, 0); }//for i strip.show(); }//void offLights // void whiteLights() { // for(int i = 0; i < ledCount; i++) { // strip.setPixelColor(i, 255, 255, 255); // }//for i // strip.show(); // }//void whiteLights ////////////////////// RAINBOW LIGHTS ///////////////////// void rainbowLights() { byte r; byte g; byte b; for(int i = 0; i < ledCount; i++) { int Hue = round((float)i/(float)ledCount * 360); HSVtoRGB(r, g, b, Hue, 255, 255); strip.setPixelColor(i, r, g, b); }//for i strip.show(); }//void rainbowLights void HSVtoRGB(byte & rgb_r, byte & rgb_g, byte & rgb_b, unsigned int hue, byte sat, byte val) { //Set led by hue, saturation, value unsigned char r,g,b; unsigned int H_accent = hue/60; unsigned int bottom = ((255 - sat) * val)>>8; unsigned int top = val; unsigned char rising = ((top-bottom) *(hue%60 ) ) / 60 + bottom; unsigned char falling = ((top-bottom) *(60-hue%60) ) / 60 + bottom; switch(H_accent) { case 0: r = top; g = rising; b = bottom; break; case 1: r = falling; g = top; b = bottom; break; case 2: r = bottom; g = top; b = rising; break; case 3: r = bottom; g = falling; b = top; break; case 4: r = rising; g = bottom; b = top; break; case 5: r = top; g = bottom; b = falling; break; } rgb_r = (byte)r; rgb_g = (byte)g; rgb_b = (byte)b; }//HSVtoRGB