Re: fade

z klima, 6 miesiące temu, napisane w C++, wyświetlone 81 razy. [paste_expire] 4 miesiące. Ta wklejka jest odpowiedzią na fade z klima - Pokaż różnice
URL https://pastebin.k4be.pl/view/3293658d Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1.  
  2. #include <Arduino.h>
  3. #include <ArduinoJson.h>
  4. //needed for library
  5. #include <DNSServer.h>
  6. #include <ESP8266WebServer.h>
  7. #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
  8. #include <EEPROM.h>
  9. //ota
  10. #include <NTPClient.h>
  11. #include <WiFiUdp.h>
  12. #include "ArduinoOTA.h"
  13. #include <Adafruit_NeoPixel.h>
  14.  
  15. /*
  16.  
  17. upload_protocol = espota
  18. upload_port = 192.168.1.32
  19. ;upload_speed = 921600
  20. monitor_speed = 9600
  21.  
  22. */
  23.  
  24. //NEOPIXEL
  25. #define PIN D3
  26. #define NUMPIXELS 59
  27. Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);
  28.  
  29. //NTP TIME
  30. WiFiUDP ntpUDP;
  31. NTPClient timeClient(ntpUDP, "ntp.task.gda.pl", 3600, 1800000);
  32. #define TIME_OFFSET +1
  33. #define SUMMER_TIME_OFFSET +2
  34.  
  35. // I/O
  36. int ledPin = LED_BUILTIN;
  37.  
  38.  
  39. //TIMER`S LOOP
  40. int period = 1000;
  41. unsigned long time_now = 0;
  42. int periodd = 100;
  43. unsigned long time_noww = 0;
  44. int periodd_f = 1;
  45. unsigned long time_noww_f = 0;
  46.  
  47.  
  48. //UPTIME
  49. byte hh=0; byte mi=0; byte ss=0; // hours, minutes, seconds
  50. unsigned int dddd=0; // days (for if you want a calendar)
  51. unsigned long lastTick=0; // time that the clock last "ticked"
  52. char timestring[25]; // for output
  53.  
  54. //JSON SERVER
  55. WiFiServer server(80);
  56. String header;
  57. bool web_access_led;
  58. byte web_led_bli;
  59. unsigned long currentTime = millis();
  60. unsigned long previousTime = 0;
  61. const long timeoutTime = 2000;
  62. bool error_get_json;
  63. byte error_get_json_cnt;
  64.  
  65. //czas z ntp
  66. byte godzina,minuta,sekunda, weekDay;
  67.  
  68. //zmienne do fade
  69. byte pora, pora_tmp;
  70. byte swit_r, swit_g, swit_b, swit_w;
  71. byte ranek_r, ranek_g, ranek_b, ranek_w;
  72. byte poludnie_r, poludnie_g, poludnie_b, poludnie_w;
  73. byte popoludnie_r, popoludnie_g, popoludnie_b, popoludnie_w;
  74. byte wieczor_r, wieczor_g, wieczor_b, wieczor_w;
  75. byte noc_r, noc_g, noc_b, noc_w, noc_b_max, noc_b_lock;
  76.  
  77. //zmienne pory
  78. byte swit_start, ranek_start, poludnie_start, popoludnie_start, wieczor_start, noc_start;
  79. byte swit_start_min, ranek_start_min, poludnie_start_min, popoludnie_start_min, wieczor_start_min, noc_start_min;
  80. unsigned int swit_fade_time, ranek_fade_time, poludnie_fade_time, popoludnie_fade_time, wieczor_fade_time, noc_fade_time;
  81. byte ii;
  82. byte swit_leds,noc_leds;
  83. byte leds_count;
  84.  
  85. //cycle time test
  86. int loop_counter;             //holds the count for every loop pass
  87. long loop_timer_now;          //holds the current millis
  88. long previous_millis;         //holds the previous millis
  89. float loop_time;              //holds difference (loop_timer_now - previous_millis) = total execution time
  90. int loop_test_times = 20000;  //Run loop 20000 times then calculate time
  91.  
  92. //zmienne FADE - RGBW
  93. byte r_in, g_in, b_in , w_in;
  94. byte r_out, g_out, b_out, w_out;
  95. unsigned int set_fade_time; //sec
  96. bool fade_init, fade_init_cycle;
  97. unsigned int fade_ptc, ptc_r, ptc_g, ptc_b, ptc_w;
  98. bool case_6_lock, case_1_lock, case_tick, case_tick_tmp, case_cnt;
  99.  
  100. //test
  101. byte led1, led2;
  102.  
  103. #define TICK_MS 100 //msec
  104. #define INIT_FADE 10 //sec
  105.  
  106. //FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S
  107. //FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S FUNCTION`S
  108.  
  109. //jakosc wifi
  110. int getQuality() {
  111.   if (WiFi.status() != WL_CONNECTED)
  112.     return -1;
  113.   int dBm = WiFi.RSSI();
  114.   if (dBm <= -100)
  115.     return 0;
  116.   if (dBm >= -50)
  117.     return 100;
  118.   return 2 * (dBm + 100);
  119. }
  120.  
  121.  
  122. unsigned char calc_weekday(struct tm *ct) {
  123.   const unsigned char mi[] = {3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
  124.         unsigned char w, x;
  125.  
  126.         x = ct->tm_year - 100;
  127.         w = x%7;
  128.         if(x != 0) {            /* if year is not 2000 */
  129.                 /* Count only past years, for example if current year is 2004, count only 2000-2003, so x-- */
  130.                 x--;
  131.                 w += x >> 2;    /* add number of years which are divisible by 4 4 */
  132.                 w -= x/100;             /* subtract number of years which are divisible by 100 */
  133.                 w++;                    /* add the year 2000 which is divisible by 400 */
  134.         }
  135.                         /* Add first weekday (Saturday 2000-01-01) */
  136.         w+=4;
  137.        
  138. /* Months */
  139.         if(ct->tm_mon+1 > 1) {          /* if it's not January */
  140.                 w += mi[ct->tm_mon+1 - 2];      /* add array increment */
  141.         }
  142. /* Days */
  143.         w += ct->tm_mday;
  144.  
  145. /*
  146. ** If current month is March or later, and it's a leap year, this means February
  147. ** was 29 days so we have to add 1.
  148. */
  149.         if(ct->tm_mon+1 > 2)
  150.                 if(!(ct->tm_year&3)) w+=1;
  151.  
  152. /* Result modulo 7 */
  153.         return (w%7)+1; /* 1 = Monday, 7 = Sunday */
  154. }
  155.  
  156.  
  157. signed int local_time_diff(time_t time) {
  158.         signed char offset = TIME_OFFSET;
  159.         unsigned char t;
  160.         struct tm *ct = gmtime(&time);
  161.         unsigned char weekday = calc_weekday(ct);
  162.  
  163.         if (ct->tm_mon+1 == 3 || ct->tm_mon+1 == 10){
  164.                 t = 35 + weekday - ct->tm_mday;
  165.                 t += 31;
  166.                 t = t%7;
  167.                 t = 31-t;
  168.                 if (ct->tm_mon+1 == 3){ // March
  169.                         if (ct->tm_mday==t) { // last Sunday
  170.                                 if (ct->tm_hour >= 2 )
  171.                                         offset = SUMMER_TIME_OFFSET;
  172.                         } else if (ct->tm_mday > t) { // after last Sunday
  173.                                 offset = SUMMER_TIME_OFFSET;
  174.                         }
  175.                 } else { // October
  176.                         if (ct->tm_mday == t) { // last Sunday
  177.                                 if(ct->tm_hour < 2)
  178.                                         offset = SUMMER_TIME_OFFSET;
  179.                         } else if (ct->tm_mday < t) { // before last Sunday
  180.                                 offset = SUMMER_TIME_OFFSET;
  181.                         }
  182.                 }
  183.         } else if (ct->tm_mon+1 > 3 && ct->tm_mon+1 < 10) {
  184.                 offset = SUMMER_TIME_OFFSET;
  185.         }
  186.         return offset;
  187. }
  188.  
  189. //pobieranie JSON
  190. void pobieranie_json(){
  191.  
  192.   // Connect to HTTP server
  193.   WiFiClient client;
  194.   client.setTimeout(10000);
  195.   if (!client.connect("192.168.1.101", 80)) {
  196.     Serial.println(F("Connection failed"));
  197.     error_get_json = true;
  198.     return;
  199.   }
  200.  
  201.   //Serial.println(F("Connected!"));
  202.  
  203.   // Send HTTP request
  204.   client.println(F("GET /~klima/miaqua.json HTTP/1.0"));
  205.   client.println(F("Host: 192.168.1.101"));
  206.   client.println(F("Connection: close"));
  207.   if (client.println() == 0) {
  208.     Serial.println(F("Failed to send request"));
  209.     client.stop();
  210.     error_get_json = true;
  211.     return;
  212.   }
  213.  
  214.   // Check HTTP status
  215.   char status[32] = {0};
  216.   client.readBytesUntil('\r', status, sizeof(status));
  217.   if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
  218.     Serial.print(F("Unexpected response: "));
  219.     Serial.println(status);
  220.     client.stop();
  221.     error_get_json = true;
  222.     return;
  223.   }
  224.  
  225.   // Skip HTTP headers
  226.   char endOfHeaders[] = "\r\n\r\n";
  227.   if (!client.find(endOfHeaders)) {
  228.     Serial.println(F("Invalid response"));
  229.     client.stop();
  230.     error_get_json = true;
  231.     return;
  232.   }
  233.  
  234.  
  235.   DynamicJsonDocument doc(1536);
  236.  
  237.   // Parse JSON object
  238.   DeserializationError error = deserializeJson(doc, client);
  239.   if (error) {
  240.     Serial.print(F("deserializeJson() failed: "));
  241.     Serial.println(error.f_str());
  242.     client.stop();
  243.     error_get_json = true;
  244.     return;
  245.   }
  246.   else
  247.   {error_get_json = false;}
  248.  
  249.   // Extract values
  250.   if(error_get_json == false){
  251.   //Serial.println(F("Response:"));
  252.   //Serial.println(doc["swit_start"].as<byte>());
  253.   }
  254.    
  255.   //nastawy
  256.  
  257.   if(error_get_json == false){
  258.    
  259.  error_get_json_cnt = 0;
  260.  
  261.  swit_start =     doc["swit_start"].as<byte>();
  262.  swit_start_min = doc["swit_start_min"].as<byte>();
  263.  swit_leds =      doc["swit_leds"].as<byte>();
  264.  swit_fade_time = doc["swit_fade"].as<unsigned int>();
  265.  swit_r =         doc["swit_r"].as<byte>();
  266.  swit_g =         doc["swit_g"].as<byte>();
  267.  swit_b =         doc["swit_b"].as<byte>();
  268.  swit_w =         doc["swit_w"].as<byte>();
  269.  
  270.  ranek_start = doc["ranek_start"].as<byte>();
  271.  ranek_start_min = doc["ranek_start_min"].as<byte>();
  272.  ranek_fade_time = doc["ranek_fade"].as<unsigned int>();
  273.  ranek_r = doc["ranek_r"].as<byte>();
  274.  ranek_g = doc["ranek_g"].as<byte>();
  275.  ranek_b = doc["ranek_b"].as<byte>();
  276.  ranek_w = doc["ranek_w"].as<byte>();
  277.  
  278.  poludnie_start = doc["poludnie_start"].as<byte>();
  279.  poludnie_start_min = doc["poludnie_start_min"].as<byte>();
  280.  poludnie_fade_time = doc["poludnie_fade"].as<unsigned int>();
  281.  poludnie_r = doc["poludnie_r"].as<byte>();
  282.  poludnie_g = doc["poludnie_g"].as<byte>();
  283.  poludnie_b = doc["poludnie_b"].as<byte>();
  284.  poludnie_w = doc["poludnie_w"].as<byte>();
  285.  
  286.  popoludnie_start = doc["popoludnie_start"].as<byte>();
  287.  popoludnie_start_min = doc["popoludnie_start_min"].as<byte>();
  288.  popoludnie_fade_time = doc["popoludnie_fade"].as<unsigned int>();
  289.  popoludnie_r = doc["popoludnie_r"].as<byte>();
  290.  popoludnie_g = doc["popoludnie_g"].as<byte>();
  291.  popoludnie_b = doc["popoludnie_b"].as<byte>();
  292.  popoludnie_w = doc["popoludnie_w"].as<byte>();
  293.  
  294.  wieczor_start = doc["wieczor_start"].as<byte>();
  295.  wieczor_start_min = doc["wieczor_star_min"].as<byte>();
  296.  wieczor_fade_time = doc["wieczor_fade"].as<unsigned int>();
  297.  wieczor_r = doc["wieczor_r"].as<byte>();
  298.  wieczor_g = doc["wieczor_g"].as<byte>();
  299.  wieczor_b = doc["wieczor_b"].as<byte>();
  300.  wieczor_w = doc["wieczor_w"].as<byte>();
  301.  
  302.  noc_start = doc["noc_start"].as<byte>();
  303.  noc_start_min = doc["noc_start_min"].as<byte>();;
  304.  noc_leds = doc["noc_leds"].as<byte>();
  305.  noc_fade_time = doc["noc_fade"].as<unsigned int>();
  306.  noc_r = doc["noc_r"].as<byte>();
  307.  noc_g = doc["noc_g"].as<byte>();
  308.  noc_b = doc["noc_b"].as<byte>();
  309.  noc_b_max = doc["noc_b_max"].as<byte>();
  310.  noc_w = doc["noc_w"].as<byte>();
  311.  
  312.   }
  313.  
  314.  
  315. //kiedy nie mozna odczytac ustawien
  316. if(error_get_json == true){
  317.   error_get_json_cnt = error_get_json + 1;
  318.   if(error_get_json_cnt >= 250){
  319.     error_get_json_cnt = 250;
  320.   }
  321. }
  322.  
  323.   //nastawy
  324.  if(error_get_json_cnt == 250){
  325.  swit_start = 6;
  326.  swit_start_min = 0;
  327.  swit_fade_time = 300;
  328.  swit_r = 20;
  329.  swit_g = 20;
  330.  swit_b = 0;
  331.  swit_w = 0;
  332.  
  333.  ranek_start = 8;
  334.  ranek_start_min = 0;
  335.  ranek_fade_time = 300;
  336.  ranek_r = 30;
  337.  ranek_g = 0;
  338.  ranek_b = 30;
  339.  ranek_w = 0;
  340.  
  341.  poludnie_start = 11;
  342.  poludnie_start_min = 0;
  343.  poludnie_fade_time = 300;
  344.  poludnie_r = 50;
  345.  poludnie_g = 50;
  346.  poludnie_b = 50;
  347.  poludnie_w = 50;
  348.  
  349.  popoludnie_start = 16;
  350.  popoludnie_start_min = 0;
  351.  popoludnie_fade_time = 300;
  352.  popoludnie_r = 40;
  353.  popoludnie_g = 40;
  354.  popoludnie_b = 40;
  355.  popoludnie_w = 25;
  356.  
  357.  wieczor_start = 19;
  358.  wieczor_start_min = 00;
  359.  wieczor_fade_time = 300;
  360.  wieczor_r = 30;
  361.  wieczor_g = 30;
  362.  wieczor_b = 30;
  363.  wieczor_w = 0;
  364.  
  365.  noc_start = 22;
  366.  noc_start_min = 0;
  367.  noc_fade_time = 300;
  368.  noc_r = 0;
  369.  noc_g = 0;
  370.  noc_b = 10;
  371.  noc_b_max = 40;
  372.  noc_w = 0;
  373.  }
  374.  
  375.  
  376.  
  377.   // Disconnect
  378.   client.stop();
  379.  
  380. }
  381. //JSON POBIERANIE KONIEC
  382.  
  383. //FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE  
  384.  
  385. /*
  386. unsigned int fade_time = 100;
  387.  
  388. struct fade_state {
  389.        uint32_t tick_current;
  390.        uint8_t from, to;
  391.     };
  392.      
  393.     void fade_start(struct fade_state *state, uint8_t to) {
  394.        uint32_t v;
  395.        v = (fade_time - state->tick_current) * (state->from);
  396.        v += (state->tick_current) * (state->to);
  397.        state->tick_current = 0;
  398.        state->from = v / fade_time;
  399.        state->to = to;
  400.     }
  401.      
  402.     void fade_reset(struct fade_state *state, uint8_t current) {
  403.        state->tick_current = fade_time;
  404.        state->from = current;
  405.        state->to = current;
  406.     }
  407.      
  408.     uint8_t fade(struct fade_state *state) {
  409.        if(state->tick_current < fade_time) {
  410.            uint32_t v;
  411.            state->tick_current++;
  412.            v = (fade_time - state->tick_current) * (state->from);
  413.            v += (state->tick_current) * (state->to);
  414.            return v / fade_time;
  415.        }
  416.      
  417.        return state->to;
  418.     }
  419.  
  420.     uint8_t fade_percent(struct fade_state *state) {
  421.           return (state->tick_current) * 100 / fade_time;
  422.     }
  423.  
  424.   */
  425.   struct fade_state {
  426.        uint32_t tick_current;
  427.        uint32_t fade_ticks;
  428.        uint8_t from, to;
  429.     };
  430.      
  431.     void fade_start(struct fade_state *state, uint8_t to, uint32_t fade_ticks) {
  432.        uint32_t v;
  433.        v = (state->fade_ticks - state->tick_current) * (state->from);
  434.        v += (state->tick_current) * (state->to);
  435.        state->from = v / (state->fade_ticks);
  436.        state->tick_current = 0;
  437.        state->fade_ticks = fade_ticks;
  438.        state->to = to;
  439.     }
  440.      
  441.     void fade_reset(struct fade_state *state, uint8_t current) {
  442.        state->fade_ticks = 1;
  443.        state->tick_current = state->fade_ticks;
  444.        state->from = current;
  445.        state->to = current;
  446.     }
  447.      
  448.     uint8_t fade(struct fade_state *state) {
  449.        if(state->tick_current < (state->fade_ticks)) {
  450.            uint32_t v;
  451.            state->tick_current++;
  452.            v = (state->fade_ticks - state->tick_current) * (state->from);
  453.            v += (state->tick_current) * (state->to);
  454.            return v / (state->fade_ticks);
  455.        }
  456.      
  457.        return state->to;
  458.     }    
  459.  
  460.      uint8_t fade_percent(struct fade_state *state) {
  461.        
  462.         return(state->tick_current) * 100 / state->fade_ticks;
  463.      
  464.      }
  465.  
  466.        struct fade_state fade_a = {0, 0, 0, 0};
  467.        struct fade_state fade_b = {0, 0, 0, 0};
  468.        struct fade_state fade_c = {0, 0, 0, 0};
  469.        struct fade_state fade_d = {0, 0, 0, 0};
  470.  
  471.  
  472. //FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE
  473. //FADE KONIEC
  474.  
  475. //SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  476. //SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  477. //SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  478.  
  479. void setup() {
  480.  
  481.  
  482.  
  483.    Serial.begin(9600);
  484.  
  485.  
  486.  
  487.    // Initialize the output variables as outputs
  488.    // pinMode(D5, OUTPUT);
  489.    // pinMode(D6, OUTPUT);
  490.     // Set outputs to LOW
  491.    // digitalWrite(D5, LOW);
  492.    // digitalWrite(D6, LOW);
  493.  
  494.    pinMode(LED_BUILTIN, OUTPUT);
  495.    digitalWrite(LED_BUILTIN, LOW);
  496.    delay(100);
  497.    digitalWrite(LED_BUILTIN, HIGH);
  498.    delay(100);
  499.    digitalWrite(LED_BUILTIN, LOW);
  500.    delay(100);
  501.    digitalWrite(LED_BUILTIN, HIGH);
  502.    delay(100);
  503.    digitalWrite(LED_BUILTIN, LOW);
  504.    delay(100);
  505.    digitalWrite(LED_BUILTIN, HIGH);
  506.    delay(100);
  507.    digitalWrite(LED_BUILTIN, LOW);
  508.    
  509.  
  510.   //WIFI MANAGER:
  511.   //Lokalnie inicjalizowana, potrzebna tylko kiedy potrzebna.
  512.   WiFiManager wifiManager;
  513.   //kasowanie ustawien
  514.   //ESP.eraseConfig();
  515.   //wifiManager.resetSettings();
  516.   //Czas po jakim wylaczy sie autokonfiguracja gdy nie zostanie nic skonfigurowane!
  517.   wifiManager.setTimeout(180);
  518.   //Wyjdz z konfiguracji wifi kiedy jest gotowe!
  519.   wifiManager.setBreakAfterConfig(true);
  520.  // jak potrzrbne z haslem -> (!wifiManager.autoConnect("HUBERT_ESP", "hubert"))
  521.  //Tutaj ustawiamy nazwe i haslo do portalu z ustawieniami wifi
  522.   if (!wifiManager.autoConnect("MINIAQUARIUM_ESP")) {
  523.     Serial.println("failed to connect, we should reset as see if it connects");
  524.     delay(3000);
  525.     ESP.reset();
  526.     delay(5000);
  527.   }
  528.  
  529.  
  530.  //ota
  531.   ArduinoOTA.onStart([]() {
  532.     String type;
  533.     if (ArduinoOTA.getCommand() == U_FLASH)
  534.       type = "sketch";
  535.     else // U_SPIFFS
  536.       type = "filesystem";
  537.  
  538.     // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  539.     Serial.println("Start updating " + type);
  540.   });
  541.   ArduinoOTA.onEnd([]() {
  542.     Serial.println("\nEnd");
  543.   });
  544.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  545.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  546.   });
  547.   ArduinoOTA.onError([](ota_error_t error) {
  548.     Serial.printf("Error[%u]: ", error);
  549.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  550.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  551.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  552.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  553.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  554.   });
  555.   ArduinoOTA.begin();
  556.  
  557.  
  558.   if (WiFi.status() == WL_CONNECTED){
  559.   digitalWrite(LED_BUILTIN, HIGH);  
  560.   }
  561.  
  562.  
  563.  //odpalenie servera web / json
  564.    server.begin();
  565.  
  566.  //pobranie daty / godziny
  567.  timeClient.setTimeOffset(3600);
  568.  timeClient.begin();
  569.  
  570.  //NEOPIX
  571.  strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  572.  strip.clear(); // Set all pixel colors to 'off'
  573.  
  574.  fade_reset(&fade_a, 0);
  575.  fade_reset(&fade_b, 0);
  576.  fade_reset(&fade_c, 0);
  577.  fade_reset(&fade_d, 0);
  578.  
  579. }
  580.  
  581. //LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
  582. //LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
  583. //LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
  584.  
  585.  
  586.  
  587. void loop(void) {
  588.  
  589. long start = micros();
  590. long duration = micros() - start;
  591.  
  592. //ota
  593. ArduinoOTA.handle();
  594. timeClient.update();
  595.  
  596. //co 1ms
  597.  
  598. if((unsigned long)(millis() - time_noww_f) > periodd_f){
  599.         time_noww_f = millis();
  600.  
  601. //uwaga to nie jest co 1 ms, niewyrabia sie ! jest 2~3ms!!!
  602.  
  603.  
  604. //
  605. }//koniec co 1 ms
  606.  
  607.  
  608. //co 100ms
  609. if((unsigned long)(millis() - time_noww) > periodd){
  610.         time_noww = millis();
  611.  
  612.      
  613.  
  614. //FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE FADE
  615.  
  616.  
  617.  r_out = fade(&fade_a);
  618.  g_out = fade(&fade_b);
  619.  b_out = fade(&fade_c);
  620.  w_out = fade(&fade_d);
  621.  
  622.  if(pora != 0 ){ fade_ptc = (fade_percent(&fade_a) + fade_percent(&fade_b) + fade_percent(&fade_c) + fade_percent(&fade_d)) / 4; }
  623.  
  624.  
  625.  
  626. //FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC FADE KONIEC
  627.  
  628.  
  629.  if(web_access_led == true){
  630.   digitalWrite(LED_BUILTIN, LOW);
  631.   web_led_bli = web_led_bli + 1;
  632.   }
  633.   if(web_led_bli > 1){
  634.   digitalWrite(LED_BUILTIN, HIGH);
  635.   web_led_bli = 0;
  636.   web_access_led = false;
  637.   }
  638.     if(leds_count == 0){ leds_count = 1;}
  639.  
  640.     //R/G/B/W
  641.    uint32_t highe = strip.Color(0, 0, 0 , 0);
  642.     strip.clear();
  643.    
  644.     for(ii = 0; ii < NUMPIXELS ; ii += leds_count){
  645.         strip.setPixelColor(ii, highe);
  646.     }  
  647.  
  648.   //strip.setPixelColor(3, 0, 0, 0, 0);
  649.   strip.show();
  650.  
  651.    
  652. //SERIAL DEBUG /SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG  DEBUG /SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG DEBUG /SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG DEBUG /SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG
  653. //czytanie znaqu
  654.  
  655. if (Serial.available() > 0) {
  656.  
  657. byte incomingbyte = Serial.read(); // read the incoming byte:
  658. Serial.print("Przeczytalem: ");
  659. Serial.println(incomingbyte);
  660.  
  661. if(incomingbyte == 49){Serial.println("PORA 1 "); pora = 1;}
  662. if(incomingbyte == 50){Serial.println("PORA 2 "); pora = 2;}
  663. if(incomingbyte == 51){Serial.println("PORA 3 "); pora = 3;}
  664. if(incomingbyte == 52){Serial.println("PORA 4 "); pora = 4;}
  665. if(incomingbyte == 53){Serial.println("PORA 5 "); pora = 5;}
  666. if(incomingbyte == 54){Serial.println("PORA 6 "); pora = 6;}
  667.  
  668. }
  669.  
  670.  
  671.  
  672.  //DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC DEBUG KONIEC
  673.  
  674.  
  675. }//co 100ms koniec
  676.  
  677. //co sekunde
  678. if((unsigned long)(millis() - time_now) > period){
  679.         time_now = millis();
  680.    
  681.    //listowanie data / godzina
  682.  
  683.   //Serial.print("Czas NTP: ");
  684.   //Serial.println(timeClient.getFormattedTime());
  685.  
  686.  
  687.    weekDay = timeClient.getDay();
  688.  // Serial.print("Dzien Tygodnia: ");
  689.  // Serial.println(weekDay);  
  690.  
  691.   sekunda = timeClient.getSeconds();
  692.   minuta = timeClient.getMinutes();
  693.   //czas letni - zimowy
  694.   if(local_time_diff(timeClient.getEpochTime()) == 2){
  695.    godzina = timeClient.getHours() + 1;
  696.    }
  697.  
  698.   if(local_time_diff(timeClient.getEpochTime()) == 1){
  699.    godzina = timeClient.getHours() + 0;
  700.    }
  701.  
  702.  //Serial.print("Czas korygowany: ");
  703.  //Serial.print(godzina);Serial.print(":");Serial.print(minuta);Serial.print(":");Serial.print(sekunda);
  704.  //Serial.println(" ");
  705.  
  706.   pobieranie_json();
  707.  
  708.  
  709. //przyspiesznie na weekend
  710. if((weekDay == 5 and godzina > 12) or (weekDay == 6 and godzina < 21)){
  711.   popoludnie_start = popoludnie_start + 1;
  712.   wieczor_start = wieczor_start + 1;
  713.   noc_start = noc_start + 1;
  714. }
  715.  
  716. /*
  717. //inicjalizacja
  718. if(  (godzina < swit_start) && (pora == 0)     )                                          {pora = 6;}
  719. if(  (godzina <= swit_start) and (godzina < ranek_start) && (pora == 0)   )               {pora = 1;}
  720. if(  (godzina >= ranek_start) and (godzina < poludnie_start) && (pora == 0) )             {pora = 2;}
  721. if(  (godzina >= poludnie_start) and (godzina < popoludnie_start ) && (pora == 0) )       {pora = 3;}
  722. if(  (godzina >= popoludnie_start) and (godzina < wieczor_start ) && (pora == 0)   )      {pora = 4;}
  723. if(  (godzina >= wieczor_start ) and (godzina < noc_start ) && (pora == 0)    )           {pora = 5;}
  724. if(  (godzina >= noc_start ) && (pora == 0) )                                             {pora = 6;}
  725.  
  726. //ustalenie pory
  727. if( (swit_start  == godzina) && (swit_start_min == minuta)  )             {pora = 1;}
  728. if( (ranek_start  == godzina) && (ranek_start_min == minuta))             {pora = 2;}
  729. if( (poludnie_start  == godzina) && (poludnie_start_min == minuta))       {pora = 3;}
  730. if( (popoludnie_start  == godzina) && (popoludnie_start_min == minuta))   {pora = 4;}
  731. if( (wieczor_start  == godzina) && (wieczor_start_min == minuta))         {pora = 5;}
  732. if( (noc_start  == godzina) && (noc_start_min == minuta)    )             {pora = 6;}
  733. */
  734.  
  735.  
  736.  
  737. if(pora != pora_tmp){
  738.   case_tick = true;
  739.   pora_tmp = pora;
  740. }
  741.  
  742. if(case_6_lock == true and pora == 6) { pora = 7; }
  743. if(case_1_lock == true and pora == 1) { pora = 8; }
  744.  
  745. //tutaj case`y
  746. switch(pora){
  747.    
  748.    case 1:
  749.         set_fade_time = swit_fade_time;
  750.         set_fade_time = 30;        
  751.         r_in = swit_r;
  752.         g_in = swit_g;
  753.         b_in = swit_b;
  754.         w_in = 0;
  755.         case_6_lock = false;
  756.         if(r_in == r_out and g_in == g_out and b_in == b_out){pora = 8; case_1_lock = true;}
  757.         break;
  758.  
  759.    case 8:
  760.         set_fade_time = swit_fade_time;
  761.         set_fade_time = 30;        
  762.         r_in = swit_r;
  763.         g_in = swit_g;
  764.         b_in = swit_b;
  765.         w_in = swit_w;
  766.         case_6_lock = false;
  767.         break;    
  768.  
  769.    case 2:
  770.         set_fade_time = ranek_fade_time;    
  771.         set_fade_time = 30;  
  772.         r_in = ranek_r;
  773.         g_in = ranek_g;
  774.         b_in = ranek_b;
  775.         w_in = ranek_w;
  776.         case_6_lock = false;
  777.         case_1_lock = false;
  778.         break;
  779.  
  780.    case 3:
  781.         set_fade_time = poludnie_fade_time;      
  782.         set_fade_time = 30;
  783.         r_in = poludnie_r;
  784.         g_in = poludnie_g;
  785.         b_in = poludnie_b;
  786.         w_in = poludnie_w;
  787.         case_6_lock = false;
  788.         case_1_lock = false;
  789.         break;
  790.    
  791.    case 4:
  792.         set_fade_time = popoludnie_fade_time;      
  793.         set_fade_time = 30;
  794.         r_in = popoludnie_r;
  795.         g_in = popoludnie_g;
  796.         b_in = popoludnie_b;
  797.         w_in = popoludnie_w;
  798.         case_6_lock = false;
  799.         case_1_lock = false;
  800.         break;
  801.  
  802.    case 5:
  803.         set_fade_time = wieczor_fade_time;      
  804.         set_fade_time = 30;
  805.         r_in = wieczor_r;
  806.         g_in = wieczor_g;
  807.         b_in = wieczor_b;
  808.         w_in = wieczor_w;
  809.         case_6_lock = false;
  810.         case_1_lock = false;
  811.         break;
  812.  
  813.    case 6:
  814.         set_fade_time = noc_fade_time;      
  815.         set_fade_time = 30;
  816.         r_in = 0;
  817.         g_in = 100;
  818.         b_in = 150;
  819.         w_in = 0;
  820.         case_1_lock = false;
  821.         if(r_in == r_out and g_in == g_out and b_in == b_out and w_in == w_out){pora = 7; case_6_lock = true;}
  822.         break;  
  823.  
  824.    case 7:
  825.         set_fade_time = noc_fade_time;      
  826.         set_fade_time = 30;
  827.         r_in = noc_r;
  828.         g_in = noc_g;
  829.         b_in = noc_b;
  830.         w_in = noc_w;
  831.         break;  
  832.      
  833. }
  834.  
  835.  
  836.  
  837.  if(case_tick == true){
  838.  fade_start(&fade_a, r_in, 100);
  839.  fade_start(&fade_b, g_in, 100);
  840.  fade_start(&fade_c, b_in, 100);
  841.  fade_start(&fade_d, w_in, 100);
  842.  
  843.  case_tick = false;
  844.  }
  845.  
  846. //DEBUG SERIAL DEBUG SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG  SERIAL DEBUG
  847.  Serial.print("(");Serial.print(weekDay);Serial.print(") ");
  848.  Serial.print(godzina);Serial.print(":");Serial.print(minuta);Serial.print(":");Serial.print(sekunda);
  849.  Serial.println(" ");
  850.  Serial.print("CYCLE:");
  851.  Serial.print(duration);
  852.  Serial.print(" INIT->:");
  853.  Serial.print(fade_init);
  854.  Serial.print(" PORA: ");
  855.  Serial.println(pora);
  856.  Serial.print(" r_in:");
  857.  Serial.print(r_in);
  858.  Serial.print(" g_in:");
  859.  Serial.print(g_in);
  860.  Serial.print(" b_in:");
  861.  Serial.print(b_in);
  862.  Serial.print(" w_in:");
  863.  Serial.println(w_in);
  864.  Serial.print(" r_out:");
  865.  Serial.print(r_out);
  866.  Serial.print(" g_out:");
  867.  Serial.print(g_out);
  868.  Serial.print(" b_out:");
  869.  Serial.print(b_out);
  870.  Serial.print(" w_out:");
  871.  Serial.println(w_out);
  872.  Serial.print("PTC:");
  873.  Serial.print(fade_ptc);
  874.  Serial.println("");
  875.  
  876.  
  877. }//co sek koniec
  878.  
  879. //server
  880. WiFiClient klient = server.available();   // Listen for incoming clients
  881.  
  882.   if (klient) {                             // If a new client connects,
  883.     web_access_led = true;
  884.     currentTime = millis();
  885.     previousTime = currentTime;
  886.     String currentLine = "";                // make a String to hold incoming data from the client
  887.     while (klient.connected() && currentTime - previousTime <= timeoutTime) {  // loop while the client's connected
  888.       currentTime = millis();
  889.       if (klient.available()) {             // if there's bytes to read from the client,
  890.         char c = klient.read();             // read a byte, then
  891.         header += c;
  892.         if (c == '\n') {                    // if the byte is a newline character
  893.           // if the current line is blank, you got two newline characters in a row.
  894.           // that's the end of the client HTTP request, so send a response:
  895.           if (currentLine.length() == 0) {
  896.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  897.             // and a content-type so the client knows what's coming, then a blank line:
  898.             klient.println("HTTP/1.1 200 OK");
  899.             klient.println("Content-type:application/json");
  900.             klient.println("Connection: close");
  901.             klient.println();
  902.            
  903.             // Display the HTML web page
  904.             DynamicJsonDocument docc(2048);
  905.             docc["name"] = "MiniAquarium";
  906.             docc["mac"] = WiFi.macAddress();
  907.             docc["wifi_qual"]   = getQuality();
  908.             docc["duration"]   = duration;
  909.             docc["up_day"]   =  dddd;
  910.             docc["up_hr"]    =  hh;
  911.             docc["up_min"]   =  mi;
  912.             docc["up_sec"]   =  ss;
  913.             docc["pora"]   =  pora;
  914.             docc["led_r"]   =  r_out;
  915.             docc["led_g"]   =  g_out;
  916.             docc["led_b"]   =  b_out;
  917.             docc["led_w"]   =  w_out;
  918.            
  919.             String jsonserv;
  920.             serializeJsonPretty(docc, jsonserv);
  921.  
  922.             klient.println(jsonserv);
  923.                        
  924.             // The HTTP response ends with another blank line
  925.             klient.println();
  926.             // Break out of the while loop
  927.             break;
  928.           } else { // if you got a newline, then clear currentLine
  929.             currentLine = "";
  930.           }
  931.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  932.           currentLine += c;      // add it to the end of the currentLine
  933.         }
  934.       }
  935.     }
  936.     // Clear the header variable
  937.     header = "";
  938.     // Close the connection
  939.    
  940.     klient.stop();
  941.    
  942.  
  943.   }
  944. //server koniec
  945.  
  946.  
  947.  
  948.  
  949. //uptime
  950. if ((micros() - lastTick) >= 1000000UL) {
  951.   lastTick += 1000000UL;
  952.   ss++;
  953.   if (ss>=60) { ss-=60; mi++; }
  954.   if (mi>=60) { mi-=60; hh++; }
  955.   if (hh>=24) { hh-=24; dddd++; }
  956. }
  957.  
  958. // To display the time in the serial monitor:
  959. //sprintf(timestring,"%d days %02d:%02d:%02d", dddd, hh, mi, ss);
  960.  
  961.  
  962.  
  963.  
  964. }
  965.  
  966.  

odpowiedź "Re: fade"

Tutaj możesz odpowiedzieć na wklejkę z góry

captcha