Coding Help: NeoPixels for a Long Board
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- MaulKill3r
- Posts: 2
- Joined: Sun Mar 18, 2018 1:52 pm
Coding Help: NeoPixels for a Long Board
I'm very new with all of this. My project I'm working on is for my electric long board to ride at night. I have a 60 led neopixel strip and I'm using the buttoncycle sketch cause I like the different patterns I can selected with the push of a button but want i'm trying to figure out is how to make it how to make led 25,26,27,28,29, and 30 stay White and leds 55,56,57,58,59, and 60 stay red while the one in between will switch with the patterns, lease led will make up the head lights and tail light of the long board while the leds will light up under the board. I would like to add a accelerometer to make the tail lights move like a turn signal when you lean into the turns, but I fear that coding is way over my head.
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Coding Help: NeoPixels for a Long Board
Can you post your code? Please use code tags when posting code or logs to the forums. It preserves formatting and makes it easier for everyone to read the code. Click the code button above the reply box and past your code between the tags created. You might consider an if statement that checks the led number and if it is 55 through 60 send white instead of the other color.
- MaulKill3r
- Posts: 2
- Joined: Sun Mar 18, 2018 1:52 pm
Re: Coding Help: NeoPixels for a Long Board
Here is the code I was using
Code: Select all
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be
// driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 60
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH)
{
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW)
{
showType++;
if (showType > 12)
showType=0;
startShow(showType);
}
}
startShow(showType);
// Set the last button state to the old state.
oldState = oldState;
}
void startShow(int i) {
switch (i) {
case 0: colorWipe(strip.Color(0, 0, 0), 2); // Black/off
break;
case 1: colorWipe(strip.Color(255, 0, 0), 2); // Red
break;
case 2: colorWipe(strip.Color(0, 255, 0), 2); // Green
break;
case 3: colorWipe(strip.Color(0, 0, 255), 2); // Blue
break;
case 4: theaterChase(strip.Color(127, 127, 127), 60); // White
break;
case 5: theaterChase(strip.Color(127, 127, 127), 30); // White
break;
case 6: theaterChase(strip.Color(127, 127, 127), 10); // White
break;
case 7: theaterChase(strip.Color(0, 255, 0), 60); // green
break;
case 8: theaterChase(strip.Color(0, 255, 0), 30); // green
break;
case 9: theaterChase(strip.Color(0, 255, 0), 10); // green
break;
case 10: theaterChase(strip.Color(127, 0, 0), 50); // Red
break;
case 11: rainbow(2);
break;
case 12: rainbow(10);
break;
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay (wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<1; j++) { //do 10 cycles of chasing
for (int q=0; q < 45; q++) {
for (int i=1; i < strip.numPixels(); i=i+45) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+45) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
}
strip.show();
delay;
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
// 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) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Coding Help: NeoPixels for a Long Board
This is not code but it should give you the idea.
Code: Select all
for (uint16_t i = 0; i < strip.numPixels(); i++) {
if(i > 54 && i < 61) strip setPixelColor(i, <whateverwhiteis>);
else
strip.setPixelColor(i, c);
Please be positive and constructive with your questions and comments.