0

BT HC-05 works, Arduino Uno works, but Neopixel strip wont l
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BT HC-05 works, Arduino Uno works, but Neopixel strip wont l

by madbrow on Tue Oct 10, 2017 1:43 pm

Hi again, I'm trying to modify this code to work for one strip (originally written for 5 but I want to get one working first just to test, then i'll add more later).
I've tried several other sketches and my RGB Neopixel strips light up fine, so i am convinced that the code (not the wiring) is the reason for my lights not lighting up. I'm definitely not an expert, so any suggestions for it? Also there is still space left, so I don't believe the size of the sketch is the problem.

I know that's probably a lot of code to go through, but I'm just not sure where the error may be.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>  // Arduino Neopixel library (used for timing LED commands)

// Pin Declarations
#define PIN1 2


// Static Variables
const uint8_t LEDs = 30;  // number of LEDs per strip
const uint8_t strip1 = 1;  // number of strips
const int colors = 1530;  // total number of colors in standard colorwheel (255 bits * 3 primary colors * 2 interactions per color)

// Dynamic Variables
String input;  // character read by serial comm
String sb;  // speed and brightness changes [+/- for faster/slower, b/d for brighter/darker]
String dir = "h";  // display direction [h/v for horizontal/vertical]
String mode;  // operation command, latched from input readings

// Mode Specific Variables
const uint8_t hot_range = ceil(255*.75);  // upper limit of sunset range
uint8_t cw_dir[strip1][LEDs];  // sunset color direction
int acc[strip1];  // aurora current color
int atc[strip1];  // aurora target color
uint8_t acb[strip1];  // aurora current brightness
uint8_t atb[strip1];  // aurora target brightness
uint8_t lp;  // lightning position
uint8_t ls;  // lightning size
uint8_t lb;  // lightning brightness

// Iteration Variables
uint8_t reset;  // when changing modes, used to initialize new mode
uint8_t i;  // counter
uint8_t j;  // counter

// LED Controls
uint8_t cw_val[3];  // temporary storage for a single set of RGB values
uint8_t cw_speed = 5;  // color change speed
float bright = 0.5;  // master brightness, initially set to 50%
int cw_pos[strip1][LEDs];  // current position within colorwheel
uint8_t R[strip1][LEDs];  // Red value for each LED within each strip
uint8_t G[strip1][LEDs];  // Green value for each LED within each strip
uint8_t B[strip1][LEDs];  // Blue value for each LED within each strip

// Strip Initializations
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDs, PIN1, NEO_GRB + NEO_KHZ800);


// Colorwheel reference (takes a 3-variable array and a position within the colorwheel, updates the array with RGB values for that position)
void cw(uint8_t c_temp[], int pos_temp){
    if (pos_temp <= 255){
      c_temp[0] = 255;
      c_temp[1] = pos_temp;
      c_temp[2] = 0;
    }
    else if (pos_temp <= 255*2){
      c_temp[0] = 255*2 - pos_temp;
      c_temp[1] = 255;
      c_temp[2] = 0;
    }
    else if (pos_temp <= 255*3){
      c_temp[0] = 0;
      c_temp[1] = 255;
      c_temp[2] = pos_temp - 255*2;
    }
    else if (pos_temp <= 255*4){
      c_temp[0] = 0;
      c_temp[1] = 255*4 - pos_temp;
      c_temp[2] = 255;
    }
    else if (pos_temp <= 255*5){
      c_temp[0] = pos_temp - 255*4;
      c_temp[1] = 0;
      c_temp[2] = 255;
    }
    else if (pos_temp <= 255*6){
      c_temp[0] = 255;
      c_temp[1] = 0;
      c_temp[2] = 255*6 - pos_temp;
    }
}

void setup() {
 
  // Initialize LED strips
  strip.begin();
  strip.show();
 

 
  // Initialize serial communication, wait until ready
  Serial.begin(115200);
  while(!Serial);
  Serial.println("ready");
}

void loop(){
 
  // Read User Inputs
  if (Serial.available()){
    input += char(Serial.read());
    delay(1);
  }
  else if (!Serial.available() && input != ""){ // once user is finished inputting
    if (input == "+" || input == "-" || input == "b" || input == "d"){  // brightness or speed change
      sb = input;
    }
    else if (input == "h" || input == "v"){  // direction change
      dir = input;
      reset = 1;
    }
    else if (input == "o" || input == "w" || input == "r" || input == "s" || input == "a" || input == "l"){  // mode change
      mode = input;
      reset = 1;
    }
    input = "";
  }
 
  // Slower
  if (sb == "-"){
    sb = "";
    if (cw_speed > 0){
      cw_speed--;
    }
    Serial.println(String(cw_speed));
  }
  // Faster
  else if (sb == "+"){
    sb = "";
    if (cw_speed < colors){
      cw_speed++;
    }
    Serial.println(String(cw_speed));
  }
  // Brighter
  else if (sb == "b"){
    sb = "";
    if (bright < 1){
      bright = bright + 0.1;
    }
    Serial.println(String(int(bright*10)));
  }
  // Darker
  else if (sb == "d"){
    sb = "";
    if (bright > 0){
      bright = bright - 0.1;
    }
    Serial.println(String(int(bright*10)));
  }
 
  // Off Mode
  if (mode == "o"){
    if (reset){
      reset = 0;
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
          R[i][j] = 0;
          G[i][j] = 0;
          B[i][j] = 0;
        }
      }
    }
  }
 
  // White Light Mode
  if (mode == "w"){
    if (reset){
      reset = 0;
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
          R[i][j] = ceil(122*bright);
          G[i][j] = ceil(122*bright);
          B[i][j] = ceil(255*bright);
        }
      }
    }
  }
 
  // Rainbow Mode
  else if (mode == "r"){
    if (reset){
      reset = 0;
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
          if (dir == "h"){
            cw_pos[i][j] = i*100;
          }
          else if (dir == "v"){
            cw_pos[i][j] = j*10;
          }
          cw(cw_val,cw_pos[i][j]);
          R[i][j] = ceil(cw_val[0]*bright);
          G[i][j] = ceil(cw_val[1]*bright);
          B[i][j] = ceil(cw_val[2]*bright);
        }
      }
    }
    else{
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
          if (cw_pos[i][j] + cw_speed >= colors){
            cw_pos[i][j] = 0;
          }
          else{
            cw_pos[i][j] = cw_pos[i][j] + cw_speed;
          }
          cw(cw_val,cw_pos[i][j]);
          R[i][j] = ceil(cw_val[0]*bright);
          G[i][j] = ceil(cw_val[1]*bright);
          B[i][j] = ceil(cw_val[2]*bright);
        }
      }
    }
  }
 
  // Sunset Mode
  else if (mode == "s"){
    if (reset){
      reset = 0;
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
         
          if (dir == "h") cw_pos[i][j] = i*hot_range/strip1;
          else if (dir == "v") cw_pos[i][j] = j*hot_range/LEDs;
         
          cw_dir[i][j] = 1;
          cw(cw_val,cw_pos[i][j]);
          R[i][j] = ceil(cw_val[0]*bright);
          G[i][j] = ceil(cw_val[1]*bright);
          B[i][j] = ceil(cw_val[2]*bright);
        }
      }
    }
    else{
      for (i = 0; i < strip1; i++){
        for (j = 0; j < LEDs; j++){
         
          if (cw_pos[i][j] - cw_speed < 0 && !cw_dir[i][j]) cw_dir[i][j] = 1;
          else if (cw_pos[i][j] + cw_speed > hot_range && cw_dir[i][j]) cw_dir[i][j] = 0;
          else if (cw_dir[i][j]) cw_pos[i][j] = cw_pos[i][j] + cw_speed;
          else cw_pos[i][j] = cw_pos[i][j] - cw_speed;
         
          cw(cw_val,cw_pos[i][j]);
          R[i][j] = ceil(cw_val[0]*bright);
          G[i][j] = ceil(cw_val[1]*bright);
          B[i][j] = ceil(cw_val[2]*bright);
        }
      }
    }
  }
 
  // Aurora Borealis Mode
  else if (mode == "a"){
    delay(ceil(100/cw_speed));
    if (reset){
      reset = 0;
      for (i = 0; i < strip1; i++){
        acc[i] = random(290+248*i,290+248*(i+1));
        atc[i] = acc[i];
        acb[i] = random(1,101);
        atb[i] = acb[i];
        cw(cw_val,acc[i]);
        for (j = 0; j < LEDs; j++){
          R[i][j] = ceil(cw_val[0]*acb[i]/100*bright);
          G[i][j] = ceil(cw_val[1]*acb[i]/100*bright);
          B[i][j] = ceil(cw_val[2]*acb[i]/100*bright);
        }
      }
    }
    else{
      for (i = 0; i < strip1; i++){
        if (acc[i] == atc[i]) atc[i] = random(290+248*i,290+248*(i+1));
        else if (acc[i] < atc[i]) acc[i] = acc[i] + 1;
        else acc[i] = acc[i] - 1;
       
        if (atb[i] == acb[i]) atb[i] = random(1,101);
        else if (acb[i] < atb[i]) acb[i]++;
        else acb[i]--;
       
        cw(cw_val,acc[i]);
        for (j = 0; j < LEDs; j++){
          R[i][j] = ceil(cw_val[0]*acb[i]/100*bright);
          G[i][j] = ceil(cw_val[1]*acb[i]/100*bright);
          B[i][j] = ceil(cw_val[2]*acb[i]/100*bright);
        }
      }
    }
  }
 
  // Lightning Mode
  else if (mode == "l"){
    for (i = 0; i < strip1; i++){
      lp = random(LEDs);
      ls = floor(random(LEDs/2));
      lb = random(256);
      for (j = 0; j < LEDs; j++){
        if (B[i][j] == 0 && ls > 5 && j > lp - floor(ls/2) && j < lp + floor(ls/2)){
          R[i][j] = 0;
          G[i][j] = 0;
          B[i][j] = ceil(lb*bright);
        }
        else{
          R[i][j] = 0;
          G[i][j] = 0;
          B[i][j] = max(B[i][j]-cw_speed,0);
        }
      }
    }
  }
 
  // LED updates
  for (i = 0; i < strip1; i++) {
    for (j = 0; j < LEDs; j++){
      if (i == 0){
        strip.setPixelColor(j,R[i][j], G[i][j], B[i][j]);
      }
      else if (i == 1){
       
      }
      else if (i == 2){
     
      }
      else if (i == 3){
     
      }
      else if (i == 4){
     
      }
    }
    strip.show();
   
   
   
   
  }
}





Image

madbrow
 
Posts: 4
Joined: Sun Oct 08, 2017 5:43 pm

Re: BT HC-05 works, Arduino Uno works, but Neopixel strip wo

by kcl1s on Tue Oct 10, 2017 2:26 pm

You are right lots of code. I assume you changed the baud rate on the HC-05 from the default 9600 to 115200 since that is what you are using. Are you getting feedback from your serial.prints? I did not see a serial.print for mode. That may help to confirm you are getting good data from your BT. I have done projects with the HC-05 but they can be troublesome keeping the connection. I would make sure the other end of the BT connection lets you know your connection status. The HC-05 will blink at about 1/2 second intervals when not connected.

I am just a hobbyist and pointing out things I would check. Take it for what its worth.

Keith

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

Re: BT HC-05 works, Arduino Uno works, but Neopixel strip wo

by madbrow on Wed Oct 11, 2017 3:01 pm

Thanks for the suggestions, I’ll have to check out the serial monitor a bit more.

madbrow
 
Posts: 4
Joined: Sun Oct 08, 2017 5:43 pm

Please be positive and constructive with your questions and comments.