Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. // Wifi Lamp Project
  4.  
  5. // Vagtsal, 14/7/2017
  6.  
  7. // Arduino Mini Code
  8. // Receives commands from ESP8266 through serial connection and drives 6 strips of 18 addressable leds (WS2812B chipset)
  9. // It supports multiple colors, effects and brightness control
  10. // Current state is stored in EEPROM to revert after a power loss
  11.  
  12. #include <FastLED.h>
  13. #include <EEPROM.h>
  14.  
  15. #define BRIGHT_ADDR 200
  16. #define RED_ADDR 201
  17. #define GREEN_ADDR 202
  18. #define BLUE_ADDR 203
  19. #define EFFECT_ADDR 204
  20. #define WEATHER_ADDR 205
  21.  
  22. #define NO_EFFECT 0
  23. #define FIRE 1
  24. #define WEATHER 2
  25.  
  26. #define CLEAR 1
  27. #define CLOUDS 2
  28. #define RAIN 3
  29. #define THUNDERSTORM 4
  30. #define SNOW 5
  31.  
  32. #define LED_PIN1 3
  33. #define LED_PIN2 4
  34.  
  35. #define COLOR_ORDER GRB
  36. #define CHIPSET WS2812B
  37. #define NUM_LEDS 11
  38.  
  39. #define COOLING 120
  40. #define SPARKING 190
  41. #define FRAMES_PER_SECOND 120
  42.  
  43. #define fadeupamount 40
  44. #define fadedownamount 30
  45. #define fadesnowupamount 15
  46. #define fadesnowdownamount 25
  47.  
  48. enum State {Idle = 0, FadeUp = 1, FadeDown = 2};
  49. enum State LedState[NUM_LEDS];
  50.  
  51.  
  52. #define DelayTime 5
  53. #define MaxLoopNum 10 //this line needs adding
  54. unsigned long timer;
  55.  
  56. CRGB leds1[NUM_LEDS];
  57. CRGB leds2[NUM_LEDS];
  58.  
  59.  
  60. CRGB color;
  61. int effect;
  62. int brightness;
  63. int weather;
  64. // Initialize our LED array.
  65. int sensorPin = A0; // for the photoresistor
  66. int sensorValue = 0;
  67. int outputValue = 0;
  68.  
  69.  
  70. // Standard delay value in milliseconds.
  71. uint8_t thisdelay = 50;
  72. // Fade rate
  73. uint8_t fadeval = 149;
  74. uint8_t fadevale = 253;
  75. // number that helps to switch between Blue and White
  76. int n = 0;
  77.  
  78. String inputString = "";
  79.  
  80. void change_state(String choice){
  81. if (choice == "r\n"){
  82. color.r = 255;
  83. EEPROM.write(RED_ADDR, 255);
  84. }
  85. else if (choice == "nr\n"){
  86. color.r = 0;
  87. EEPROM.write(RED_ADDR, 0);
  88. }
  89. else if (choice == "g\n"){
  90. color.g = 255;
  91. EEPROM.write(GREEN_ADDR, 255);
  92. }
  93. else if (choice == "ng\n"){
  94. color.g = 0;
  95. EEPROM.write(GREEN_ADDR, 0);
  96. }
  97. else if (choice == "b\n"){
  98. color.b = 255;
  99. EEPROM.write(BLUE_ADDR, 255);
  100. }
  101. else if (choice == "nb\n"){
  102. color.b = 0;
  103. EEPROM.write(BLUE_ADDR, 0);
  104. }
  105. else if (choice == "bu\n"){
  106. if (brightness <= 75){brightness += 25;}else{brightness = 100;}
  107. EEPROM.write(BRIGHT_ADDR, brightness);
  108. }
  109. else if (choice == "bd\n"){
  110. if (brightness >= 25){brightness -= 25;}else{brightness = 0;}
  111. EEPROM.write(BRIGHT_ADDR, brightness);
  112. }
  113. else if (choice == "f\n"){
  114. effect = FIRE;
  115. EEPROM.write(EFFECT_ADDR, FIRE);
  116. }
  117. else if (choice == "nf\n"){
  118. effect = NO_EFFECT;
  119. EEPROM.write(EFFECT_ADDR, NO_EFFECT);
  120. }
  121. else if (choice == "w\n"){
  122. effect = WEATHER;
  123. EEPROM.write(EFFECT_ADDR, WEATHER);
  124. }
  125. else if (choice == "nw\n"){
  126. effect = NO_EFFECT;
  127. EEPROM.write(EFFECT_ADDR, NO_EFFECT);
  128. }
  129.  
  130. else if (choice == "ws\n"){
  131. weather = CLEAR;
  132. EEPROM.write(WEATHER_ADDR, weather);
  133. }
  134. else if (choice == "wc\n"){
  135. weather = CLOUDS;
  136. EEPROM.write(WEATHER_ADDR, weather);
  137. }
  138. else if (choice == "wr\n"){
  139. weather = RAIN;
  140. EEPROM.write(WEATHER_ADDR, weather);
  141. }
  142. else if (choice == "wt\n"){
  143. weather = THUNDERSTORM;
  144. EEPROM.write(WEATHER_ADDR, weather);
  145. }
  146. else if (choice == "wx\n"){
  147. weather = SNOW;
  148. EEPROM.write(WEATHER_ADDR, weather);
  149. }
  150.  
  151. if (effect == NO_EFFECT){
  152. for( int j = 0; j < NUM_LEDS; j++) {
  153. leds1[j] = color;
  154. leds2[j] = color;
  155. }
  156. }
  157.  
  158. // exponential mapping
  159. int expBrightness = brightness * brightness;
  160. FastLED.setBrightness(map(expBrightness, 0, 10000, 0, 143));
  161.  
  162. FastLED.show();
  163. }
  164.  
  165.  
  166. void Fire2012(CRGB leds[NUM_LEDS]){
  167. // Array of temperature readings at each simulation cell
  168. static byte heat[NUM_LEDS];
  169.  
  170. // Step 1. Cool down every cell a little
  171. for( int i = 0; i < NUM_LEDS; i++) {
  172. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  173. }
  174.  
  175. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  176. for( int k= NUM_LEDS - 1; k >= 2; k--) {
  177. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  178. }
  179.  
  180. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  181. if( random8() < SPARKING ) {
  182. int y = random8(7);
  183. heat[y] = qadd8( heat[y], random8(160,255) );
  184. }
  185.  
  186. // Step 4. Map from heat cells to LED colors
  187. for( int j = 0; j < NUM_LEDS; j++) {
  188. CRGB color = HeatColor( heat[j]);
  189. leds[j] = color;
  190. }
  191. }
  192.  
  193. void weather_effect(CRGB leds[NUM_LEDS], int period0, int period1, int period2, int period3, int period4, int period5){
  194. for( int i = 0; i < NUM_LEDS; i++) {
  195. switch (i){
  196. case 0:
  197. case 5:
  198. case 2:
  199. if (weather == CLEAR || weather == CLOUDS){
  200. leds[i].b = period0;
  201. leds[i].g = period0;
  202. leds[i].r = period0;
  203. }
  204. if (weather == CLOUDS){
  205. leds[i] = CHSV((150), (0), period0);
  206. }
  207. break;
  208. case 1:
  209. case 17:
  210. case 15:
  211. if (weather == CLEAR || weather == CLOUDS){
  212. leds[i].b = period1;
  213. leds[i].g = period1;
  214. }
  215. if (weather == CLOUDS){
  216. leds[i] = CHSV((150), (0), period1);
  217. }
  218. break;
  219. case 12:
  220. case 7:
  221. case 10:
  222. if (weather == CLEAR || weather == CLOUDS){
  223. leds[i].b = period2;
  224. leds[i].g = period2;
  225. }
  226. if (weather == CLOUDS){
  227. leds[i] = CHSV((150), (0), period2);
  228. }
  229. break;
  230. case 3:
  231. case 9:
  232. case 13:
  233. if (weather == CLEAR || weather == CLOUDS){
  234. leds[i].g = period3;
  235. leds[i].r = period3;
  236. }
  237. if (weather == CLOUDS){
  238. leds[i] = CHSV((150), (0), period3);
  239. }
  240. break;
  241. case 4:
  242. case 14:
  243. case 8:
  244. if (weather == CLEAR || weather == CLOUDS){
  245. leds[i].b = period4;
  246. leds[i].g = period4;
  247.  
  248.  
  249. }
  250. if (weather == CLOUDS){
  251. leds[i] = CHSV((150), (0), period4);
  252. }
  253. break;
  254. case 6:
  255. case 16:
  256. case 11:
  257. if (weather == CLEAR || weather == CLOUDS){
  258. leds[i].b = period5;
  259. leds[i].g = period5;
  260.  
  261. }
  262. if (weather == CLOUDS){
  263. leds[i] = CHSV((150), (0), period5);
  264. }
  265. break;
  266. }
  267. if (weather == CLEAR){
  268.  
  269. }
  270. else if (weather == CLOUDS){
  271.  
  272. }
  273. }
  274. }
  275.  
  276.  
  277. void rain(){
  278.  
  279. int i = random(0,45);
  280. int LedPerUpdate;
  281. int y;
  282.  
  283. while (y != 1){
  284. FastLED.clear();
  285. y = 1;
  286. }
  287.  
  288. if (i == 0){
  289. LedPerUpdate = 0;
  290. }
  291. else if (i == 1){
  292. LedPerUpdate = 1;
  293. }
  294. else if (i == 2){
  295. LedPerUpdate = 1;
  296. }
  297. else if (i == 3){
  298. LedPerUpdate = 0;
  299. }
  300. else if (i == 4){
  301. LedPerUpdate = 0;
  302. }
  303.  
  304. if (timer <= millis()) {
  305. //Serial.println("Timer Called");
  306. timer = millis() + DelayTime;
  307.  
  308. uint8_t l;
  309. uint8_t LoopNum;
  310.  
  311. while (l < LedPerUpdate) {
  312. //Serial.println("Getting new led..");
  313.  
  314. int t = random(NUM_LEDS);
  315. LoopNum ++;
  316.  
  317. if (LedState[t] == Idle) {
  318. //Serial.println("Got new led");
  319. LedState[t] = FadeUp;
  320. l++;
  321. }
  322.  
  323. if(LoopNum >= MaxLoopNum){
  324. break;
  325. }
  326. }
  327. }
  328.  
  329. //update each led
  330. for (int i = 0; i < NUM_LEDS; i++) {
  331. switch (LedState[i]) {
  332. case Idle:
  333. break;
  334. case FadeUp:
  335. if (leds1[i].b <= (255 - fadeupamount)) {
  336. leds1[i].b += fadeupamount;
  337. leds2[i].b += fadeupamount;
  338. } else {
  339. LedState[i] = FadeDown;
  340. }
  341. break;
  342. case FadeDown:
  343. if (leds1[i].b >= (0 + fadedownamount)) {
  344. leds1[i].b -= fadedownamount;
  345. leds2[i].b -= fadedownamount;
  346. } else {
  347. LedState[i] = Idle;
  348. leds1[i] = CRGB(0, 0, 0);
  349. leds2[i] = CRGB(0, 0, 0);
  350. }
  351. break;
  352. }
  353. }
  354. FastLED.show();
  355.  
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362. void thunder(){
  363. int randomizeThunder = random8(6);
  364. for( int i = 0; i < NUM_LEDS; i++) {
  365. switch (randomizeThunder){
  366. case 0:
  367. leds1[i] = CRGB::White;
  368. break;
  369. case 1:
  370. leds2[i] = CRGB::White;
  371. break;
  372. }
  373. }
  374. FastLED.delay(50);
  375. for( int i = 0; i < NUM_LEDS; i++) {
  376. switch (randomizeThunder){
  377. case 0:
  378. leds1[i] = CRGB::Black;
  379. leds1[i].b = 15;
  380. break;
  381. case 1:
  382. leds2[i] = CRGB::Black;
  383. leds2[i].b = 15;
  384. break;
  385. }
  386. }
  387. }
  388.  
  389. void snowing(){
  390. int i = random(0,390);
  391. int LedPerUpdate;
  392.  
  393. int t;
  394.  
  395.  
  396. while (t != 1){
  397. FastLED.clear();
  398. t = 1;
  399. }
  400.  
  401.  
  402. if (i == 0){
  403. LedPerUpdate = 0;
  404. }
  405. else if (i == 1){
  406. LedPerUpdate = 1;
  407. }
  408. else if (i == 2){
  409. LedPerUpdate = 1;
  410. }
  411. else if (i == 3){
  412. LedPerUpdate = 0;
  413. }
  414. else if (i == 4){
  415. LedPerUpdate = 0;
  416. }
  417.  
  418. if (timer <= millis()) {
  419. //Serial.println("Timer Called");
  420. timer = millis() + DelayTime;
  421.  
  422. uint8_t l;
  423. uint8_t LoopNum;
  424.  
  425. while (l < LedPerUpdate) {
  426. //Serial.println("Getting new led..");
  427.  
  428. int t = random(NUM_LEDS);
  429. LoopNum ++;
  430.  
  431. if (LedState[t] == Idle) {
  432. //Serial.println("Got new led");
  433. LedState[t] = FadeUp;
  434. l++;
  435. }
  436.  
  437. if(LoopNum >= MaxLoopNum){
  438. break;
  439. }
  440. }
  441. }
  442.  
  443. //update each led
  444. for (int i = 0; i < NUM_LEDS; i++) {
  445. switch (LedState[i]) {
  446. case Idle:
  447. break;
  448. case FadeUp:
  449. if (leds1[i].b <= (255 - fadeupamount)) {
  450. leds1[i] += CHSV((190), (0), fadesnowupamount);
  451. leds2[i] += CHSV((120), (70), fadesnowupamount);
  452. } else {
  453. LedState[i] = FadeDown;
  454. }
  455. break;
  456. case FadeDown:
  457. if (leds1[i].b >= (0 + fadedownamount)) {
  458. leds1[i] -= CHSV((190), (0), fadesnowdownamount);
  459. leds2[i] -= CHSV((120), (70), fadesnowdownamount);
  460. } else {
  461. LedState[i] = Idle;
  462. leds1[i] = CRGB(0, 0, 0);
  463. leds2[i] = CRGB(0, 0, 0);
  464.  
  465. }
  466. break;
  467. }
  468. }
  469. FastLED.show();
  470.  
  471. }
  472.  
  473.  
  474. void setup() {
  475. delay(1000); // sanity delay
  476. FastLED.addLeds<CHIPSET, LED_PIN1, COLOR_ORDER>(leds1, NUM_LEDS).setCorrection( TypicalSMD5050 );
  477. FastLED.addLeds<CHIPSET, LED_PIN2, COLOR_ORDER>(leds2, NUM_LEDS).setCorrection( TypicalSMD5050 );
  478.  
  479.  
  480. /*
  481. EEPROM.write(BRIGHT_ADDR, 50);
  482. EEPROM.write(RED_ADDR, 1);
  483. EEPROM.write(GREEN_ADDR, 1);
  484. EEPROM.write(BLUE_ADDR, 1);
  485. EEPROM.write(EFFECT_ADDR, NO_EFFECT);
  486. EEPROM.write(WEATHER_ADDR, CLEAR);
  487. */
  488.  
  489.  
  490. brightness = EEPROM.read(BRIGHT_ADDR);
  491. color.r = EEPROM.read(RED_ADDR);
  492. color.g = EEPROM.read(GREEN_ADDR);
  493. color.b = EEPROM.read(BLUE_ADDR);
  494. effect = EEPROM.read(EFFECT_ADDR);
  495. weather = EEPROM.read(WEATHER_ADDR);
  496.  
  497. Serial1.begin(115200);
  498. inputString.reserve(200);
  499. }
  500.  
  501. void loop() {
  502.  
  503.  
  504.  
  505.  
  506. if (effect == FIRE){
  507. Fire2012(leds1);
  508. Fire2012(leds2);
  509. }
  510. if (effect == WEATHER){
  511. int period0;
  512. int period1;
  513. int period2;
  514. int period3;
  515. int period4;
  516. int period5;
  517.  
  518. if (weather == CLEAR || weather == CLOUDS){
  519. if (weather == CLEAR){
  520. period0 = beatsin16(3,50,255);
  521. period1 = beatsin16(4,50,255);
  522. period2 = beatsin16(5,50,255);
  523. period3 = beatsin16(7,50,255);
  524. period4 = beatsin16(11,20,255);
  525. period5 = beatsin16(12,40,255);
  526. }
  527. else if (weather == CLOUDS){
  528. period0 = beatsin16(0,100,255);
  529. period1 = beatsin16(0,100,255);
  530. period2 = beatsin16(0,100,255);
  531. period3 = beatsin16(0,100,255);
  532. period4 = beatsin16(0,100,255);
  533. period5 = beatsin16(0,100,255);
  534.  
  535.  
  536. }
  537. }
  538. weather_effect(leds1, period0, period1, period2, period3, period4, period5);
  539. weather_effect(leds2, period1, period2, period3, period4, period5, period0);
  540.  
  541.  
  542. if (weather == THUNDERSTORM || weather == RAIN){
  543. rain();
  544. if (weather == THUNDERSTORM){
  545. int randomThunder = random8(100);
  546. if (randomThunder > 98){
  547. thunder();
  548. FastLED.delay(1000 / 120);
  549. }
  550. }
  551. }
  552.  
  553. else if (weather == SNOW){
  554. snowing();
  555. }
  556.  
  557. }
  558.  
  559.  
  560. FastLED.show();
  561. FastLED.delay(1000 / FRAMES_PER_SECOND);
  562. }
  563.  
  564.  
  565. void serialEvent1() {
  566. while (Serial1.available()) {
  567. char inChar = (char)Serial1.read();
  568. inputString += inChar;
  569.  
  570. if (inChar == '\n') {
  571. change_state(inputString);
  572. Serial1.print(inputString);
  573.  
  574. inputString = "";
  575. }
  576. }
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement