Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- // Wifi Lamp Project
- // Vagtsal, 14/7/2017
- // Arduino Mini Code
- // Receives commands from ESP8266 through serial connection and drives 6 strips of 18 addressable leds (WS2812B chipset)
- // It supports multiple colors, effects and brightness control
- // Current state is stored in EEPROM to revert after a power loss
- #include <FastLED.h>
- #include <EEPROM.h>
- #define BRIGHT_ADDR 200
- #define RED_ADDR 201
- #define GREEN_ADDR 202
- #define BLUE_ADDR 203
- #define EFFECT_ADDR 204
- #define WEATHER_ADDR 205
- #define NO_EFFECT 0
- #define FIRE 1
- #define WEATHER 2
- #define CLEAR 1
- #define CLOUDS 2
- #define RAIN 3
- #define THUNDERSTORM 4
- #define SNOW 5
- #define LED_PIN1 3
- #define LED_PIN2 4
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812B
- #define NUM_LEDS 11
- #define COOLING 120
- #define SPARKING 190
- #define FRAMES_PER_SECOND 120
- #define fadeupamount 40
- #define fadedownamount 30
- #define fadesnowupamount 15
- #define fadesnowdownamount 25
- enum State {Idle = 0, FadeUp = 1, FadeDown = 2};
- enum State LedState[NUM_LEDS];
- #define DelayTime 5
- #define MaxLoopNum 10 //this line needs adding
- unsigned long timer;
- CRGB leds1[NUM_LEDS];
- CRGB leds2[NUM_LEDS];
- CRGB color;
- int effect;
- int brightness;
- int weather;
- // Initialize our LED array.
- int sensorPin = A0; // for the photoresistor
- int sensorValue = 0;
- int outputValue = 0;
- // Standard delay value in milliseconds.
- uint8_t thisdelay = 50;
- // Fade rate
- uint8_t fadeval = 149;
- uint8_t fadevale = 253;
- // number that helps to switch between Blue and White
- int n = 0;
- String inputString = "";
- void change_state(String choice){
- if (choice == "r\n"){
- color.r = 255;
- EEPROM.write(RED_ADDR, 255);
- }
- else if (choice == "nr\n"){
- color.r = 0;
- EEPROM.write(RED_ADDR, 0);
- }
- else if (choice == "g\n"){
- color.g = 255;
- EEPROM.write(GREEN_ADDR, 255);
- }
- else if (choice == "ng\n"){
- color.g = 0;
- EEPROM.write(GREEN_ADDR, 0);
- }
- else if (choice == "b\n"){
- color.b = 255;
- EEPROM.write(BLUE_ADDR, 255);
- }
- else if (choice == "nb\n"){
- color.b = 0;
- EEPROM.write(BLUE_ADDR, 0);
- }
- else if (choice == "bu\n"){
- if (brightness <= 75){brightness += 25;}else{brightness = 100;}
- EEPROM.write(BRIGHT_ADDR, brightness);
- }
- else if (choice == "bd\n"){
- if (brightness >= 25){brightness -= 25;}else{brightness = 0;}
- EEPROM.write(BRIGHT_ADDR, brightness);
- }
- else if (choice == "f\n"){
- effect = FIRE;
- EEPROM.write(EFFECT_ADDR, FIRE);
- }
- else if (choice == "nf\n"){
- effect = NO_EFFECT;
- EEPROM.write(EFFECT_ADDR, NO_EFFECT);
- }
- else if (choice == "w\n"){
- effect = WEATHER;
- EEPROM.write(EFFECT_ADDR, WEATHER);
- }
- else if (choice == "nw\n"){
- effect = NO_EFFECT;
- EEPROM.write(EFFECT_ADDR, NO_EFFECT);
- }
- else if (choice == "ws\n"){
- weather = CLEAR;
- EEPROM.write(WEATHER_ADDR, weather);
- }
- else if (choice == "wc\n"){
- weather = CLOUDS;
- EEPROM.write(WEATHER_ADDR, weather);
- }
- else if (choice == "wr\n"){
- weather = RAIN;
- EEPROM.write(WEATHER_ADDR, weather);
- }
- else if (choice == "wt\n"){
- weather = THUNDERSTORM;
- EEPROM.write(WEATHER_ADDR, weather);
- }
- else if (choice == "wx\n"){
- weather = SNOW;
- EEPROM.write(WEATHER_ADDR, weather);
- }
- if (effect == NO_EFFECT){
- for( int j = 0; j < NUM_LEDS; j++) {
- leds1[j] = color;
- leds2[j] = color;
- }
- }
- // exponential mapping
- int expBrightness = brightness * brightness;
- FastLED.setBrightness(map(expBrightness, 0, 10000, 0, 143));
- FastLED.show();
- }
- void Fire2012(CRGB leds[NUM_LEDS]){
- // Array of temperature readings at each simulation cell
- static byte heat[NUM_LEDS];
- // Step 1. Cool down every cell a little
- for( int i = 0; i < NUM_LEDS; i++) {
- heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for( int k= NUM_LEDS - 1; k >= 2; k--) {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if( random8() < SPARKING ) {
- int y = random8(7);
- heat[y] = qadd8( heat[y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for( int j = 0; j < NUM_LEDS; j++) {
- CRGB color = HeatColor( heat[j]);
- leds[j] = color;
- }
- }
- void weather_effect(CRGB leds[NUM_LEDS], int period0, int period1, int period2, int period3, int period4, int period5){
- for( int i = 0; i < NUM_LEDS; i++) {
- switch (i){
- case 0:
- case 5:
- case 2:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].b = period0;
- leds[i].g = period0;
- leds[i].r = period0;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period0);
- }
- break;
- case 1:
- case 17:
- case 15:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].b = period1;
- leds[i].g = period1;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period1);
- }
- break;
- case 12:
- case 7:
- case 10:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].b = period2;
- leds[i].g = period2;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period2);
- }
- break;
- case 3:
- case 9:
- case 13:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].g = period3;
- leds[i].r = period3;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period3);
- }
- break;
- case 4:
- case 14:
- case 8:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].b = period4;
- leds[i].g = period4;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period4);
- }
- break;
- case 6:
- case 16:
- case 11:
- if (weather == CLEAR || weather == CLOUDS){
- leds[i].b = period5;
- leds[i].g = period5;
- }
- if (weather == CLOUDS){
- leds[i] = CHSV((150), (0), period5);
- }
- break;
- }
- if (weather == CLEAR){
- }
- else if (weather == CLOUDS){
- }
- }
- }
- void rain(){
- int i = random(0,45);
- int LedPerUpdate;
- int y;
- while (y != 1){
- FastLED.clear();
- y = 1;
- }
- if (i == 0){
- LedPerUpdate = 0;
- }
- else if (i == 1){
- LedPerUpdate = 1;
- }
- else if (i == 2){
- LedPerUpdate = 1;
- }
- else if (i == 3){
- LedPerUpdate = 0;
- }
- else if (i == 4){
- LedPerUpdate = 0;
- }
- if (timer <= millis()) {
- //Serial.println("Timer Called");
- timer = millis() + DelayTime;
- uint8_t l;
- uint8_t LoopNum;
- while (l < LedPerUpdate) {
- //Serial.println("Getting new led..");
- int t = random(NUM_LEDS);
- LoopNum ++;
- if (LedState[t] == Idle) {
- //Serial.println("Got new led");
- LedState[t] = FadeUp;
- l++;
- }
- if(LoopNum >= MaxLoopNum){
- break;
- }
- }
- }
- //update each led
- for (int i = 0; i < NUM_LEDS; i++) {
- switch (LedState[i]) {
- case Idle:
- break;
- case FadeUp:
- if (leds1[i].b <= (255 - fadeupamount)) {
- leds1[i].b += fadeupamount;
- leds2[i].b += fadeupamount;
- } else {
- LedState[i] = FadeDown;
- }
- break;
- case FadeDown:
- if (leds1[i].b >= (0 + fadedownamount)) {
- leds1[i].b -= fadedownamount;
- leds2[i].b -= fadedownamount;
- } else {
- LedState[i] = Idle;
- leds1[i] = CRGB(0, 0, 0);
- leds2[i] = CRGB(0, 0, 0);
- }
- break;
- }
- }
- FastLED.show();
- }
- void thunder(){
- int randomizeThunder = random8(6);
- for( int i = 0; i < NUM_LEDS; i++) {
- switch (randomizeThunder){
- case 0:
- leds1[i] = CRGB::White;
- break;
- case 1:
- leds2[i] = CRGB::White;
- break;
- }
- }
- FastLED.delay(50);
- for( int i = 0; i < NUM_LEDS; i++) {
- switch (randomizeThunder){
- case 0:
- leds1[i] = CRGB::Black;
- leds1[i].b = 15;
- break;
- case 1:
- leds2[i] = CRGB::Black;
- leds2[i].b = 15;
- break;
- }
- }
- }
- void snowing(){
- int i = random(0,390);
- int LedPerUpdate;
- int t;
- while (t != 1){
- FastLED.clear();
- t = 1;
- }
- if (i == 0){
- LedPerUpdate = 0;
- }
- else if (i == 1){
- LedPerUpdate = 1;
- }
- else if (i == 2){
- LedPerUpdate = 1;
- }
- else if (i == 3){
- LedPerUpdate = 0;
- }
- else if (i == 4){
- LedPerUpdate = 0;
- }
- if (timer <= millis()) {
- //Serial.println("Timer Called");
- timer = millis() + DelayTime;
- uint8_t l;
- uint8_t LoopNum;
- while (l < LedPerUpdate) {
- //Serial.println("Getting new led..");
- int t = random(NUM_LEDS);
- LoopNum ++;
- if (LedState[t] == Idle) {
- //Serial.println("Got new led");
- LedState[t] = FadeUp;
- l++;
- }
- if(LoopNum >= MaxLoopNum){
- break;
- }
- }
- }
- //update each led
- for (int i = 0; i < NUM_LEDS; i++) {
- switch (LedState[i]) {
- case Idle:
- break;
- case FadeUp:
- if (leds1[i].b <= (255 - fadeupamount)) {
- leds1[i] += CHSV((190), (0), fadesnowupamount);
- leds2[i] += CHSV((120), (70), fadesnowupamount);
- } else {
- LedState[i] = FadeDown;
- }
- break;
- case FadeDown:
- if (leds1[i].b >= (0 + fadedownamount)) {
- leds1[i] -= CHSV((190), (0), fadesnowdownamount);
- leds2[i] -= CHSV((120), (70), fadesnowdownamount);
- } else {
- LedState[i] = Idle;
- leds1[i] = CRGB(0, 0, 0);
- leds2[i] = CRGB(0, 0, 0);
- }
- break;
- }
- }
- FastLED.show();
- }
- void setup() {
- delay(1000); // sanity delay
- FastLED.addLeds<CHIPSET, LED_PIN1, COLOR_ORDER>(leds1, NUM_LEDS).setCorrection( TypicalSMD5050 );
- FastLED.addLeds<CHIPSET, LED_PIN2, COLOR_ORDER>(leds2, NUM_LEDS).setCorrection( TypicalSMD5050 );
- /*
- EEPROM.write(BRIGHT_ADDR, 50);
- EEPROM.write(RED_ADDR, 1);
- EEPROM.write(GREEN_ADDR, 1);
- EEPROM.write(BLUE_ADDR, 1);
- EEPROM.write(EFFECT_ADDR, NO_EFFECT);
- EEPROM.write(WEATHER_ADDR, CLEAR);
- */
- brightness = EEPROM.read(BRIGHT_ADDR);
- color.r = EEPROM.read(RED_ADDR);
- color.g = EEPROM.read(GREEN_ADDR);
- color.b = EEPROM.read(BLUE_ADDR);
- effect = EEPROM.read(EFFECT_ADDR);
- weather = EEPROM.read(WEATHER_ADDR);
- Serial1.begin(115200);
- inputString.reserve(200);
- }
- void loop() {
- if (effect == FIRE){
- Fire2012(leds1);
- Fire2012(leds2);
- }
- if (effect == WEATHER){
- int period0;
- int period1;
- int period2;
- int period3;
- int period4;
- int period5;
- if (weather == CLEAR || weather == CLOUDS){
- if (weather == CLEAR){
- period0 = beatsin16(3,50,255);
- period1 = beatsin16(4,50,255);
- period2 = beatsin16(5,50,255);
- period3 = beatsin16(7,50,255);
- period4 = beatsin16(11,20,255);
- period5 = beatsin16(12,40,255);
- }
- else if (weather == CLOUDS){
- period0 = beatsin16(0,100,255);
- period1 = beatsin16(0,100,255);
- period2 = beatsin16(0,100,255);
- period3 = beatsin16(0,100,255);
- period4 = beatsin16(0,100,255);
- period5 = beatsin16(0,100,255);
- }
- }
- weather_effect(leds1, period0, period1, period2, period3, period4, period5);
- weather_effect(leds2, period1, period2, period3, period4, period5, period0);
- if (weather == THUNDERSTORM || weather == RAIN){
- rain();
- if (weather == THUNDERSTORM){
- int randomThunder = random8(100);
- if (randomThunder > 98){
- thunder();
- FastLED.delay(1000 / 120);
- }
- }
- }
- else if (weather == SNOW){
- snowing();
- }
- }
- FastLED.show();
- FastLED.delay(1000 / FRAMES_PER_SECOND);
- }
- void serialEvent1() {
- while (Serial1.available()) {
- char inChar = (char)Serial1.read();
- inputString += inChar;
- if (inChar == '\n') {
- change_state(inputString);
- Serial1.print(inputString);
- inputString = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement