Advertisement
Guest User

Beambox NEXT code

a guest
Jan 2nd, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <CapacitiveSensor.h>
  3.  
  4.  
  5. /*
  6. * CapitiveSense Library Demo Sketch
  7. * Oringinal sketch by Paul Badger 2008, Modified by PerfectPixel 2016 with code by Adafruit.
  8. * http://www.instructables.com/member/perfectpixel/
  9. * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
  10. * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
  11. * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
  12. * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
  13. *
  14. *---LINKS---
  15. * Original Sketch obtained from:
  16. * http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense
  17. *
  18. * Edited by PerfectPixel:
  19. * http://www.instructables.com/member/perfectpixel/
  20. *
  21. * With code by Adafruit
  22. * http://www.adafruit.com/
  23. */
  24.  
  25. #define PIN 9 //the pin the LED's are connected to.
  26.  
  27. Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_RGB + NEO_KHZ800);
  28.  
  29. CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4); // 1 megohm resistor between pins 4 & 2, pin 2 is sensor pin
  30. CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5); // 1 megohm resistor between pins 4 & 6, pin 6 is sensor pin
  31. CapacitiveSensor cs_2_8 = CapacitiveSensor(2,8); // 1 megohm resistor between pins 4 & 8, pin 8 is sensor pin
  32.  
  33. int i = 0;
  34. int on = 1;
  35.  
  36. void setup()
  37. {
  38. //Serial.begin(9600);
  39. pinMode(13, OUTPUT);
  40. strip.begin();
  41. strip.show();
  42. }
  43.  
  44. void loop()
  45. {
  46. long start = millis();
  47. long total1 = cs_2_4.capacitiveSensor(30);
  48. long total2 = cs_2_5.capacitiveSensor(30);
  49. long total3 = cs_2_8.capacitiveSensor(30);
  50. delay(10);
  51. if(total1 > 800){
  52. i++;
  53. colorWipe(strip.Color(0,0,0),1);
  54. digitalWrite(13, HIGH);
  55. delay(100);
  56. digitalWrite(13, LOW);
  57. } else if(total3 > 800){
  58. i--;
  59. colorWipe(strip.Color(0,0,0),1);
  60. digitalWrite(13, HIGH);
  61. delay(100);
  62. digitalWrite(13, LOW);
  63. }
  64.  
  65. if(i > 9) i = 9;
  66. if(i < 0) i = 0;
  67.  
  68. if(total2 < 800){ // Feel free to change these effects - they will be what is
  69. switch(i){ // displayed on each selecting using the capacitive touch buttons.
  70.  
  71. case 0: colorWipe(strip.Color(255,255,255),5);
  72. break;
  73. case 1: colorWipe(strip.Color(255,100,0),5);
  74. break;
  75. case 2: colorWipe(strip.Color(140,0,227),5);
  76. break;
  77. case 3: colorWipe(strip.Color(255,0,0),5);
  78. break;
  79. case 4: colorWipe(strip.Color(237,7,176),5);
  80. break;
  81. case 5: colorWipe(strip.Color(0,255,0),5);
  82. break;
  83. case 6: FadeInOut(0x00, 0xFF, 0xB3);
  84. break;
  85. case 7: RGBLoop();
  86. break;
  87. case 8: rainbowCycle(12);
  88. break;
  89. case 9: rainbow(12);
  90. break;
  91. // case 999; vixen() //activate vixen control - by pressing the middle button
  92. // break; //TODO - implement vixenlights control
  93. }
  94. } else {
  95. if(on == 0){
  96. on = 1;
  97. //Serial.println("Vixen Control toggled ON");
  98. delay(1000);
  99. i = 999;
  100. } else if(on == 1){
  101. on = 0;
  102. //Serial.println("Vixen Control toggled OFF");
  103. delay(1000);
  104. }
  105. }
  106. }
  107.  
  108. void colorWipe(uint32_t c, uint8_t wait) {
  109. for(uint16_t i=0; i<strip.numPixels(); i++) {
  110. strip.setPixelColor(i, c);
  111. delay(wait);
  112. }
  113. if(on == 1){
  114. for(int i=0; i<strip.numPixels(); i++){
  115. strip.setPixelColor(i, 0,0,0);
  116. }
  117. }
  118. strip.show();
  119. }
  120.  
  121. void rainbow(uint8_t wait) {
  122. uint16_t i, j;
  123.  
  124. for(j=0; j<256; j++) {
  125. for(i=0; i<strip.numPixels(); i++) {
  126. strip.setPixelColor(i, Wheel((i+j) & 255));
  127. }
  128. strip.show();
  129. delay(wait);
  130. }
  131. }
  132.  
  133. uint32_t Wheel(byte WheelPos) {
  134. WheelPos = 255 - WheelPos;
  135. if(WheelPos < 85) {
  136. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  137. }
  138. if(WheelPos < 170) {
  139. WheelPos -= 85;
  140. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  141. }
  142. WheelPos -= 170;
  143. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  144. }
  145.  
  146. void rainbowCycle(uint8_t wait) {
  147. uint16_t i, j;
  148.  
  149. for(j=0; j<256; j++) { // 1 cycles of all colors on wheel
  150. for(i=0; i< strip.numPixels(); i++) {
  151. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  152. }
  153. strip.show();
  154. delay(wait);
  155. }
  156. }
  157.  
  158. void RGBLoop(){
  159. for(int j = 0; j < 3; j++ ) {
  160. // Fade IN
  161. for(int k = 0; k < 256; k++) {
  162. switch(j) {
  163. case 0: setAll(k,0,0); break;
  164. case 1: setAll(0,k,0); break;
  165. case 2: setAll(0,0,k); break;
  166. }
  167. strip.show();
  168. delay(3);
  169. }
  170. // Fade OUT
  171. for(int k = 255; k >= 0; k--) {
  172. switch(j) {
  173. case 0: setAll(k,0,0); break;
  174. case 1: setAll(0,k,0); break;
  175. case 2: setAll(0,0,k); break;
  176. }
  177. strip.show();
  178. delay(3);
  179. }
  180. }
  181. }
  182.  
  183. void setAll(byte red, byte green, byte blue) {
  184. for(int i = 0; i < 6; i++ ) {
  185. strip.setPixelColor(i, red, green, blue);
  186. }
  187. strip.show();
  188. }
  189.  
  190. void FadeInOut(byte red, byte green, byte blue){
  191. float r, g, b;
  192.  
  193. for(int k = 50; k < 255; k=k+3) {
  194. r = (k/256.0)*red;
  195. g = (k/256.0)*green;
  196. b = (k/256.0)*blue;
  197. setAll(r,g,b);
  198. strip.show();
  199. delay(13);
  200. }
  201. delay(1500);
  202. for(int k = 255; k >= 50; k=k-3) {
  203. r = (k/256.0)*red;
  204. g = (k/256.0)*green;
  205. b = (k/256.0)*blue;
  206. setAll(r,g,b);
  207. strip.show();
  208. delay(13);
  209. }
  210. delay(350);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement