SHARE
TWEET

Untitled

a guest Sep 10th, 2015 230 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void setup() {
  2. // put your setup code here, to run once:
  3.  
  4. /*
  5. Lighting Cloud Mood Lamp By James Bruce
  6. View the full tutorial and build guide at http://www.makeuseof.com/
  7.  
  8. Sound sampling code originally by Adafruit Industries. Distributed under the BSD license.
  9. This paragraph must be included in any redistribution.
  10. */
  11.  
  12. #include
  13. #include “FastLED.h”
  14.  
  15. // How many leds in your strip?
  16. #define NUM_LEDS 85
  17. #define DATA_PIN 6
  18.  
  19. // Mode enumeration – if you want to add additional party or colour modes, add them here; you’ll need to map some IR codes to them later;
  20. // and add the modes into the main switch loop
  21. enum Mode { CLOUD,ACID,OFF,ON,RED,GREEN,BLUE,FADE};
  22. Mode mode = CLOUD;
  23. Mode lastMode = CLOUD;
  24.  
  25. // Mic settings, shouldn’t need to adjust these.
  26. #define MIC_PIN 0 // Microphone is attached to this analog pin
  27. #define DC_OFFSET 0 // DC offset in mic signal – if unusure, leave 0
  28. #define NOISE 10 // Noise/hum/interference in mic signal
  29. #define SAMPLES 10 // Length of buffer for dynamic level adjustment
  30. byte
  31. volCount = 0; // Frame counter for storing past volume data
  32. int
  33. vol[SAMPLES]; // Collection of prior volume samples
  34. int n, total = 30;
  35. float average = 0;
  36.  
  37. // used to make basic mood lamp colour fading feature
  38. int fade_h;
  39. int fade_direction = 1;
  40.  
  41. // Define the array of leds
  42. CRGB leds[NUM_LEDS];
  43.  
  44. void setup() {
  45. // this line sets the LED strip type – refer fastLED documeantion for more details https://github.com/FastLED/FastLED
  46. FastLED.addLeds(leds, NUM_LEDS);
  47. // starts the audio samples array at volume 15.
  48. memset(vol, 15, sizeof(vol));
  49. Serial.begin(115200);
  50. Wire.begin(9); // Start I2C Bus as a Slave (Device Number 9)
  51. Wire.onReceive(receiveEvent); // register event
  52. }
  53.  
  54. void receiveEvent(int bytes) {
  55.  
  56. // Here, we set the mode based on the IR signal received. Check the debug log when you press a button on your remote, and
  57. // add the hex code here (you need 0x prior to each command to indicate it’s a hex value)
  58. while(Wire.available())
  59. {
  60. unsigned int received = Wire.read();
  61. Serial.print(“Receiving IR hex: “);
  62. Serial.println(received,HEX);
  63. lastMode = mode;
  64. switch(received){
  65. case 0x3F:
  66. mode = ON; break;
  67. case 0xBF:
  68. mode = OFF; break;
  69. case 0x2F:
  70. mode = CLOUD; break;
  71. case 0xF:
  72. mode = ACID; break;
  73. case 0x37:
  74. mode = FADE; break;
  75. case 0x9F:
  76. mode = BLUE; break;
  77. case 0x5F:
  78. mode = GREEN; break;
  79. case 0xDF:
  80. mode = RED; break;
  81. }
  82.  
  83. void loop() {
  84. // put your main code here, to run repeatedly:
  85. // Maps mode names to code functions.
  86. switch(mode){
  87. case CLOUD: detect_thunder();reset();break;
  88. case ACID: acid_cloud();reset();break;
  89. case OFF:reset();break;
  90. case ON: constant_lightning();reset();break;
  91. case RED: single_colour(0);break;
  92. case BLUE: single_colour(160);break;
  93. case GREEN: single_colour(96);break;
  94. case FADE: colour_fade();break;
  95. default: detect_thunder(); reset();break;
  96. }
  97.  
  98. }
  99.  
  100. // Makes all the LEDs a single colour, see https://raw.githubusercontent.com/FastLED/FastLED/gh-pages/images/HSV-rainbow-with-desc.jpg for H values
  101. void single_colour(int H){
  102. for (int i=0;i<NUM_LEDS;i++){
  103. leds[i] = CHSV( H, 255, 255);
  104. }
  105. //avoid flickr which occurs when FastLED.show() is called – only call if the colour changes
  106. if(lastMode != mode){
  107. FastLED.show();
  108. lastMode = mode;
  109. }
  110. delay(50);
  111. }
  112.  
  113. void colour_fade(){
  114. //mood mood lamp that cycles through colours
  115. for (int i=0;i254){
  116. fade_direction = -1; //reverse once we get to 254
  117. }
  118. else if(fade_h < 0){
  119. fade_direction = 1;
  120. }
  121.  
  122. fade_h += fade_direction;
  123. FastLED.show();
  124. delay(100);
  125. }
  126.  
  127. void detect_thunder() {
  128.  
  129. n = analogRead(MIC_PIN); // Raw reading from mic
  130. n = abs(n – 512 – DC_OFFSET); // Center on zero
  131. n = (n = SAMPLES) volCount = 0; // Advance/rollover sample counter
  132.  
  133. total = 0;
  134. for(int i=0; i<SAMPLES; i++) {
  135. total += vol[i];
  136. }
  137.  
  138. // If you're having trouble getting the cloud to trigger, uncomment this block to output a ton of debug on current averages.
  139. // Note that this WILL slow down the program and make it less sensitive due to lower sample rate.
  140.  
  141. /*
  142. for(int t=0; taverage){
  143. Serial.println(“TRIGGERED”);
  144. reset();
  145.  
  146. //I’ve programmed 3 types of lightning. Each cycle, we pick a random one.
  147. switch(random(1,3)){
  148. //switch(3){
  149.  
  150. case 1:
  151. thunderburst();
  152. delay(random(10,500));
  153. Serial.println(“Thunderburst”);
  154. break;
  155.  
  156. case 2:
  157. rolling();
  158. Serial.println(“Rolling”);
  159. break;
  160.  
  161. case 3:
  162. crack();
  163. delay(random(50,250));
  164. Serial.println(“Crack”);
  165. break;
  166.  
  167. }
  168. }
  169. }
  170.  
  171. // utility function to turn all the lights off.
  172. void reset(){
  173. for (int i=0;i<NUM_LEDS;i++){
  174. leds[i] = CHSV( 0, 0, 0);
  175. }
  176. FastLED.show();
  177.  
  178. }
  179.  
  180. void acid_cloud(){
  181. // a modification of the rolling lightning which adds random colour. trippy.
  182. //iterate through every LED
  183. for(int i=0;i90){
  184. leds[i] = CHSV( random(0,255), 255, 255);
  185.  
  186. }
  187. else{
  188. leds[i] = CHSV(0,0,0);
  189. }
  190. }
  191. FastLED.show();
  192. delay(random(5,100));
  193. reset();
  194.  
  195. //}
  196. }
  197.  
  198. void rolling(){
  199. // a simple method where we go through every LED with 1/10 chance
  200. // of being turned on, up to 10 times, with a random delay wbetween each time
  201. for(int r=0;r<random(2,10);r++){
  202. //iterate through every LED
  203. for(int i=0;i90){
  204. leds[i] = CHSV( 0, 0, 255);
  205.  
  206. }
  207. else{
  208. //dont need reset as we’re blacking out other LEDs her
  209. leds[i] = CHSV(0,0,0);
  210. }
  211. }
  212. FastLED.show();
  213. delay(random(5,100));
  214. reset();
  215.  
  216. }
  217. }
  218.  
  219. void crack(){
  220. //turn everything white briefly
  221. for(int i=0;i<NUM_LEDS;i++) {
  222. leds[i] = CHSV( 0, 0, 255);
  223. }
  224. FastLED.show();
  225. delay(random(10,100));
  226. reset();
  227. }
  228.  
  229. void thunderburst(){
  230.  
  231. // this thunder works by lighting two random lengths
  232. // of the strand from 10-20 pixels.
  233. int rs1 = random(0,NUM_LEDS/2);
  234. int rl1 = random(10,20);
  235. int rs2 = random(rs1+rl1,NUM_LEDS);
  236. int rl2 = random(10,20);
  237.  
  238. //repeat this chosen strands a few times, adds a bit of realism
  239. for(int r = 0;r<random(3,6);r++){
  240.  
  241. for(int i=0;i< rl1; i++){
  242. leds[i+rs1] = CHSV( 0, 0, 255);
  243. }
  244.  
  245. if(rs2+rl2 < NUM_LEDS){
  246. for(int i=0;i< rl2; i++){
  247. leds[i+rs2] = CHSV( 0, 0, 255);
  248. }
  249. }
  250.  
  251. FastLED.show();
  252. //stay illuminated for a set time
  253. delay(random(10,50));
  254.  
  255. reset();
  256. delay(random(10,50));
  257. }
  258.  
  259. }
  260.  
  261. // basically just a debug mode to show off the lightning in all its glory, no sound reactivity.
  262. void constant_lightning(){
  263. switch(random(1,10)){
  264. case 1:
  265. thunderburst();
  266. delay(random(10,500));
  267. Serial.println("Thunderburst");
  268. break;
  269.  
  270. case 2:
  271. rolling();
  272. Serial.println("Rolling");
  273. break;
  274.  
  275. case 3:
  276. crack();
  277. delay(random(50,250));
  278. Serial.println("Crack");
  279. break;
  280. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top