123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
/*
* -----------------------
* Chroma - light in a box.
* -----------------------
* Code by PerfectPixel, 2016.
* Made with code snippets from Adafruit's Neopixel library and Bluetuino Examples
* -----------------------
* PerfectPixel - http://www.instructables.com/member/perfectpixel
* Adafruit - http://www.adafruit.com
* Bluetuino - http://blog.bluetuino.com
* -----------------------
*/
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#define PIN 9
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial bluetooth(10, 11); // RX, TX
int fadespeed = 2;
int manual = false;
int mRed = 0;
int mGreen = 0;
int mBlue = 0;
uint32_t colour = strip.Color(0,0,0);
String receivedBluetoothString = "";
void setup() {
bluetooth.begin(9600);
strip.begin();
strip.show();
}
void loop() {
while (bluetooth.available() > 0) {
char receivedBluetoothChar = bluetooth.read();
receivedBluetoothString += receivedBluetoothChar;
if (receivedBluetoothChar == '\n') {
if (receivedBluetoothString.toInt() == 876) { //RED - color preset #1
colour = strip.Color(255,0,0); //change this to change the colour
} else if(receivedBluetoothString.toInt() == 877) { //GREEN - color preset #2
colour = strip.Color(0,255,0); //change this to change the colour
}else if(receivedBluetoothString.toInt() == 878) { //BLUE - color preset #3
colour = strip.Color(0,0,255); //change this to change the colour
}else if(receivedBluetoothString.toInt() == 1) { //RAINBOW (full)
rainbow(fadespeed);
}else if(receivedBluetoothString.toInt() == 2) { //RAINBOW (individual)
rainbowCycle(fadespeed);
}else if (receivedBluetoothString.endsWith("Slider\n")) { //FADESPEED SLIDER
fadespeed = receivedBluetoothString.toInt();
} else if(receivedBluetoothString.toInt() == 55) { //MANUAL COLOUR CONTROL
if(manual == true){
manual = false;
} else if (manual == false){
manual = true;
}
}
if (receivedBluetoothString.endsWith("RedSlider\n")) { //update mRed value
mRed = receivedBluetoothString.toInt();
}
if (receivedBluetoothString.endsWith("GreenSlider\n")) { //update mRed value
mGreen = receivedBluetoothString.toInt();
}
if (receivedBluetoothString.endsWith("BlueSlider\n")) { //update mRed value
mBlue = receivedBluetoothString.toInt();
}
if(manual == true){ //overwrides colours if manual control is active.
colorWipe(strip.Color(mRed, mGreen, mBlue), 1);
} else {
colorWipe(colour, 1); //if manual color is not activated
}
receivedBluetoothString = "";
}
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
if(manual == true) return;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
if(manual == true) return;
for(j=0; j<256*2; j++) { // 2 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}