Go Down

Topic: trouble identifying error w/ pololu SK9822 Addressable LED's and A-Star 32U4 (Read 141 times) previous topic - next topic

tinygreentrees

having trouble identifying the problem- no errors in code but LED strip is unresponsive/not light up
its not the cleanest most succinct code- its my first Arduino project!
adapted from the Xmas sample sketch
Thanks in advance for your patience!



Code: [Select]
#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
#include <APA102.h>
#include <EEPROM.h>
const uint8_t dataPin = 10; //can i have multiple strands doing diff code on same arduino? ||?
const uint8_t clockPin = 12;
APA102<dataPin, clockPin> ledStrip;

//#define NEXT_PATTERN_BUTTON_PIN  2  // button between this pin and ground
//#define AUTOCYCLE_SWITCH_PIN  3  // switch between this pin and ground
#define LED_COUNT 31
rgb_color colors[LED_COUNT];
const uint8_t globalBrightness = 1;
#define NUM_STATES  3 //number of patterns
unsigned int loopCount = 0;
unsigned int seed = 0;

enum Pattern
{ WarmWhiteShimmer = 0,
  AllOn = 1,
  FadeOut = 2,
  AllOff = 255};
  
unsigned char pattern = AllOff;
unsigned int maxLoops;  // go to next state when loopCount >= maxLoops

void setup()
{
  for (int i = 0; i < 8; i++)/// i dont know why 8
  seed += analogRead(i);
  seed += EEPROM.read(0);
  EEPROM.write(0, random(249));

  // optionally connect a switch between this pin and ground
  // when the input is low, freeze the cycle at the current patter
  //pinMode(AUTOCYCLE_SWITCH_PIN, INPUT_PULLUP);
  // optionally connect a button between this pin and ground
  // when the input goes low, advance to the next pattern in cycle
  //pinMode(NEXT_PATTERN_BUTTON_PIN, INPUT_PULLUP);
  //delay(10);  // give pull-ups time raise the input voltage
}

void loop()
{
  uint8_t startTime = millis();
  //  handleNextPatternButton();
  if (loopCount == 0)
         {
         // whenever timer resets, clear the LED colors array (all off) do i need this??
         for (int i = 0; i < LED_COUNT; i++)
          {colors[i] = rgb_color(0, 0, 0);}
         }
  {if (loopCount % 6 == 0)
    {seed = random(30000);}
    randomSeed(seed);}

  // call the appropriate pattern routine based on state; these
  // routines just set the colors in the colors array
  switch (pattern)
  {
    case WarmWhiteShimmer:  // warm white shimmer for 300 loopCounts, fading over last 70
      maxLoops = 300;
      (loopCount < maxLoops - 70);
      break;

    case AllOn:
      maxLoops = 600;
      break;

    case FadeOut:
      maxLoops = 300;
      (loopCount > maxLoops - 250);
      break;
  }

}

//void handleNextPatternButton()
//{
//  if (digitalRead(NEXT_PATTERN_BUTTON_PIN) == 0)
//  {
// if optional button is pressed
//   while (digitalRead(NEXT_PATTERN_BUTTON_PIN) == 0)
//   {
// wait for button to be released
//     while (digitalRead(NEXT_PATTERN_BUTTON_PIN) == 0);
//   delay(10);  // debounce the button
//   }
//  loopCount = 0;  // reset timer
//  pattern = ((unsigned char)(pattern+1))%NUM_STATES;  // advance to next pattern
// }
//}



// This function applies a random walk to val by increasing or
// decreasing it by changeAmount or by leaving it unchanged.
// val is a pointer to the byte to be randomly changed.
// The new value of val will always be within [0, maxVal].
// A walk direction of 0 decreases val and a walk direction of 1
// increases val.  The directions argument specifies the number of
// possible walk directions to choose from, so when directions is 1, val
// will always decrease; when directions is 2, val will have a 50% chance
// of increasing and a 50% chance of decreasing; when directions is 3,
// val has an equal chance of increasing, decreasing, or staying the same.


void randomWalk(unsigned char *val, unsigned char maxVal, unsigned char changeAmount, unsigned char directions)
{ unsigned char walk = random(directions);  // direction of random walk
  if (walk == 0)
  {
    // decrease val by changeAmount down to a min of 0
    if (*val >= changeAmount)
    {
      *val -= changeAmount;
    }
    else
    {
      *val = 0;
    }
  }
  else if (walk == 1)  {
    // increase val by changeAmount up to a max of maxVal
    if (*val <= maxVal - changeAmount)
    {
      *val += changeAmount;
    }
    else
    {
      *val = maxVal;
    }
  }
}

// This function fades val by decreasing it by an amount proportional
// to its current value.  The fadeTime argument determines the
// how quickly the value fades.  The new value of val will be:
//   val = val - val*2^(-fadeTime)
// So a smaller fadeTime value leads to a quicker fade.
// If val is greater than zero, val will always be decreased by
// at least 1.
// val is a pointer to the byte to be faded.


void fade(unsigned char *val, unsigned char fadeTime)
{
  if (*val != 0)
  {
  unsigned char subAmt = *val >> fadeTime;  // val * 2^-fadeTime

  if (subAmt < 1)
     subAmt = 1; // make sure we always decrease by at least 1
    *val -= subAmt; // decrease value of byte pointed to by val
  }
}

// ***** PATTERN WarmWhiteShimmer *****
// This function randomly increases or decreases the brightness of the
// even red LEDs by changeAmount, capped at maxBrightness.  The green
// and blue LED values are set proportional to the red value so that
// the LED color is warm white.  Each odd LED is set to a quarter the
// brightness of the preceding even LEDs.  The dimOnly argument
// disables the random increase option when it is true, causing
// all the LEDs to get dimmer by changeAmount; this can be used for a
// fade-out effect.
void warmWhiteShimmer(unsigned char dimOnly)
{
  const unsigned char maxBrightness = 31;  // cap on LED brighness
  const unsigned char changeAmount = 2;   // size of random walk step

  for (int i = 0; i < LED_COUNT; i += 2)
  {
    // randomly walk the brightness of every even LED
    randomWalk(&colors[i].red, maxBrightness, changeAmount, dimOnly ? 1 : 2);
  
 // warm white: red = x, green = 0.8x, blue = 0.125x
    colors[i].green = colors[i].red * 4 / 5; // green = 80% of red
    colors[i].blue = colors[i].red >> 3;  // blue = red/8

    // every odd LED gets set to a quarter the brighness of the preceding even LED
    if (i + 1 < LED_COUNT)
    {
      colors[i + 1] = rgb_color(colors[i].red >> 2, colors[i].green >> 2, colors[i].blue >> 2);
    }
  }
}



Im trying to make a wig light up like this  RAPUNZEL TANGLED


sterretje

Nowhere in your code is anything that does something with ledStrip; so I suspect that you have stripped too much from the original code; do you have a link for that original code?

I see you have some comments in your code asking for info.

1)
I'm not familiar with APA102, so don't know if you can have multiple strips on different pins.

2)
Code: [Select]
  for (int i = 0; i < 8; i++)/// i dont know why 8

Your for-loop reads the 8 analog inputs of the 32U4. If nothing is connected to those pins, they float and give a bit of a variable number. That number is usually used to seed the random number generator but that part seems to have fallen away when you modified the code.

3)
Code: [Select]
    // whenever timer resets, clear the LED colors array (all off) do i need this??
    for (int i = 0; i < LED_COUNT; i++)

Probably yes; you can try with and without once you have code that at least can light up some leds.

PS
code tags and clickable links in your first post; if I could give you two karmas, I would. You will have to do with just one, sorry.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

tinygreentrees

Thank you for the response!

heres the code i've mangled/ adapted: https://github.com/pololu/pololu-led-strip-arduino/blob/master/examples/LedStripXmas/LedStripXmas.ino

added the pololu Led strip line and library, no change. Ill keep fiddling.
 
THANKS

sterretje

Compare the original
Code: [Select]
    case WarmWhiteShimmer:
      // warm white shimmer for 300 loopCounts, fading over last 70
      maxLoops = 300;
      warmWhiteShimmer(loopCount > maxLoops - 70);
      break;

with your
Code: [Select]
    case WarmWhiteShimmer:  // warm white shimmer for 300 loopCounts, fading over last 70
      maxLoops = 300;
      (loopCount < maxLoops - 70);
      break;

You're missing the call to warmWhiteShimmer.

Do APA102 examples (if there are any) work?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up