


// beta 3 -           // beta 4 -       ,     // beta 4 -         // beta 4 -   : /* -35 - -25  0 -25 - -15  15 -15 - 0 - 50 0 - 15 - 85 15 - 20 100  20 - 25 - 170 25 - 30 - 235 30+  254 // : if (colorV < 100) { targetR = colorV*0.05; } else { targetR = 5+(colorV-100)*1.29; } //  if (colorV < 75) { // 0 - 180 targetG = 2.4*colorV; } if (colorV > 75 && colorV < 100) { // 180 - 254 targetG = 180 + (colorV-75)*2.96; } if (colorV > 100 && colorV < 150) { // 254 targetG = 254; } if (colorV > 150 && colorV < 200) { // 254 - 154 targetG = 254 - (colorV-150)*2; } if (colorV > 200) { // 154 - 0 targetG = 154 - (colorV-200)*2.85; } //  if (colorV < 100) { targetB = 254 - colorV*2.54; } else { targetB = colorV*0.05; } */ #include <RCSwitch.h> //   SC2260/2262 http://code.google.com/p/rc-switch/ #include <livolo.h> //   Livolo #define txPin 8 //   Livolo livolo(8); //  Livolo RCSwitch mySwitch = RCSwitch(); //  RC-Switch int weatherData = 0; unsigned long dimmerDelay, timerStart, timerStop, timerSwitchStart, timerSwitchStop, timerRange; //  int prevRange, nowRange, tempRange, switchRange, vectorRange, initRange, rangeFinder, tempC; byte nightLightLevel, rainbow, delta, redB, greenB, blueB, targetL, targetR, targetG, targetB, lastValueRGB; byte lightMode = 0; //   (0 - , 1 - , 2 - , 3 -  , 4 -  ) byte lastMode = 1; //       byte valueRGB = 0; //    (  nowRange) boolean rainbowUp, followMeLight; //   boolean timing = false; //    (  ) boolean switchLock = false; //           boolean tempLock = false; //          boolean backLight = false; //     boolean mainLight = false; //     boolean nightLight = false; //    boolean tempRcvd = false; //    #define nightLightLimit 65 //   #define rangePin A0 //   #define redPin 9 //  #define greenPin 10 //  #define bluePin 11 //  #define bottom 630 //    #define top 135 //    #define delta 20 //      #define shortDelay 350 //     #define longDelay 3000 #define timeOutRange 100 //         ""   ,       #define rainbowStep 75 //     #define dimmerStep 35 //    void setup() { // Serial.begin(9600); pinMode(rangePin, INPUT); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); mySwitch.enableTransmit(txPin); //   mySwitch.enableReceive(0); //   ( 0 ->  2) rgbLight(0, 0, 0); //    lastMode = 1; //       timerRange = millis(); //       nowRange = getRange(); //    } void loop() { //    if ((millis() - timerRange) > timeOutRange) { //     timeOutRange prevRange = nowRange; //     nowRange = getRange(); //    timerRange = millis(); //      // Serial.print("Range: "); // Serial.println(nowRange); } //   --      ( ),   -   ( ) if ((nowRange < bottom + delta) && (nowRange > top)) { //         if (nowRange > bottom - delta) { //    ""         (2*delta) valueRGB = 0; } else { valueRGB = 254 - map(nowRange, top, bottom, 0, 254); //      0 - 254  "" } // Serial.print("valueRGB: "); // Serial.println(valueRGB); //         if (lightMode == 1) { //    if (valueRGB > 70 && valueRGB < 110) { //       followMeLight = true; //      ,     lastValueRGB = valueRGB; //     rgbLight(0, 255, 0); //       ""  // Serial.println("Set Green to 255"); } else { if (valueRGB > 175 && valueRGB < 215) { //       followMeLight = true; lastValueRGB = valueRGB; //     rgbLight(0, 0, 255); //       ""  // Serial.println("Set Blue to 255"); } else { if (valueRGB > 235) { //      followMeLight = true; lastValueRGB = valueRGB; //     rgbLight(valueRGB, 0, 0); //        // Serial.println("Set RED"); } else { lastValueRGB = valueRGB; followMeLight = false; } } } } //    if (timing == false) { //     timerStart = millis(); //      timerSwitchStart = millis(); //      timing = true; //    } if (timing == true) { //        (  ) //        tempRange = nowRange - prevRange; //         if (abs(tempRange) > delta) { //     timerStart = millis(); //     } if ((millis() - timerStart) > longDelay) { //          //   if (lightMode == 0 && switchLock == false) { //     -      lightMode = lastMode; switchLock = true; //        } //   (        ) if (lightMode == 1 && switchLock == false) { //    //    if ((nowRange > (bottom/2)) && (nowRange < (bottom - 2*delta))) { //      ,     +4*delta // // Serial.print("Backlight mode (nowRange): "); // // Serial.println(nowRange); // // Serial.print("backLight:"); // // Serial.println(backLight); if (backLight == false) { //     //    mySwitch.send(863029, 24); //   backLight = true; switchLock = true; // // Serial.println("Backlight ON"); // // Serial.print("backLight:"); // // Serial.println(backLight); } else { //     //    mySwitch.send(863028, 24); //   backLight = false; switchLock = true; // // Serial.println("Backlight OFF"); // // Serial.print("backLight:"); // // Serial.println(backLight); } } //    if ((nowRange < (bottom/2)) && (nowRange > top)) { //      ,     /* // Serial.print("Main light mode (nowRange): "); // Serial.println(nowRange); // Serial.print("mainLight:"); // Serial.println(mainLight); */ if (mainLight == false) { //     //    livolo.sendButton(8500, 0); //    livolo.sendButton(8500, 96); //    mainLight = true; switchLock = true; /* // Serial.println("Main light ON"); // Serial.print("mainLight:"); // Serial.println(mainLight); */ } else { //     //    livolo.sendButton(8500, 0); //    livolo.sendButton(8500, 96); //    mainLight = false; switchLock = true; /* // Serial.println("Main light OFF"); // Serial.print("mainLight:"); // Serial.println(mainLight); */ } } } //        if (nowRange > (bottom - delta)) { //     (2*delta) // Serial.print("Full off mode (nowRange): "); // Serial.println(nowRange); if (lightMode > 0 && switchLock == false) { //      blinkLight(150); rgbLight(0, 0, 0); //   livolo.sendButton(8500, 106); //    mySwitch.send(863028, 24); //   mainLight = false; backLight = false; lastMode = lightMode; //    lightMode = 0; //   switchLock = true; //      } } } // > longDelay } // timing = true //    if (lightMode == 1) { if (followMeLight == false) { //      ,     // Serial.println("Brightness control"); rgbLight(valueRGB, valueRGB, valueRGB); } } //   if (lightMode == 3) { if (valueRGB > 0) { //   colorRGB(valueRGB); } else { rgbLight(0, 0, 0); //       } } } // nowRange > bottom //     ,    if ((nowRange < (top + delta))) { if (timing == true) { //       if (lightMode == 1) { rgbLight(lastValueRGB, lastValueRGB, lastValueRGB); //          followMeLight = false; } unsigned long millistart = millis() - timerSwitchStart; if (millistart < shortDelay) { //     //     if (lightMode == 0 && switchLock == false) { //    if (nightLight == false) { //    nightLightLevel = 0; nightLight = true; //   } else { nightLightLevel = nightLightLimit; nightLight = false; //   } dimmerDelay = millis(); //      } //   if (lightMode > 0) { //    lightMode = lightMode + 1; //   blinkLight(150); if (lightMode == 4) { //    dimmerDelay = millis(); //      if (nowRange < 255) { //     rainbow = nowRange; rainbowUp = true; } else { rainbow = 254; rainbowUp = false; //   } } if (lightMode == 3) { //    -    colorRGB(valueRGB); } if (lightMode == 2) { if (tempRcvd == true) { colorRGB(tempC); //         } else { noTemp(); } } if (lightMode > 4) { //    lightMode = 1; } } } // < longDelay } // timing = true timing = false; switchLock = false; //            } // nowRange < bottom + delta if (nightLight == true && nightLightLevel < nightLightLimit) { //   ,     if ((millis() - dimmerDelay) > dimmerStep) { //     dimmerStep  nightLightLevel++; rgbLight(nightLightLevel, nightLightLevel, nightLightLevel); dimmerDelay = millis(); //      } } if (nightLight == false && nightLightLevel > 0) { //   ,     if ((millis() - dimmerDelay) > dimmerStep) { nightLightLevel--; rgbLight(nightLightLevel, nightLightLevel, nightLightLevel); dimmerDelay = millis(); //      } } //   if (lightMode == 2) { if (valueRGB > 0) { //        if (tempRcvd == true) { if (tempLock == false) { //       colorRGB(tempC); //         tempLock = true; //        } else { noTemp(); } } } else { rgbLight(0, 0, 0); tempLock = false; } } //  if (lightMode == 4) { if (valueRGB > 0) { //        if ((millis() - dimmerDelay) > rainbowStep) { // Serial.print("Rainbow value: "); // Serial.println(rainbow); if (rainbowUp == true) { if (rainbow < 254) { rainbow++; } else { rainbowUp = false; } } if (rainbowUp == false) { if (rainbow > 1) { rainbow--; } else { rainbowUp = true; } } colorRGB(rainbow); dimmerDelay = millis(); } } else { rgbLight(0, 0, 0); } } if (mySwitch.available()) { //     int value = mySwitch.getReceivedValue(); if (value != 0) { //     if (lightMode == 0) { if (mySwitch.getReceivedValue() == 863029) { lightMode = 1; rgbLight(254, 254, 254); switchLock = true; } } //     if (mySwitch.getReceivedValue() == 863028) { rgbLight(0, 0, 0); //   mainLight = false; //        backLight = false; if (lightMode == 0) { lastMode = 1; } else { lastMode = lightMode; //    } lightMode = 0; //   switchLock = true; //      } if (mySwitch.getReceivedValue()/100000 == 161) { weatherData = mySwitch.getReceivedValue() - 16100000; if (weatherData < 10000) { //    if (weatherData > 1000) { //   if (weatherData > 1250) { //   -25 tempC = 0; } if (weatherData < 1250 && weatherData > 1150) { //  -25 -- -15C tempC = 15; } if (weatherData < 1150) { //  -15C -- 0C tempC = 50; } } else { //   if (weatherData > 300) { //    +30 tempC = 254; } if (weatherData > 250 && weatherData < 300) { //   +25C -- +30 tempC = 235; } if (weatherData > 200 && weatherData < 250) { //   +25C -- +30 tempC = 170; } if (weatherData > 150 && weatherData < 200) { //   +15C -- +25 tempC = 100; } if (weatherData < 150) { //    +15 tempC = 85; } } tempRcvd = true; tempLock = false; //     // tempC = tempC*0.363; //      0  255 // Serial.print("TempC: "); // Serial.println(tempC); } } mySwitch.resetAvailable(); } } } // loop void rgbLight(byte redL, byte greenL, byte blueL) { analogWrite(redPin, redL); analogWrite(greenPin, greenL); analogWrite(bluePin, blueL); } void colorRGB(int colorV) { // : if (colorV < 100) { targetR = colorV*0.05; } else { targetR = 5+(colorV-100)*1.29; } //  if (colorV < 75) { // 0 - 180 targetG = 2.4*colorV; } if (colorV > 75 && colorV < 100) { // 180 - 254 targetG = 180 + (colorV-75)*2.96; } if (colorV > 100 && colorV < 150) { // 254 targetG = 254; } if (colorV > 150 && colorV < 200) { // 254 - 154 targetG = 254 - (colorV-150)*2; } if (colorV > 200) { // 154 - 0 targetG = 154 - (colorV-200)*2.85; } //  if (colorV < 100) { targetB = 254 - colorV*2.54; } else { targetB = colorV*0.05; } rgbLight(targetR, targetG, targetB); } void blinkLight(byte spark) { for (byte i = 0; i < 3; i++) { rgbLight(0, 0, 0); delay(75); rgbLight(spark, spark, spark); delay(75); } } unsigned int getRange(){ byte i; unsigned int rangeFinder = 0; for (i = 0; i < 100; i++) { rangeFinder = rangeFinder + analogRead(rangePin); } rangeFinder = rangeFinder/100; return rangeFinder; } //    void noTemp() { rgbLight(254, 0, 0); //    delay(500); rgbLight(0, 254, 0); delay(500); rgbLight(0, 0, 254); delay(500); blinkLight(150); //   lightMode = 3; //     } Source: https://habr.com/ru/post/243037/
All Articles