Hardware
-Arduino nano
-7x ws2812 Neopixel jewel
-1x ws2812 Neopixel
-hm-10 Bluetooth module
-Pre existing code that works fine-
--Bluetooth app communicates with Arduino Nano and changes a 7 chip Neopixel jewel -
I want to add a single neopixel chip and be able to switch power on between one or the other via a momentary switch...
I am very amateur at coding so bear with me. I am sure it's a simple thing that needs to be changed or that i tried to combine the wrong code from different projects.
Code: Select all
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
//hook the bluetooth rx to pin 9, tx to pin 8
SoftwareSerial bt(9,8); // RX, TX
int red,green,blue;
#define PIN 7
#define PIN2 6
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?
//#define LED_COUNT 16
// 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_a = Adafruit_NeoPixel(7, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(1, PIN2, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(PIN, OUTPUT);
pinMode(PIN2, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
randomSeed(analogRead(0));
Serial.begin(9600);
Serial.println("Lets light the world");
// set the data rate for the SoftwareSerial port - Bluetooth
bt.begin(9600);
//Strip LEDs
strip_b.begin();
strip_b.show(); // Initialize all pixels to 'off'
strip_a.begin();
strip_a.show(); // Initialize all pixels to 'off'
//start doing something
colorSet(strip_a.Color(random(1,255), random(1,255), random(1,255)), 0);
colorSet2(strip_b.Color(random(1,255), random(1,255), random(1,255)), 0);
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistent readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 2;
} else {
if (Mode == 2) {
Mode = 3;
} else {
if (Mode == 3) {
Mode = 0;
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
{
strip_b.begin();
strip_b.show(); // Initialize all pixels to 'off'
strip_a.begin();
strip_a.show(); // Initialize all pixels to 'off'
}
// run over and over
if (bt.available() > 0) {
// Get the colors
int red = bt.parseInt();
// do it again:
int green = bt.parseInt();
// do it again:
int blue = bt.parseInt();
// look for the newline. That's the end of your
if (bt.read() == '\n') {
// sends confirmation
bt.println("received");
// constrain the values to 0 - 255
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
//create some codes here:
//if all 0, lets create some sequence
if (red == 0 && blue == 0 and green == 0) {
rainbow(150);
Serial.println("Rainbow");
}
else {
// fill strip
colorSet(strip_a.Color(red, green, blue), 0);
colorSet2(strip_b.Color(red, green, blue), 0);
Serial.println("received:"+String(red)+","+String(green)+","+String(blue));
}
}
}
}
// Fill strip with a color
void colorSet(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip_a.numPixels(); i++) {
strip_a.setPixelColor(i, c);
strip_a.show();
for(uint16_t i=0; i<strip_b.numPixels(); i++) {
strip_b.setPixelColor(i, c);
strip_b.show();
delay(wait);
}
}
}
//Rainbow
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip_a.numPixels(); i++) {
strip_a.setPixelColor(i, Wheel((i+j) & 255));
}
for(i=0; i<strip_b.numPixels(); i++) {
strip_b.setPixelColor(i, Wheel((i+j) & 255));
}
strip_b.show();
strip_a.show();
delay(wait);
}
}
// 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) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip_a.Color(255 - WheelPos * 3, 0, WheelPos * 3);
return strip_b.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip_a.Color(0, WheelPos * 3, 255 - WheelPos * 3);
return strip_b.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip_a.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
return strip_b.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
error 'colorSet2' was not declared in this scope
if I change it to
Code: Select all
colorSet((strip_a,strip_b).Color(random(1,255), random(1,255), random(1,255)), 0);
it verifies but I'm not sure that will work...
Thanks