0

Help with basic rainbow cycle
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help with basic rainbow cycle

by jaxdesign on Sun Jul 16, 2017 5:18 pm

Hello! This is my third project, after creating a led coat and the florabrella. This time I'm making a simple hat similar to this one: https://learn.adafruit.com/guggenhat-bl ... at-display

However, my hat doesn't need to spell anything. I have one 4m / 30led strand wrapping around the hat. All I want to do is have the individual LEDs fade randomly in color and fade in and out in brightness from 0 to 200. There should be 120 leds total.

Unfortunately I'm really bad at the coding aspect of this project. I tried using snippets of code found on this tutorial, but I am getting errors when compiling.

https://learn.adafruit.com/multi-taskin ... ommon-bits

Here is my code, which I copied and pasted from the above tutorial, and I'm sure is incomplete. It won't compile, so I included the error message as well:

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

// Pattern types supported:
enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
// Patern directions supported:
enum  direction { FORWARD, REVERSE };

// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
    public:

    // Member Variables: 
    pattern  ActivePattern;  // which pattern is running
    direction Direction;     // direction to run the pattern
   
    unsigned long Interval;   // milliseconds between updates
    unsigned long lastUpdate; // last update of position
   
    uint32_t Color1, Color2;  // What colors are in use
    uint16_t TotalSteps;  // total number of steps in the pattern
    uint16_t Index;  // current step within the pattern
   
    void (*OnComplete)();  // Callback on completion of pattern

   
    // Initialize for a RainbowCycle
    void RainbowCycle(uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = RAINBOW_CYCLE;
        Interval = interval;
        TotalSteps = 120;
        Index = 0;
        Direction = dir;
    }

    // Update the Rainbow Cycle Pattern
    void RainbowCycleUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
        }
        show();
        Increment();
    }
}



Error:

Code: Select all | TOGGLE FULL SIZE
Arduino: 1.8.0 (Mac OS X), Board: "Adafruit Flora"

hat:47: error: expected ';' after class definition
 }
 ^
/Users/jacksonwalker/Documents/Arduino/hat/hat.ino: In member function 'void NeoPatterns::RainbowCycleUpdate()':
hat:42: error: 'Wheel' was not declared in this scope
             setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
                                                                           ^
hat:45: error: 'Increment' was not declared in this scope
         Increment();
                   ^
exit status 1
expected ';' after class definition

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Thanks for the help, you guys are the best.

jaxdesign
 
Posts: 27
Joined: Tue Sep 20, 2016 10:29 pm

Re: Help with basic rainbow cycle

by kcl1s on Sun Jul 16, 2017 6:38 pm

You have several errors in that code. One of the big ones is it uses the 'wheel' function which you have not included.
Here is the rainbow cycle code from strandtest. It will give you a starting point.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUMPIXELS 120
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  pixels.begin();
  pixels.show(); // Initialize all pixels to 'off'
}

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 < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
    }
    pixels.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 pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


Keith

kcl1s
 
Posts: 346
Joined: Tue Aug 30, 2016 12:06 pm

Re: Help with basic rainbow cycle

by jaxdesign on Tue Jul 18, 2017 3:10 pm

Thank you so much!

jaxdesign
 
Posts: 27
Joined: Tue Sep 20, 2016 10:29 pm

Re: Help with basic rainbow cycle

by jaxdesign on Thu Jul 20, 2017 9:37 pm

Sorry to bother you again, would you mind looking at my code? I altered RainbowCycle to randomly have white lights appear. But there's an error I cant seem to figure out when I try to compile:

The error: https://www.screencast.com/t/UbIXlIeBO

Seems like wheel isnt defined on the line, but I see it below. I tried moving the rainbowcycle function below the wheel function, but that didn't work.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUMPIXELS 120
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  pixels.begin();
  pixels.show(); // Initialize all pixels to 'off'
}

void loop() {

  rainbowCycle(20);
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
  long randNumber;


  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < pixels.numPixels(); i++) {
      // get a random number from 0 to 99
      randNumber = random(100);
      // pick how often you want random white pixels.  i picked 10, meaning pixels will be white approx 10% of the time.
      if(randNumber < 10){
        // set pixel color to pure white.  you might wanna lower these values if pure white is too intense.
        pixels.setPixelColor(i, pixels.Color(255,255,255));
      }else{
        // normal code to continue rendering the rainbow
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
    }
    pixels.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 pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}



Thank you!
Last edited by jaxdesign on Sat Jul 22, 2017 6:11 pm, edited 1 time in total.

jaxdesign
 
Posts: 27
Joined: Tue Sep 20, 2016 10:29 pm

Re: Help with basic rainbow cycle

by kcl1s on Fri Jul 21, 2017 8:30 am

First please use the code button above the text editor to create code tags and place code between them. It makes reading the code so much easier.

OK your code was missing 3 closing curly brackets. Each open bracket '{' needs a corresponding close bracket '}'. If you click tools/Auto format it will indent your code and help you see where you need them.
Here is the fixed code it compiles but I did not test it.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUMPIXELS 120
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  pixels.begin();
  pixels.show(); // Initialize all pixels to 'off'
}

void loop() {

  rainbowCycle(20);
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
  long randNumber;


  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < pixels.numPixels(); i++) {
      // get a random number from 0 to 99
      randNumber = random(100);
      // pick how often you want random white pixels. i picked 10, meaning pixels will be white approx 10% of the time.
      if (randNumber < 10) {
        // set pixel color to pure white. you might wanna lower these values if pure white is too intense.
        pixels.setPixelColor(i, pixels.Color(255, 255, 255));
      }
      else {
        // normal code to continue rendering the rainbow
        for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
          for (i = 0; i < pixels.numPixels(); i++) {
            pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
          }
          pixels.show();
          delay(wait);
        }
      }
    }     //3 missing brackets
  }
}
// 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 pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


Keith

kcl1s
 
Posts: 346
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.