Array programming issue, APA102 (Dotstar) LED strip and Mega 2560

Im not sure if this should be posted here or in LED forum, apologize in advance if im in the wrong spot.

I am duplicating a project found here:

at 23:00 he starts explaining the coding I am having trouble with.

Here are the schematics, breadboard view, etc.:

The only thing I have done different is I am using the APA102 (Dotstar) Leds on a 300 pc strip.

All is working fine except I am trying to achieve the effect of the signal propagating from the middle of the strip. It does start the propagation in the middle however I can only have one half of the strip work at a time, the other side is solid red. I can flip it so it goes one way or the other but not in both directions at once. I can also have it propagate in one direction thru the whole strip. So hardware/connections seem fine, just a programming problem.

The program file is attached. The problem area seems to be lines 62-87.

I really appreciate any help provided! Thank you!

lights_take2.ino (2.94 KB)

I can flip it so it goes one way or the other but not in both directions at once.

Post your code for how you were able to switch directions please

By the way, you havent set your analog pins to inputs inside the setup functions

you havent set your analog pins to inputs inside the setup functions

Not strictly necessary as they default to inputs, but I would do it as a matter of course anyway.

    //set pixel color
    
    strip.setPixelColor(i, left_array[i]);   -------------//If I reverse these
    strip.setPixelColor(length-i, right_array[i]);------// two lines where length-i is left or right channel     will reverse it.

RE: the analog inputs, I have that here:

//Analog Input Pins
int left_channel = 0;
int right_channel = 1;

on lines 26 & 27 correct?

//Analog Input Pins
int left_channel = 0;
int right_channel = 1;

Those 2 lines give names to 2 pins but do not set the pinMode() to input.

Personally I would use

//Analog Input Pins
const byte left_channel = A0;
const byte right_channel = A1;

const because they are not going to change within the program
byte because a byte is plenty big enough for the values and using int wastes RAM
A0 & A1 because it makes it more obvious that the program uses analogue pins

Ok, a weird update. I disconnected the A1 input from the breadboard and it immediately started working. Although I cant control the right channel potentiameter anymore.

The effect is what I was looking for but it doesn't seem right...

strip.setPixelColor(length-i, right_array[i]);

This won't help, but this should be fixed. When i is 0, "length - i" is equal to 300, but the range for indexing the LEDs is 0-299, so it should be "length - i - 1".

Also, what happens when you connect A1 to ground, and what happens when you connect it to 5V?

you are right about the i-1.

when A1 goes to gnd or 5v it causes half of the strip to go solid red.

By the way, while I have your attention.

Below is a different program to have the entire strip blink in unison to the beat with color rotation.

The program is actually very similar I think there is just one loop that does this rather than have it propagate from the middle.

It would be great if I could have the strip alternate between this effect and the one I first mentioned above. Any idea how I might do that?

Thanks again for your help.

Btw - im using 240 pixels right now as opposed to the 300.

//Jeremy Blum's Arduino Tutorial Series - Episode 14 - Lights and Sound Holiday Special
//Sample Code 1 - Blinking an RGB LED chain to mono music
//http://www.jeremyblum.com
//WS2801 Library and Helper Functions by Adafruit http://www.adafruit.com

/* 
  The circuit:
 * A0 from Amplififed Left Channel
 * A1 from Amplified Right Channel
 * Digital 2 to Light Clock Line
 * Digital 3 to Light Data Line
 * Don't forget to connect all your grounds!
 */

//SPI Library
#include "SPI.h"

//RGB LED Library
//You can download it here: https://github.com/adafruit/Adafruit-WS2801-Library/archive/master.zip
//Install it in your library folder and restart the Arduino IDE
#include <Adafruit_DotStar.h>

//Analog Input Pins
int left_channel = A0;
int right_channel = A1;

//Light Control Pins
int light_data = 3;
int light_clk = 2;

//Set Strip Constants
const int length = 240;



//Library Setup
Adafruit_DotStar strip = Adafruit_DotStar(length, light_data, light_clk);

void setup()
{
  //Initialize Serial Connection (for debugging)
  Serial.begin(9600);
 
  //Initialize Strip
  strip.begin();
  strip.show();
  
  
}

void loop()
{
  
  //Print out some Debug Info
  Serial.print("L: ");
  Serial.print(analogRead(left_channel));
  Serial.print("     R: ");
  Serial.println(analogRead(right_channel));



  //Set the hue (0-255) depending on left channel value
 byte hue = constrain(map(analogRead(left_channel), 0, 400, 0, 255), 0, 255);
  
  //convert the hue to a 24-bit color
  uint32_t color = Wheel(hue);
  
  //Go through each Pixel on the strip and set its color
  for (int i=0; i<strip.numPixels(); i++)
  {
    //set pixel color
    strip.setPixelColor(i, color);
  }


  //Display the new values
  strip.show();
  
  //sample delay
  delay(100);

}

/* Helper functions */
//http://www.ladyada.net/products/rgbledpixel/

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

//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 Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170; 
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Just create different states.

byte state = 1;
void loop(){
if(state == 1)
  runFunction1();
else if(state == 2)
  runFunction2();
}

void runFunction1(){
  boolean quit = false;
  while(quit == false){
    //...do stuff
  }
}

void runFunction2(){
  boolean quit = false;
  while(quit == false){
    //...do other stuff
  }
}

Also, you are using a lot of RAM in your project.
you have 2 arrays of 150 elements and each element is 4 bytes: 2 arrays * 150 elements * 4 bytes = 1200 bytes.
The library itself allocates 3 bytes per pixel and you have 300 of them, you are using 300 elements * 3 bytes = 900 bytes
1200 + 900 + the arduino library and LED library and the few variables you are using is over the amount of ram you can use on most arduinos. If your board is using an ATmega328p, then you only have 2048 bytes, you have passed this limit.