Advertisement
Double_G

Untitled

Nov 11th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>        
  2. #include <NtpClientLib.h>
  3. #include <FastLED.h>
  4. #include "Timer.h"
  5. #include "BluetoothSerial.h"
  6. #include <DHT.h>
  7.  
  8. #define NUM_LEDS 86    
  9. #define DATA_PIN 4
  10. #define BRIGHTNESS  32
  11. CRGB LEDs[NUM_LEDS];
  12.  
  13. #define DHTPIN 0
  14. #define DHTTYPE DHT22
  15. DHT dht(DHTPIN, DHTTYPE);
  16.  
  17. Timer t1;
  18. Timer t2;
  19. Timer t3;
  20.  
  21. BluetoothSerial SerialBT;
  22.  
  23. String btBuffer;
  24. CRGB colorCRGB = CRGB::Red;           // Change this if you want another default color, for example CRGB::Blue
  25. CHSV colorCHSV = CHSV(95, 255, 255);  // Green
  26. CRGB colorOFF  = CRGB(0,0,0);      // Color of the segments that are 'disabled'. You can also set it to CRGB::Black
  27. volatile int colorMODE = 1;           // 0=CRGB, 1=CHSV, 2=Constant Color Changing pattern
  28. volatile int mode = 0;                // 0=Clock, 1=Temperature, 2=Humidity, 3=Scoreboard, 4=Time counter
  29. volatile int scoreLeft = 0;
  30. volatile int scoreRight = 0;
  31. volatile long timerValue = 0;
  32. volatile int timerRunning = 0;
  33.  
  34. const char* ssid     = "wiFi";               // wifi SSD
  35. const char* password = "Password";        // wifi password
  36. String NTPServer = "NTPserver";                // NTP server address
  37.  
  38. int8_t minutesTimeZone = 0;
  39. int8_t timeZone = 1;                              // timezone in central europe (hour)
  40. String ora1, ora2, perc1, perc2, masod1, masod2, ido;
  41. int ORA1,ORA2, PERC1, PERC2, MIN;
  42. int tomb[4];
  43.  
  44. #define blinkDots   1                 // Set this to 1 if you want the dots to blink in clock mode, set it to value 0 to disable
  45. #define hourFormat  24                // Set this to 12 or to 24 hour format
  46. #define temperatureMode 'C'           // Set this to 'C' for Celcius or 'F' for Fahrenheit
  47.  
  48. void setup () {
  49.   dht.begin();
  50.   SerialBT.begin("Clock");
  51.   WiFi.begin(ssid, password);  
  52.   NTP.begin (NTPServer, timeZone, true, minutesTimeZone); // initialize NTP
  53.   NTP.setInterval (6000);           // sync interval
  54.   FastLED.delay(500);
  55.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(LEDs, NUM_LEDS);
  56.  
  57.   Serial.begin(115200);
  58.   FastLED.setBrightness(BRIGHTNESS);
  59.    
  60.   t1.every(1000 * 29, refreshDisplay);
  61.   t2.every(1000, refreshTimer);
  62.   t3.every(50, updateHue);
  63.   refreshDisplay();
  64. }
  65.  
  66. void loop () {
  67.  
  68.   t1.update();
  69.   t2.update();
  70.   t3.update();
  71.   if (SerialBT.available())
  72.   {
  73.     char received = SerialBT.read();
  74.     btBuffer += received;
  75.  
  76.     if (received == '|')
  77.     {
  78.         processCommand();
  79.         btBuffer = "";
  80.     }
  81.   }
  82. }
  83.  
  84. void updateHue() {
  85.   if (colorMODE != 2)
  86.     return;
  87.    
  88.   colorCHSV.sat = 255;
  89.   colorCHSV.val = 255;
  90.   if (colorCHSV.hue >= 255){
  91.     colorCHSV.hue = 0;
  92.   } else {
  93.     colorCHSV.hue++;
  94.   }
  95.   refreshDisplay();
  96. }
  97.  
  98. void refreshDisplay() {
  99.   switch (mode) {
  100.     case 0:
  101.       displayClock();
  102.       break;
  103.     case 1:
  104.       displayTemperature();
  105.       break;
  106.     case 2:
  107.       displayHumidity();
  108.       break;
  109.     case 3:
  110.       displayScoreboard();
  111.       break;      
  112.     case 4:
  113.       // Time counter has it's own timer
  114.       break;
  115.     default:  
  116.       break;
  117.   }
  118. }
  119.  
  120. void refreshTimer() {
  121.  
  122.   if (mode == 0 && blinkDots == 1) {    
  123.     displayDots(3);
  124.   } else if (mode == 4 && timerRunning == 1 && timerValue < 6000) {
  125.     timerValue++;
  126.  
  127.     int m1 = (timerValue / 60) / 10 ;
  128.     int m2 = (timerValue / 60) % 10 ;
  129.     int s1 = (timerValue % 60) / 10;
  130.     int s2 = (timerValue % 60) % 10;
  131.  
  132.     displaySegments(0, m1);
  133.     displaySegments(21, m2);
  134.     displaySegments(44, s1);    
  135.     displaySegments(65, s2);  
  136.     displayDots(0);  
  137.     FastLED.show();
  138.   }
  139. }
  140.  
  141. void processCommand(){
  142.  
  143.   if (btBuffer.startsWith("RGBD")) {
  144.     long R = getValue(btBuffer, ',', 1).toInt();
  145.     long G = getValue(btBuffer, ',', 2).toInt();
  146.     long B = getValue(btBuffer, ',', 3).toInt();
  147.     long D = getValue(btBuffer, ',', 4).toInt();
  148.     colorCRGB.red = R;
  149.     colorCRGB.green = G;
  150.     colorCRGB.blue = B;
  151.     colorMODE = 0;
  152.     if (D > 0) FastLED.setBrightness(D);
  153.   } else if (btBuffer.startsWith("HSVD")) {
  154.     long H = getValue(btBuffer, ',', 1).toInt();
  155.     long S = getValue(btBuffer, ',', 2).toInt();
  156.     long V = getValue(btBuffer, ',', 3).toInt();
  157.     long D = getValue(btBuffer, ',', 4).toInt();
  158.     colorCHSV.hue = H;
  159.     colorCHSV.sat = S;
  160.     colorCHSV.val = V;
  161.     colorMODE = 1;
  162.     if (D > 0) FastLED.setBrightness(D);
  163.   } else if (btBuffer.startsWith("RTC")) {
  164.     long y = getValue(btBuffer, ',', 1).toInt();
  165.     long m = getValue(btBuffer, ',', 2).toInt();
  166.     long d = getValue(btBuffer, ',', 3).toInt();
  167.     long h = getValue(btBuffer, ',', 4).toInt();
  168.     long mm = getValue(btBuffer, ',', 5).toInt();
  169.     long s = getValue(btBuffer, ',', 6).toInt();
  170.     Serial.println("DateTime set");
  171.   } else if (btBuffer.startsWith("CLOCK")) {
  172.     mode = 0;    
  173.   } else if (btBuffer.startsWith("TEMPERATURE")) {
  174.     mode = 1;    
  175.   } else if (btBuffer.startsWith("HUMIDITY")) {
  176.     mode = 2;
  177.   } else if (btBuffer.startsWith("SCOREBOARD")) {
  178.     scoreLeft = getValue(btBuffer, ',', 1).toInt();
  179.     scoreRight = getValue(btBuffer, ',', 2).toInt();
  180.     mode = 3;    
  181.   } else if (btBuffer.startsWith("STARTTIMER")) {
  182.     timerValue = 0;
  183.     timerRunning = 1;
  184.     mode = 4;    
  185.   } else if (btBuffer.startsWith("STOPTIMER")) {
  186.     timerRunning = 0;
  187.     mode = 4;    
  188.   } else if (btBuffer.startsWith("CHANGINGPATTERN")) {
  189.     colorMODE = 2;
  190.   }
  191.       refreshDisplay();
  192.  
  193.    
  194. }
  195.  
  196. void displayClock() {  
  197.   ido = NTP.getTimeDateString ();
  198.   ora1 = (ido.substring(0,1));
  199.   ora2 = (ido.substring(1,2));
  200.   perc1 = (ido.substring(3,4));
  201.   perc2 = (ido.substring(4,5));
  202.   masod1 = (ido.substring(6,7));
  203.   masod2 = (ido.substring(7,8));
  204.   tomb[0] = ora1.toInt();
  205.   tomb[1] = ora2.toInt();
  206.   tomb[2] = perc1.toInt();
  207.   tomb[3] = perc2.toInt();
  208.    if (tomb[0] == 0)
  209.   {
  210.     tomb[0] = 13;
  211.   }
  212.  
  213.   displaySegments(0, tomb[0]);    
  214.   displaySegments(21,tomb[1]);
  215.   displaySegments(44,tomb[2]);    
  216.   displaySegments(65,tomb[3]);  
  217.   displayDots(0);  
  218.   FastLED.show();
  219. }
  220.  
  221. void displayTemperature() {
  222.   float tmp = dht.readTemperature(temperatureMode == 'F' ? true : false);
  223.  
  224.   if (isnan(tmp)) {
  225.     Serial.println("Failed to read from DHT sensor!");
  226.   } else {
  227.     int tmp1 = tmp / 10;
  228.     int tmp2 = ((int)tmp) % 10;
  229.     displaySegments(65, (temperatureMode == 'F' ? 14 : 11));    
  230.     displaySegments(44, 10);
  231.     displaySegments(21, tmp2);    
  232.     displaySegments(0, tmp1);
  233.     displayDots(1);  
  234.     FastLED.show();    
  235.   }  
  236. }
  237.  
  238. void displayHumidity() {
  239.   float hum = dht.readHumidity();
  240.  
  241.   if (isnan(hum)) {
  242.     Serial.println("Failed to read from DHT sensor!");
  243.   } else {
  244.     int hum1 = hum / 10;
  245.     int hum2 = ((int)hum) % 10;
  246.     displaySegments(65, 10);    
  247.     displaySegments(44, 12);
  248.     displaySegments(21,  hum2);    
  249.     displaySegments(0,  hum1
  250.     );
  251.     displayDots(1);  
  252.     FastLED.show();    
  253.   }  
  254. }
  255.  
  256. void displayScoreboard() {
  257.   int s1 = scoreLeft % 10;
  258.   int s2 = scoreLeft / 10;
  259.   int s3 = scoreRight % 10;
  260.   int s4 = scoreRight / 10;
  261.   displaySegments(0, s4);    
  262.   displaySegments(21, s3);
  263.   displaySegments(44, s2);    
  264.   displaySegments(65, s1);
  265.   displayDots(2);  
  266.   FastLED.show();  
  267. }
  268.  
  269. void displayDots(int dotMode) {
  270.   // dotMode: 0=Both on, 1=Both Off, 2=Bottom On, 3=Blink
  271.   switch (dotMode) {
  272.     case 0:
  273.       LEDs[42] = colorMODE == 0 ? colorCRGB : colorCHSV;
  274.       LEDs[43] = colorMODE == 0 ? colorCRGB : colorCHSV;
  275.       break;
  276.     case 1:
  277.       LEDs[42] = colorOFF;
  278.       LEDs[43] = colorOFF;
  279.       break;
  280.     case 2:
  281.       LEDs[42] = colorOFF;
  282.       LEDs[43] = colorMODE == 0 ? colorCRGB : colorCHSV;
  283.       break;
  284.     case 3:
  285.       LEDs[42] = (LEDs[42] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  286.       LEDs[43] = (LEDs[43] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  287.       FastLED.show();  
  288.       break;
  289.     default:
  290.       break;    
  291.   }
  292. }
  293.  
  294. void displaySegments(int startindex, int number) {
  295.  
  296.   int numbers[] = {
  297.     0b0000111111111111111111, // 0    
  298.     0b0000000000111111000000, // 1
  299.     0b0111000111111000111111, // 2
  300.     0b0111000111111111111000, // 3
  301.     0b0111111000111111000000, // 4
  302.     0b0111111111000111111000, // 5
  303.     0b0111111111000111111111, // 6
  304.     0b0000000111111111000000, // 7
  305.     0b0111111111111111111111, // 8
  306.     0b0111111111111111111000, // 9  
  307.     0b0111111111111000000000, // º              10
  308.     0b0000111111000000111111, // C(elcius)      11
  309.     0b0111000000000111111111, // º lower        12
  310.     0b0000000000000000000000, // Empty          13
  311.     0b0111111111000000000111, // F(ahrenheit)   14
  312.   };
  313.  
  314.   for (int i = 0; i < 21; i++) {
  315.     LEDs[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  316.   }
  317. }
  318.  
  319. String getValue(String data, char separator, int index) {
  320.   int found = 0;
  321.   int strIndex[] = {0, -1};
  322.   int maxIndex = data.length()-1;
  323.  
  324.   for(int i=0; i<=maxIndex && found<=index; i++){
  325.     if(data.charAt(i)==separator || i==maxIndex){
  326.         found++;
  327.         strIndex[0] = strIndex[1]+1;
  328.         strIndex[1] = (i == maxIndex) ? i+1 : i;
  329.     }
  330.   }
  331.  
  332.   return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement