Created
January 1, 2014 04:58
-
-
Save brainrake/8205247 to your computer and use it in GitHub Desktop.
led strand control with wii nunchuck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include "WiiChuck.h" | |
#include <Adafruit_NeoPixel.h> | |
#define PIN 0 | |
//#define N 232 | |
#define N 62 | |
// Parameter 1 = number of pixels in strip | |
// Parameter 2 = pin number (most are valid) | |
// Parameter 3 = pixel type flags, add together as needed: | |
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | |
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | |
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | |
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N, PIN, NEO_GRB + NEO_KHZ800); | |
WiiChuck chuck = WiiChuck(); | |
void setup() { | |
delay(3000); | |
Serial.begin(115200); | |
Serial.print("serial\n"); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
Serial.print("strip\n"); | |
chuck.begin(); | |
chuck.update(); | |
Serial.print("chuck\n"); | |
} | |
uint32_t L = 5; | |
#define IDLE 0 | |
#define XPOS 1 | |
uint32_t state = IDLE; | |
uint32_t idle = 0; | |
uint32_t fire = 0; | |
uint32_t i = 0; | |
uint32_t T = 0; | |
uint32_t d = 0; | |
boolean up = true; | |
void loop() { | |
T++; | |
chuck.update(); | |
Serial.print(chuck.readJoyX()); | |
Serial.print(", "); | |
Serial.print(chuck.readJoyY()); | |
Serial.print(", "); | |
if (chuck.buttonZ) | |
Serial.print("Z"); | |
else | |
Serial.print("-"); | |
Serial.print(", "); | |
if (chuck.buttonC) | |
Serial.print("C"); | |
else | |
Serial.print("-"); | |
Serial.println(); | |
for (uint32_t j=0; j<N; j++) { strip.setPixelColor(j, strip.Color(0,0,0)); } | |
if (state == IDLE) { | |
uint16_t p = map(chuck.readJoyX(), -128, 128, 0, N-L); | |
if (T % 4) { | |
d++; | |
} | |
for (uint32_t j=0; j<L; j++) { | |
strip.setPixelColor(j+i, Wheel(((j+i)*255/N + chuck.readJoyY() + d)*2 & 255)); | |
if (j+i > N) { | |
strip.setPixelColor(j+i-N-1, Wheel(((j+i)*255/N + chuck.readJoyY() + d)*2 & 255)); | |
} | |
} | |
if ((abs(chuck.readJoyX()) > 50)) { | |
state = XPOS; | |
} | |
if (T) { | |
//if (up) { i++; } else {i--;} | |
//if (i == N+L+2) {up = false;} | |
//if (i == 0-L-2) {up = true;} | |
i++; | |
if(i >= N) {i = 0;} | |
} | |
} else if (state == XPOS) { | |
uint16_t p = map(chuck.readJoyX(), -127, 128, 0, N-L); | |
L = map(chuck.readJoyY(), -128, 128, 1, 30); | |
if (abs(i - p) == 1) {i = p;} | |
i = (i*3 + p) / 4; | |
for (uint32_t j=0; j<L; j++) { | |
strip.setPixelColor(j+i, Wheel(((j+i)*255/N + chuck.readJoyY() + T)*2 & 255)); | |
} | |
if ((abs(chuck.readJoyX()) < 50)) { | |
idle += 1; | |
} else { | |
idle = 0; | |
} | |
if (idle > 200) { | |
idle = 0; | |
state = IDLE; | |
} | |
} | |
if (fire) { | |
strip.setPixelColor(fire-3, strip.Color(0,0,0)); | |
strip.setPixelColor(fire-2, strip.Color(128,128,128)); | |
strip.setPixelColor(fire-1, strip.Color(255,255,255)); | |
strip.setPixelColor(fire, strip.Color(255,255,255)); | |
strip.setPixelColor(fire+2, strip.Color(128,128,128)); | |
strip.setPixelColor(fire+3, strip.Color(0,0,0)); | |
fire += 4; | |
if (fire > N+4) {fire = 0;} | |
} else { | |
if (chuck.buttonZ) {fire = 1;} | |
} | |
strip.show(); | |
delay(5); | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
if(WheelPos < 85) { | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} else if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} else { | |
WheelPos -= 170; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment