Double_G

Workout Timer

Nov 17th, 2018
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <FastLED.h>
  3. #include "Countimer.h"
  4.  
  5. const char* ssid     = "Clock_By_Grubits_Gabor";
  6. const char* password = "Clock2018#";
  7.  
  8. WiFiServer server(80);
  9.  
  10. String perc1, perc2, masod1, masod2, ido, header;
  11. int tomb[4];
  12. uint8_t colorIndex;
  13.  
  14. #define NUM_LEDS 86
  15. #define DATA_PIN 4
  16. #define BRIGHTNESS  128
  17.  
  18. CRGB LEDs[NUM_LEDS];
  19. CRGB colorCRGB = CRGB::Blue;
  20. CHSV colorCHSV = CHSV(0, 165, 255);
  21. CRGB colorOFF  = CRGB(0, 0, 0);
  22. volatile int colorMODE = 0;
  23.  
  24. Countimer tDown;      //Work timer
  25. Countimer tDown2;     //Pause timer
  26.  
  27. int pausemin = 0;
  28. int pausesec = 10;
  29. int workmin = 0;
  30. int worksec = 20;
  31. int retry = 16;
  32. String workminstring = String(workmin);
  33. String worksecstring = String(worksec);
  34.  
  35. void setup() {
  36.   pinMode(2, OUTPUT);
  37.   Serial.begin(115200);
  38.   FastLED.delay(500);
  39.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(LEDs, NUM_LEDS);
  40.   FastLED.setBrightness(BRIGHTNESS);
  41.  
  42.   WiFi.softAP(ssid, password);
  43.  
  44.   //Serial.print("AP IP address: ");
  45.  
  46.   server.begin();
  47.  
  48.   tDown.setInterval(print_time1, 1000);
  49.   tDown2.setInterval(print_time2, 1000);
  50.  
  51.   for (int z = 0; z < 1024; z++)
  52.   {
  53.     static uint8_t colorIndex = 0;
  54.     colorIndex = colorIndex + 1;
  55.     uint8_t brightness = 255;
  56.     for ( int i = 0; i < NUM_LEDS; i++) {
  57.       LEDs[i] = ColorFromPalette( RainbowColors_p, colorIndex, brightness, LINEARBLEND);
  58.       colorIndex += 3;
  59.     }
  60.     FastLED.show();
  61.     FastLED.delay(7);
  62.   }
  63.   //Serial.println(WiFi.softAPIP());
  64.   ido = "00:00:20";
  65.   tDown.setCounter(0, workmin, worksec, tDown.COUNT_DOWN, tDownComplete);
  66.   tDown2.setCounter(0, pausemin, pausesec, tDown2.COUNT_DOWN, tDown2Complete);
  67.   displayClock();
  68. }
  69.  
  70. void loop() {
  71.   tDown.run();
  72.   tDown2.run();
  73.   WiFiClient client = server.available();
  74.   char clientline[BUFSIZ];
  75.   int index = 0;
  76.   if (client) {
  77.     boolean currentLineIsBlank = true;
  78.     boolean currentLineIsGet = true;
  79.     int tCount = 0;
  80.     char tBuf[64];
  81.     char *pch;
  82.     //Serial.print("Client request: ");
  83.     while (client.connected()) {
  84.       while (client.available()) {
  85.         char c = client.read();
  86.         header += c;
  87.         if (currentLineIsGet && tCount < 63)
  88.         {
  89.           tBuf[tCount] = c;
  90.           tCount++;
  91.           tBuf[tCount] = 0;
  92.         }
  93.         if (c == '\n' && currentLineIsBlank) {
  94.           while (client.available()) client.read();
  95.           pch = strtok(tBuf, "?");
  96.           while (pch != NULL)
  97.           {
  98.             if (strncmp(pch, "workmin=", 8) == 0)
  99.             {
  100.               workmin = atoi(pch + 8);
  101.               if (workmin > 60) {
  102.                 workmin = 59;
  103.               }
  104.               if (worksec > 60) {
  105.                 worksec = 59;
  106.               }
  107.               tDown.setCounter(0, workmin, worksec, tDown.COUNT_DOWN, tDownComplete);
  108.             }
  109.             if (strncmp(pch, "worksec=", 8) == 0)
  110.             {
  111.               worksec = atoi(pch + 8);
  112.               if (workmin > 60) {
  113.                 workmin = 59;
  114.               }
  115.               if (worksec > 60) {
  116.                 worksec = 60;
  117.               }
  118.               tDown.setCounter(0, workmin, worksec, tDown.COUNT_DOWN, tDownComplete);
  119.               String worksecstring = String(worksec);
  120.               String workminstring = String(workmin);
  121.               if (worksec < 10) {
  122.                 worksecstring = "0" + worksecstring;
  123.               }
  124.               if (workmin < 10) {
  125.                 workminstring = "0" + workminstring;
  126.               }
  127.               ido = "00:" + workminstring + ":" + worksecstring;
  128.               displayClock();
  129.             }
  130.             if (strncmp(pch, "pausemin=", 9) == 0)
  131.             {
  132.               pausemin = atoi(pch + 9);
  133.               tDown2.setCounter(0, pausemin, pausesec, tDown2.COUNT_DOWN, tDown2Complete);
  134.             }
  135.             if (strncmp(pch, "pausesec=", 9) == 0)
  136.             {
  137.               pausesec = atoi(pch + 9);
  138.               tDown2.setCounter(0, pausemin, pausesec, tDown2.COUNT_DOWN, tDown2Complete);
  139.               if (pausesec > 60) {
  140.                 pausesec = 60;
  141.               }
  142.               if (pausemin > 60) {
  143.                 pausemin = 59;
  144.               }
  145.             }
  146.  
  147.             if (strncmp(pch, "retry=", 6) == 0)
  148.             {
  149.               retry = atoi(pch + 6);
  150.              }
  151.  
  152.             pch = strtok(NULL, "& ");
  153.           }
  154.           if (header.indexOf("GET /start") >= 0) {
  155.             tDown.start();
  156.             tDown2.stop();
  157.           } else if (header.indexOf("GET /stop") >= 0) {
  158.             tDown.stop();
  159.             tDown2.stop();
  160.           } else if (header.indexOf("GET /pihi") >= 0) {
  161.             tDown.pause();
  162.           }
  163.           client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TIMER</H1><form method=GET>");
  164.           client.print("Work: <input type=text name=workmin value=");
  165.           client.print(workmin);
  166.           client.print(">min.<input type=text name=worksec value=");
  167.           client.print(worksec);
  168.           client.print(">sec.<br><br>");
  169.           client.print("Pause:<input type=text name=pausemin value=");
  170.           client.print(pausemin);
  171.           client.print(">min.<input type=text name=pausesec value=");
  172.           client.print(pausesec);
  173.           client.print(">sec.<br><br>");
  174.           client.print("Retry: <input type=text name=retry value=");
  175.           client.print(retry);
  176.           client.print("><br><input type=submit></form>");
  177.           client.println("<!DOCTYPE html><html>");
  178.           client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  179.           client.println("<link rel=\"icon\" href=\"data:,\">");
  180.           client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  181.           client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 8px 20px;");
  182.           client.println("text-decoration: none; font-size: 15px; margin: 2px; cursor: pointer;}");
  183.           client.println(".button2 {background-color: #555555;}</style></head>");
  184.           client.println("<body><h1>Control buttons</h1>");
  185.           client.println("<p><a href=\"/start\"><button class=\"button button2\">RE/START</button></a></p>");
  186.           client.println("<p><a href=\"/stop\"><button class=\"button button2\">STOP</button></a></p>");
  187.           client.println("<p><a href=\"/pihi\"><button class=\"button button2\">PAUSE</button></a></p>");
  188.           client.println("<br><br><br>By Grubits Gabor<br>[email protected]<br>+36-30-2267716");
  189.           client.write("</body></html>\r\n\r\n");
  190.           header = "";
  191.           client.stop();
  192.         }
  193.         else if (c == '\n') {
  194.           currentLineIsBlank = true;
  195.           currentLineIsGet = false;
  196.         }
  197.         else if (c != '\r') {
  198.           currentLineIsBlank = false;
  199.         }
  200.       }
  201.     }
  202.   }
  203. }
  204.  
  205. void displayClock() {
  206.   perc1 = (ido.substring(3, 4));
  207.   perc2 = (ido.substring(4, 5));
  208.   masod1 = (ido.substring(6, 7));
  209.   masod2 = (ido.substring(7, 8));
  210.   tomb[0] = perc1.toInt();
  211.   tomb[1] = perc2.toInt();
  212.   tomb[2] = masod1.toInt();
  213.   tomb[3] = masod2.toInt();
  214.  
  215.   displaySegments(0, tomb[0]);
  216.   displaySegments(21, tomb[1]);
  217.   displaySegments(44, tomb[2]);
  218.   displaySegments(65, tomb[3]);
  219.   displayDots(0);
  220.   FastLED.show();
  221. }
  222.  
  223.  
  224. void displayDots(int dotMode) {
  225.   LEDs[42] = (LEDs[42] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  226.   LEDs[43] = (LEDs[43] == colorOFF) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  227.   FastLED.show();
  228. }
  229.  
  230. void displaySegments(int startindex, int number) {
  231.   int numbers[] = {
  232.     0b0000111111111111111111, // 0
  233.     0b0000000000111111000000, // 1
  234.     0b0111000111111000111111, // 2
  235.     0b0111000111111111111000, // 3
  236.     0b0111111000111111000000, // 4
  237.     0b0111111111000111111000, // 5
  238.     0b0111111111000111111111, // 6
  239.     0b0000000111111111000000, // 7
  240.     0b0111111111111111111111, // 8
  241.     0b0111111111111111111000, // 9
  242.     0b0111111111111000000000, // ΒΊ              10
  243.     0b0000111111000000111111, // C(elcius)      11
  244.     0b0111000000000111111111, // ΒΊ lower        12
  245.     0b0000000000000000000000, // Empty          13
  246.     0b0111111111000000000111, // F(ahrenheit)   14
  247.   };
  248.  
  249.   for (int i = 0; i < 21; i++) {
  250.     LEDs[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? (colorMODE == 0 ? colorCRGB : colorCHSV) : colorOFF;
  251.   }
  252. }
  253.  
  254. void tDownComplete()
  255. {
  256.   tDown2.start();
  257. }
  258.  
  259. void tDown2Complete()
  260. {
  261.   if (retry > 1)
  262.   {
  263.     tDown.start();
  264.     retry--;
  265.   } else {
  266.     digitalWrite(2, HIGH);
  267.     for (int i = 0; i < 20; i++)
  268.     {
  269.       colorCRGB = CRGB::Red;
  270.       displaySegments(0, 10);
  271.       displaySegments(21, 12);
  272.       displaySegments(44, 10);
  273.       displaySegments(65, 12);
  274.       displayDots(0);
  275.       FastLED.show();
  276.       FastLED.delay(200);
  277.       colorCRGB = CRGB::White;
  278.       displaySegments(0, 12);
  279.       displaySegments(21, 10);
  280.       displaySegments(44, 12);
  281.       displaySegments(65, 10);
  282.       displayDots(0);
  283.       FastLED.show();
  284.       FastLED.delay(200);
  285.     }
  286.     String worksecstring = String(worksec);
  287.     String workminstring = String(workmin);
  288.     if (worksec < 10) {
  289.       worksecstring = "0" + worksecstring;
  290.     }
  291.     if (workmin < 10) {
  292.       workminstring = "0" + workminstring;
  293.     }
  294.     colorCRGB = CRGB::Blue;
  295.     ido = "00:" + workminstring + ":" + worksecstring;
  296.     displayClock();
  297.     digitalWrite(2, LOW);
  298.   }
  299. }
  300.  
  301. void print_time1()
  302. {
  303.   colorCRGB = CRGB::Blue;
  304.   ido = tDown.getCurrentTime();
  305.   displayClock();
  306. }
  307.  
  308. void print_time2()
  309. {
  310.   colorCRGB = CRGB::Green;
  311.   ido = tDown2.getCurrentTime();
  312.   displayClock();
  313. }
Advertisement
Add Comment
Please, Sign In to add comment