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();
}
}
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 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!