Skip to content

Instantly share code, notes, and snippets.

@jbobrow
Created October 31, 2017 00:22
Show Gist options
  • Save jbobrow/35b67e862dc8a03f55865fae3021e210 to your computer and use it in GitHub Desktop.
Save jbobrow/35b67e862dc8a03f55865fae3021e210 to your computer and use it in GitHub Desktop.
/*
* Color By Number/Neigbor
*
* An example showing how to use the blinkstate library.
*
* Each Blink broadcasts 1 to its neighbors, letting its neigbors know it's present.
* Each Blink displays a color based on the number of neighbors around it.
*
*/
#include "blinklib.h"
#include "blinkstate.h"
// color by number of neighbors
Color colors[] = {dim(WHITE,5), // 0 neighbors
RED, // 1 neighbors
YELLOW, // 2 neighbors
GREEN, // 3 neighbors
CYAN, // 4 neighbors
BLUE, // 5 neighbors
MAGENTA }; // 6 neighbors
void setup() {
// put your setup code here, to run once:
for(int i=0; i<6; i++) {
// simple startup sequence with a ring of magenta
setFaceColor( i , MAGENTA );
delay(100);
setFaceColor( i , OFF );
}
blinkStateBegin();
setState(1);
}
void loop() {
// count neighbors
int numNeighbors = 0;
for(int i=0; i<6; i++) {
if(getNeighborState(i) == 1) {
numNeighbors++;
}
}
// show color based on number of neighbors
setColor( colors[ numNeighbors ] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment