Advertisement
Guest User

Chroma

a guest
Mar 31st, 2016
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /*
  2. * -----------------------
  3. * Chroma - light in a box.
  4. * -----------------------
  5. * Code by PerfectPixel, 2016.
  6. * Made with code snippets from Adafruit's Neopixel library and Bluetuino Examples
  7. * -----------------------
  8. * PerfectPixel - http://www.instructables.com/member/perfectpixel
  9. * Adafruit - http://www.adafruit.com
  10. * Bluetuino - http://blog.bluetuino.com
  11. * -----------------------
  12. */
  13.  
  14. #include <SoftwareSerial.h>
  15. #include <Adafruit_NeoPixel.h>
  16. #define PIN 9
  17.  
  18. Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
  19.  
  20. SoftwareSerial bluetooth(10, 11); // RX, TX
  21.  
  22. int fadespeed = 2;
  23. int manual = false;
  24.  
  25. int mRed = 0;
  26. int mGreen = 0;
  27. int mBlue = 0;
  28.  
  29. uint32_t colour = strip.Color(0,0,0);
  30.  
  31. String receivedBluetoothString = "";
  32.  
  33. void setup() {
  34. bluetooth.begin(9600);
  35. strip.begin();
  36. strip.show();
  37. }
  38.  
  39. void loop() {
  40. while (bluetooth.available() > 0) {
  41.  
  42. char receivedBluetoothChar = bluetooth.read();
  43. receivedBluetoothString += receivedBluetoothChar;
  44.  
  45. if (receivedBluetoothChar == '\n') {
  46. if (receivedBluetoothString.toInt() == 876) { //RED - color preset #1
  47. colour = strip.Color(255,0,0); //change this to change the colour
  48. } else if(receivedBluetoothString.toInt() == 877) { //GREEN - color preset #2
  49. colour = strip.Color(0,255,0); //change this to change the colour
  50. }else if(receivedBluetoothString.toInt() == 878) { //BLUE - color preset #3
  51. colour = strip.Color(0,0,255); //change this to change the colour
  52. }else if(receivedBluetoothString.toInt() == 1) { //RAINBOW (full)
  53. rainbow(fadespeed);
  54. }else if(receivedBluetoothString.toInt() == 2) { //RAINBOW (individual)
  55. rainbowCycle(fadespeed);
  56. }else if (receivedBluetoothString.endsWith("Slider\n")) { //FADESPEED SLIDER
  57. fadespeed = receivedBluetoothString.toInt();
  58. } else if(receivedBluetoothString.toInt() == 55) { //MANUAL COLOUR CONTROL
  59. if(manual == true){
  60. manual = false;
  61. } else if (manual == false){
  62. manual = true;
  63. }
  64. }
  65. if (receivedBluetoothString.endsWith("RedSlider\n")) { //update mRed value
  66. mRed = receivedBluetoothString.toInt();
  67. }
  68. if (receivedBluetoothString.endsWith("GreenSlider\n")) { //update mRed value
  69. mGreen = receivedBluetoothString.toInt();
  70. }
  71. if (receivedBluetoothString.endsWith("BlueSlider\n")) { //update mRed value
  72. mBlue = receivedBluetoothString.toInt();
  73. }
  74. if(manual == true){ //overwrides colours if manual control is active.
  75. colorWipe(strip.Color(mRed, mGreen, mBlue), 1);
  76. } else {
  77. colorWipe(colour, 1); //if manual color is not activated
  78. }
  79. receivedBluetoothString = "";
  80. }
  81. }
  82. }
  83. void colorWipe(uint32_t c, uint8_t wait) {
  84. for(uint16_t i=0; i<strip.numPixels(); i++) {
  85. strip.setPixelColor(i, c);
  86. strip.show();
  87. delay(wait);
  88. }
  89. }
  90.  
  91.  
  92. void rainbow(uint8_t wait) {
  93. uint16_t i, j;
  94. if(manual == true) return;
  95. for(j=0; j<256; j++) {
  96. for(i=0; i<strip.numPixels(); i++) {
  97. strip.setPixelColor(i, Wheel((i+j) & 255));
  98. }
  99. strip.show();
  100. delay(wait);
  101. }
  102. }
  103. void rainbowCycle(uint8_t wait) {
  104. uint16_t i, j;
  105. if(manual == true) return;
  106. for(j=0; j<256*2; j++) { // 2 cycles of all colors on wheel
  107. for(i=0; i< strip.numPixels(); i++) {
  108. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  109. }
  110. strip.show();
  111. delay(wait);
  112. }
  113. }
  114. uint32_t Wheel(byte WheelPos) {
  115. WheelPos = 255 - WheelPos;
  116. if(WheelPos < 85) {
  117. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  118. }
  119. if(WheelPos < 170) {
  120. WheelPos -= 85;
  121. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  122. }
  123. WheelPos -= 170;
  124. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement