Advertisement
Guest User

cloudlamp sample

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