Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <NtpClientLib.h>
- #include <FastLED.h>
- #include "Timer.h"
- #include "BluetoothSerial.h"
- #include <DHT.h>
- #define NUM_LEDS 86
- #define DATA_PIN 4
- #define BRIGHTNESS 32
- CRGB LEDs[NUM_LEDS];
- #define DHTPIN 0
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- Timer t1;
- Timer t2;
- Timer t3;
- BluetoothSerial SerialBT;
- String btBuffer;
- CRGB colorCRGB = CRGB::Red; // Change this if you want another default color, for example CRGB::Blue
- CHSV colorCHSV = CHSV(95, 255, 255); // Green
- CRGB colorOFF = CRGB(0,0,0); // Color of the segments that are 'disabled'. You can also set it to CRGB::Black
- volatile int colorMODE = 1; // 0=CRGB, 1=CHSV, 2=Constant Color Changing pattern
- volatile int mode = 0; // 0=Clock, 1=Temperature, 2=Humidity, 3=Scoreboard, 4=Time counter
- volatile int scoreLeft = 0;
- volatile int scoreRight = 0;
- volatile long timerValue = 0;
- volatile int timerRunning = 0;
- const char* ssid = "wiFi"; // wifi SSD
- const char* password = "Password"; // wifi password
- String NTPServer = "NTPserver"; // NTP server address
- int8_t minutesTimeZone = 0;
- int8_t timeZone = 1; // timezone in central europe (hour)
- String ora1, ora2, perc1, perc2, masod1, masod2, ido;
- int ORA1,ORA2, PERC1, PERC2, MIN;
- int tomb[4];
- #define blinkDots 1 // Set this to 1 if you want the dots to blink in clock mode, set it to value 0 to disable
- #define hourFormat 24 // Set this to 12 or to 24 hour format
- #define temperatureMode 'C' // Set this to 'C' for Celcius or 'F' for Fahrenheit
- void setup () {
- dht.begin();
- SerialBT.begin("Clock");
- WiFi.begin(ssid, password);
- NTP.begin (NTPServer, timeZone, true, minutesTimeZone); // initialize NTP
- NTP.setInterval (6000); // sync interval
- FastLED.delay(500);
- FastLED.addLeds<WS2812B, DATA_PIN, GRB>(LEDs, NUM_LEDS);
- Serial.begin(115200);
- FastLED.setBrightness(BRIGHTNESS);
- t1.every(1000 * 29, refreshDisplay);
- t2.every(1000, refreshTimer);
- t3.every(50, updateHue);
- refreshDisplay();
- }
- void loop () {
- t1.update();
- t2.update();
- t3.update();
- if (SerialBT.available())
- {
- char received = SerialBT.read();
- btBuffer += received;
- if (received == '|')
- {
- processCommand();
- btBuffer = "";
- }
- }
- }
- void updateHue() {
- if (colorMODE != 2)
- return;
- colorCHSV.sat = 255;
- colorCHSV.val = 255;
- if (colorCHSV.hue >= 255){
- colorCHSV.hue = 0;
- } else {
- colorCHSV.hue++;
- }
- refreshDisplay();
- }
- void refreshDisplay() {
- switch (mode) {
- case 0:
- displayClock();
- break;
- case 1:
- displayTemperature();
- break;
- case 2:
- displayHumidity();
- break;
- case 3:
- displayScoreboard();
- break;
- case 4:
- // Time counter has it's own timer
- break;
- default:
- break;
- }
- }
- void refreshTimer() {
- if (mode == 0 && blinkDots == 1) {
- displayDots(3);
- } else if (mode == 4 && timerRunning == 1 && timerValue < 6000) {
- timerValue++;
- int m1 = (timerValue / 60) / 10 ;
- int m2 = (timerValue / 60) % 10 ;
- int s1 = (timerValue % 60) / 10;
- int s2 = (timerValue % 60) % 10;
- displaySegments(0, m1);
- displaySegments(21, m2);
- displaySegments(44, s1);
- displaySegments(65, s2);
- displayDots(0);
- FastLED.show();
- }
- }
- void processCommand(){
- if (btBuffer.startsWith("RGBD")) {
- long R = getValue(btBuffer, ',', 1).toInt();
- long G = getValue(btBuffer, ',', 2).toInt();
- long B = getValue(btBuffer, ',', 3).toInt();
- long D = getValue(btBuffer, ',', 4).toInt();
- colorCRGB.red = R;
- colorCRGB.green = G;
- colorCRGB.blue = B;
- colorMODE = 0;
- if (D > 0) FastLED.setBrightness(D);
- } else if (btBuffer.startsWith("HSVD")) {
- long H = getValue(btBuffer, ',', 1).toInt();
- long S = getValue(btBuffer, ',', 2).toInt();
- long V = getValue(btBuffer, ',', 3).toInt();
- long D = getValue(btBuffer, ',', 4).toInt();
- colorCHSV.hue = H;
- colorCHSV.sat = S;
- colorCHSV.val = V;
- colorMODE = 1;
- if (D > 0) FastLED.setBrightness(D);
- } else if (btBuffer.startsWith("RTC")) {
- long y = getValue(btBuffer, ',', 1).toInt();
- long m = getValue(btBuffer, ',', 2).toInt();
- long d = getValue(btBuffer, ',', 3).toInt();
- long h = getValue(btBuffer, ',', 4).toInt();
- long mm = getValue(btBuffer, ',', 5).toInt();
- long s = getValue(btBuffer, ',', 6).toInt();
- Serial.println("DateTime set");
- } else if (btBuffer.startsWith("CLOCK")) {
- mode = 0;
- } else if (btBuffer.startsWith("TEMPERATURE")) {
- mode = 1;
- } else if (btBuffer.startsWith("HUMIDITY")) {
- mode = 2;
- } else if (btBuffer.startsWith("SCOREBOARD")) {
- scoreLeft = getValue(btBuffer, ',', 1).toInt();
- scoreRight = getValue(btBuffer, ',', 2).toInt();
- mode = 3;
- } else if (btBuffer.startsWith("STARTTIMER")) {
- timerValue = 0;
- timerRunning = 1;
- mode = 4;
- } else if (btBuffer.startsWith("STOPTIMER")) {
- timerRunning = 0;
- mode = 4;
- } else if (btBuffer.startsWith("CHANGINGPATTERN")) {
- colorMODE = 2;
- }
- refreshDisplay();
- }
- void displayClock() {
- ido = NTP.getTimeDateString ();
- ora1 = (ido.substring(0,1));
- ora2 = (ido.substring(1,2));
- perc1 = (ido.substring(3,4));
- perc2 = (ido.substring(4,5));
- masod1 = (ido.substring(6,7));
- masod2 = (ido.substring(7,8));
- tomb[0] = ora1.toInt();
- tomb[1] = ora2.toInt();
- tomb[2] = perc1.toInt();
- tomb[3] = perc2.toInt();
- if (tomb[0] == 0)
- {
- tomb[0] = 13;
- }
- displaySegments(0, tomb[0]);
- displaySegments(21,tomb[1]);
- displaySegments(44,tomb[2]);
- displaySegments(65,tomb[3]);
- displayDots(0);
- FastLED.show();
- }
- void displayTemperature() {
- float tmp = dht.readTemperature(temperatureMode == 'F' ? true : false);
- if (isnan(tmp)) {
- Serial.println("Failed to read from DHT sensor!");
- } else {
- int tmp1 = tmp / 10;
- int tmp2 = ((int)tmp) % 10;
- displaySegments(65, (temperatureMode == 'F' ? 14 : 11));
- displaySegments(44, 10);
- displaySegments(21, tmp2);
- displaySegments(0, tmp1);
- displayDots(1);
- FastLED.show();
- }
- }
- void displayHumidity() {
- float hum = dht.readHumidity();
- if (isnan(hum)) {
- Serial.println("Failed to read from DHT sensor!");
- } else {
- int hum1 = hum / 10;
- int hum2 = ((int)hum) % 10;
- displaySegments(65, 10);
- displaySegments(44, 12);
- displaySegments(21, hum2);
- displaySegments(0, hum1
- );
- displayDots(1);
- FastLED.show();
- }
- }
- void displayScoreboard() {
- int s1 = scoreLeft % 10;
- int s2 = scoreLeft / 10;
- int s3 = scoreRight % 10;
- int s4 = scoreRight / 10;
- displaySegments(0, s4);
- displaySegments(21, s3);
- displaySegments(44, s2);
- displaySegments(65, s1);
- displayDots(2);
- FastLED.show();
- }
- void displayDots(int dotMode) {
- // dotMode: 0=Both on, 1=Both Off, 2=Bottom On, 3=Blink
- switch (dotMode) {
- case 0:
- LEDs[42] = colorMODE == 0 ? colorCRGB : colorCHSV;
- LEDs[43] = colorMODE == 0 ? colorCRGB : colorCHSV;
- break;
- case 1:
- LEDs[42] = colorOFF;
- LEDs[43] = colorOFF;
- break;
- case 2:
- LEDs[42] = colorOFF;
- LEDs[43] = colorMODE == 0 ? colorCRGB : colorCHSV;
- break;
- case 3:
- LEDs[42] = (LEDs[42] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
- LEDs[43] = (LEDs[43] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
- FastLED.show();
- break;
- default:
- break;
- }
- }
- void displaySegments(int startindex, int number) {
- int numbers[] = {
- 0b0000111111111111111111, // 0
- 0b0000000000111111000000, // 1
- 0b0111000111111000111111, // 2
- 0b0111000111111111111000, // 3
- 0b0111111000111111000000, // 4
- 0b0111111111000111111000, // 5
- 0b0111111111000111111111, // 6
- 0b0000000111111111000000, // 7
- 0b0111111111111111111111, // 8
- 0b0111111111111111111000, // 9
- 0b0111111111111000000000, // º 10
- 0b0000111111000000111111, // C(elcius) 11
- 0b0111000000000111111111, // º lower 12
- 0b0000000000000000000000, // Empty 13
- 0b0111111111000000000111, // F(ahrenheit) 14
- };
- for (int i = 0; i < 21; i++) {
- LEDs[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
- }
- }
- String getValue(String data, char separator, int index) {
- int found = 0;
- int strIndex[] = {0, -1};
- int maxIndex = data.length()-1;
- for(int i=0; i<=maxIndex && found<=index; i++){
- if(data.charAt(i)==separator || i==maxIndex){
- found++;
- strIndex[0] = strIndex[1]+1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement