WS2812 RGB LED - Need help with code.

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MJ
 
Posts: 4
Joined: Sat Apr 19, 2014 1:56 pm

WS2812 RGB LED - Need help with code.

Post by MJ »

Hello,

I am working on an electric motorcycle project to use an arduino to control some WS2812 leds as blinkers and I want to also be able to control the lights via bluetooth. I am just a beginner so please excuse my poor coding. =D

I have an Uno with the BLE Shield and I have modified the RGB Led example from Red Bear Lab that lets me change the color of an LED from my iphone. I wanted to just add the functionality to read a pin connected to the flasher relay on the motorcycle and make the led's blink yellow. With a little help I am at the point where the blinkers work, but now the ios app can't change the colors, they just flash the color. Here is the code I have so far:

Code: Select all

#include "Adafruit_NeoPixel.h"
#include <SPI.h>
#include <boards.h>
#include <ble_shield.h>
#include <services.h>

//Control Pin(s)
#define license_light 4
#define Left_Blinkers 5
#define Right_Blinkers 6
#define Left_Signal_Input 7
#define Right_Signal_Input 8
int lsiState = 0;
int rsiState = 0;

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip_left = Adafruit_NeoPixel(4, Left_Blinkers, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip_right = Adafruit_NeoPixel(4, Right_Blinkers, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel rear_pixel = Adafruit_NeoPixel(1, license_light, NEO_GRB + NEO_KHZ800);

int R = 0;
int G = 0;
int B = 0;

void setup() 
{
  strip_left.begin();
  strip_left.show(); // Initialize all pixels to 'off'
  strip_right.begin();
  strip_right.show(); // Initialize all pixels to 'off'
  rear_pixel.begin();
  rear_pixel.setPixelColor(0, 255, 255, 255);
  rear_pixel.show(); // turn rear light on
  pinMode(Left_Signal_Input, INPUT);
  pinMode(Right_Signal_Input, INPUT);
  ble_begin();
}

void loop() 
{
  if(3 == ble_available())
  {
    R = ble_read();
    G = ble_read();
    B = ble_read();
    colorWipe( strip_left.Color(R, G, B) );
    colorWipe( strip_right.Color(R, G, B) );
  }
  ble_do_events();

  //simplify syntax
  uint32_t yellowl = strip_left.Color(255, 255, 0);
  uint32_t yellowr = strip_right.Color(255, 255, 0);
  uint32_t offl = strip_left.Color(0, 0, 0);
  uint32_t offr = strip_right.Color(0, 0, 0);

  //Configure Input Pins
  lsiState = digitalRead(Left_Signal_Input);
  rsiState = digitalRead(Right_Signal_Input);

  //Actions for Blinkers 
  if(lsiState == HIGH) { 
    strip_left.setPixelColor(0, yellowl);
    strip_left.setPixelColor(1, yellowl);
    strip_left.setPixelColor(2, yellowl);
    strip_left.setPixelColor(3, yellowl);
    strip_left.show();
  }
  if(lsiState == LOW) {
    strip_left.setPixelColor(0, offl);
    strip_left.setPixelColor(1, offl);
    strip_left.setPixelColor(2, offl);
    strip_left.setPixelColor(3, offl); 
    strip_left.show();
  }
  if(rsiState == HIGH) { 
    strip_right.setPixelColor(0, yellowr);
    strip_right.setPixelColor(1, yellowr);
    strip_right.setPixelColor(2, yellowr);
    strip_right.setPixelColor(3, yellowr);
    strip_right.show();
  }
  if(rsiState == LOW) {
    strip_right.setPixelColor(0, offr);
    strip_right.setPixelColor(1, offr);
    strip_right.setPixelColor(2, offr);
    strip_right.setPixelColor(3, offr); 
    strip_right.show();
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c) 
{
  for(uint16_t i=0; i<strip_left.numPixels(); i++) 
  {
    strip_left.setPixelColor(i, c);
    strip_left.show();
  }
  for(uint16_t i=0; i<strip_right.numPixels(); i++) 
  {
    strip_right.setPixelColor(i, c);
    strip_right.show();
  }
}
I know it's ugly, I could really use some help figuring it out. I have two issues at this point:

1. I would like it so that that when a blinker is activated then deacvtivated, the LED's go back to whatever state they were in before. Say if I set the color of the led's to white, then use the blinker, I would like them return to white.

2. how can I set the syntax for the color 'yellow' so that any pixel can use it?

Code: Select all

 uint32_t yellowl = strip_left.Color(255, 255, 0);
  uint32_t yellowr = strip_right.Color(255, 255, 0);
I don't know enough about coding to understand what I am really doing so in order for the sketch to compile was to delare yellow the way I did above. How can I declare yellow so that both strip_left and strip_right can use it?

I hope I was clear with my questions. I really appreciate the help and please let me know if there is a way improve or do something differently... Cheers!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: WS2812 RGB LED - Need help with code.

Post by adafruit_support_mike »

The first piece of advice I'm required to give is that you check your local street vehicle regs to make sure your project won't get you ticketed.

We tend to be leery of vehicular mods because of the whole "could kill someone if it fails in an appropriately bad way" thing. We're less cautious about non-critical stuff.. down lighting, interior lighting, etc.. but anything that can be tied to operation is our of range for our expertise and legal fees.

I'd be especially leery of signal lights controlled by bluetooth, as that's just another few points of potential failure.

MJ
 
Posts: 4
Joined: Sat Apr 19, 2014 1:56 pm

Re: WS2812 RGB LED - Need help with code.

Post by MJ »

Yes Mike, thanks. My main purpose with the bluetooth functionality is just for show while not riding.

After some more testing, it looks like the right side blinkers aren't working, they just stay on and don't blink. I could really use some help with the code. =D

MJ
 
Posts: 4
Joined: Sat Apr 19, 2014 1:56 pm

Re: WS2812 RGB LED - Need help with code.

Post by MJ »

So I got the blinkers working again by just using the number values for the pixels:

Code: Select all

  //Actions for Blinkers 
  if(lsiState == HIGH) { 
    strip_left.setPixelColor(0, 255, 255, 0);
    strip_left.setPixelColor(1, 255, 255, 0);
    strip_left.setPixelColor(2, 255, 255, 0);
    strip_left.setPixelColor(3, 255, 255, 0);
    strip_left.show();
  }
  if(lsiState == LOW) {
    strip_left.setPixelColor(0, 0, 0, 0);
    strip_left.setPixelColor(1, 0, 0, 0);
    strip_left.setPixelColor(2, 0, 0, 0);
    strip_left.setPixelColor(3, 0, 0, 0); 
    strip_left.show();
  }
  if(rsiState == HIGH) { 
    strip_right.setPixelColor(0, 255, 255, 0);
    strip_right.setPixelColor(1, 255, 255, 0);
    strip_right.setPixelColor(2, 255, 255, 0);
    strip_right.setPixelColor(3, 255, 255, 0);
    strip_right.show();
  }
  if(rsiState == LOW) {
    strip_right.setPixelColor(0, 0, 0, 0);
    strip_right.setPixelColor(1, 0, 0, 0);
    strip_right.setPixelColor(2, 0, 0, 0);
    strip_right.setPixelColor(3, 0, 0, 0); 
    strip_right.show();
  }
}

So the important part of this is working, but would still like to figure out how to use the bluetooth part as well.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: WS2812 RGB LED - Need help with code.

Post by adafruit_support_mike »

"Standing still" is good. ;-)

Your control code looks okay.. the technical term for what you've done is "unrolling the loop," which is slightly faster than wrapping a small number of operations in a for() or while() loop. In this case, the parts you're repeating are small enough and simple enough that you wouldn't gain much by making the code fancier. It works, you can understand what it does by reading it, so call it a win and move on to the next interesting problem.

WRT the bluetooth shield, I'd suggest putting the NeoPixels aside temporarily and working through the examples in the tutorial: https://learn.adafruit.com/getting-star ... e-breakout

Those will give you a chance to see how the thing works and what the code should do. Once things start looking familiar there, you'll be ready to mix and match.

MJ
 
Posts: 4
Joined: Sat Apr 19, 2014 1:56 pm

Re: WS2812 RGB LED - Need help with code.

Post by MJ »

Thank you so much for your reply!

Makes me feel a lot better knowing I can move on and you are also right about trying too much at once. I am going to mess around with the LED's some more to get a better understanding before diving into the Bluetooth part... I thought it may be cool to have a "startup" light sequence.

Again, thank you.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”