91Views2Replies

Author Options:

Arduino code help Answered

Hey guys, So Im by no means good at coding. I can work with electronics, but the coding part of arduino is beyond me. I normally work with libraries that others have already created. 
Im just wondering, is there anyway someone could help me write an arduino sketch for a personal project?

Im using a arduino nano and a strip of 22 ws2812b led strips seen in my last instructable; https://www.instructables.com/id/DIY-Bike-LED-Lights/

I worked around the neopixal library until I got the rainbow cycle effect seen here (Not my video) https://www.youtube.com/watch?v=qUL7x_0r-bA

Im just wondering if its possible to make the rainbow cycle code run between 3 different colors such as red green and yellow or red white and blue.

here is my current code
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PUT THE NUMBER OF LED's HERE, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {


  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  // Some example procedures showing how to display to the pixels:
}

void loop() {


  rainbowCycle(20);
}


// 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);
  }
}



// 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);
}

Please let me know or if you can help ill take any advice Thanks!

Discussions

0
None
mach1950

3 years ago

...sorry, Blue is 0,0,255.... so, in your code, Green has only two possibilities, 0 and 255, off or on, nothing in between. 255 for white and 0 for Red or Blue. The code you have for rainbow effects won't do what you want, and in fact is probably far more complex than you need.

0
None
mach1950

3 years ago

Change the maximum and minimum colour settings to give you the output you want. Red is 255,0,0 white is 255,255,255 Blue is 0, 255 ,0. So instead of cycling through all combinations you must code in variations on these settings only.