Szerelo

pittyu2

May 28th, 2022 (edited)
1,022
2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.40 KB
  1. // 4.
  2.  
  3. #include <virtuabotixRTC.h>
  4. #include <Adafruit_NeoPixel.h>
  5. #include "DHT.h"
  6.  
  7. #define buttonPlus A1
  8. #define buttonMinus A2
  9. #define PLUS 9
  10. #define GND 11
  11.  
  12.  
  13. // Creation of the Real Time Clock Object
  14. // SCLK(CLK) -> 6, I/O(DAT) -> 7, CE(RST) -> 8
  15. virtuabotixRTC myRTC(6, 7, 8);
  16. //virtuabotixRTC myRTC(2, 3, 4);
  17.  
  18. #define NUMPIXELS 24
  19. #define PIN 5
  20. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  21.  
  22. // DHT11 Defines
  23. #define DHTPIN 4
  24. #define DHTTYPE DHT11
  25.  
  26. #define TEMPPIXELS 6
  27. #define TEMPPIN 3
  28. DHT dht(DHTPIN, DHTTYPE);
  29. Adafruit_NeoPixel TEMPpixels = Adafruit_NeoPixel(TEMPPIXELS, TEMPPIN, NEO_GRB + NEO_KHZ800);
  30.  
  31.  
  32. unsigned long debounceDelay=50;     // the debounce time; increase if the output flickers
  33. unsigned long longPressDelay=1000;  // A gombokhoz tartozó hosszú megnyomás
  34. unsigned long repeatDelay=400;      // A gombokhoz tartozó ismétlődés ideje
  35. signed long repeatDelayDynamic=400;
  36. signed long dynamic=10;
  37. unsigned long lastDebounceTime1=0, lastDebounceTime2=0;
  38.  
  39. unsigned long counter=0;
  40.  
  41. bool readingButton1, readingButton2, readingButton3, readingButton4, readingButton5;
  42. bool buttonShort1=false, buttonShort2=false, buttonShort3=false, buttonShort4=false, buttonShort5=false;
  43. bool buttonLong1=false, buttonLong2=false, buttonLong3=false, buttonLong4=false, buttonLong5=false;
  44. bool repeat1=false, repeat2=false;
  45.  
  46.  
  47. int hourLEDs[4] = {4,10,16,22};
  48. int hourTensLEDs[2] = {5,11};
  49. int minuteLEDs[4] = {2,8,14,20};
  50. int minuteTensLEDs[3] = {3,9,15};
  51. int secondLEDs[4] = {0, 6, 12, 18};
  52. int secondsTensLEDs[3] = {1, 7, 13};
  53.  
  54. char buffer[9] ="";
  55.  
  56. int secs, mins, hrs;
  57. int sunit, munit, hunit;
  58. int sten, mten, hten;
  59.  
  60. int sunittemp, stentemp, munittemp,mtentemp, hunittemp, htentemp;
  61.  
  62. long currentMillis, previousMillis;
  63. int temp, humi;
  64.  
  65. int temperatureArray[41][6]{
  66.   {0,2,6,6,6,6}, //5
  67.   {1,2,6,6,6,6}, //6
  68.   {0,1,2,6,6,6}, //7
  69.   {3,6,6,6,6,6}, //8
  70.   {0,3,6,6,6,6}, //9
  71.   {1,3,6,6,6,6}, //10
  72.   {0,1,3,6,6,6}, //11
  73.   {2,3,6,6,6,6}, //12
  74.   {0,2,3,6,6,6}, //13
  75.   {1,2,3,6,6,6}, //14
  76.   {0,1,2,3,6,6}, //15
  77.   {4,6,6,6,6,6}, //16
  78.   {0,4,6,6,6,6}, //17
  79.   {1,4,6,6,6,6}, //18
  80.   {0,1,4,6,6,6}, //19
  81.   {2,4,6,6,6,6}, //20
  82.   {0,2,4,6,6,6}, //21
  83.   {1,2,4,6,6,6}, //22
  84.   {0,1,2,4,6,6}, //23
  85.   {3,4,6,6,6,6}, //24
  86.   {0,3,4,6,6,6}, //25
  87.   {1,2,4,6,6,6}, //26
  88.   {0,1,3,4,6,6}, //27
  89.   {2,3,4,6,6,6}, //28
  90.   {0,1,3,4,6,6}, //29
  91.   {1,2,3,4,6,6}, //30
  92.   {0,1,2,3,4,6}, //31
  93.   {5,6,6,6,6,6}, //32
  94.   {0,5,6,6,6,6}, //33
  95.   {1,5,6,6,6,6}, //34
  96.   {0,2,5,6,6,6}, //35
  97.   {2,5,6,6,6,6}, //36
  98.   {0,2,5,6,6,6}, //37
  99.   {1,2,5,6,6,6}, //38
  100.   {0,1,2,5,6,6}, //39
  101.   {3,5,6,6,6,6}, //40
  102.   {0,3,5,6,6,6}, //41
  103.   {1,3,5,6,6,6}, //42
  104.   {0,1,3,5,6,6}, //43
  105.   {2,3,5,6,6,6}, //44
  106.   {1,2,3,5,6,6} //45
  107. };
  108.  
  109. int sunitArray[10][3]{
  110.   {24,24,24},
  111.   {0,24,24},
  112.   {6,24,24},
  113.   {0,6,24},
  114.   {12,24,24},
  115.   {0,12,24},
  116.   {6,12,24},
  117.   {0,6,12},
  118.   {18,24,24},
  119.   {0,18,24}
  120.   };
  121.  
  122. int stenArray[10][3]{
  123.   {24,24,24},
  124.   {1,24,24},
  125.   {7,24,24},
  126.   {1,7,24},
  127.   {13,24,24},
  128.   {1,13,24},
  129.   {7,13,24},
  130.   {1,7,13},
  131.   {19,24,24},
  132.   (1,19,24)
  133.   };
  134.  
  135. int munitArray[10][3]{
  136.   {24,24,24},
  137.   {2,24,24},
  138.   {8,24,24},
  139.   {2,8,24},
  140.   {14,24,24},
  141.   {2,14,24},
  142.   {8,14,24},
  143.   {2,8,14},
  144.   {20,24,24},
  145.   {2,20,24}
  146.   };
  147.  
  148. int mtenArray[10][3]{
  149.   {24,24,24},
  150.   {3,24,24},
  151.   {9,24,24},
  152.   {3,9,24},
  153.   {15,24,24},
  154.   {3,15,24},
  155.   {9,15,24},
  156.   {3,9,15},
  157.   {21,24,24},
  158.   {3,21,24}
  159.   };
  160.  
  161. int hunitArray[10][3]{
  162.   {24,24,24},
  163.   {4,24,24},
  164.   {10,24,24},
  165.   {4,10,24},
  166.   {16,24,24},
  167.   {4,16,24},
  168.   {10,16,24},
  169.   {4,10,16},
  170.   {22,24,24},
  171.   {4,22,24}
  172.   };
  173.  
  174. int htenArray[10][3]{
  175.   {24,24,24},
  176.   {5,24,24},
  177.   {11,24,24},
  178.   {5,11,24},
  179.   {17,24,24},
  180.   {5,17,24},
  181.   {11,17,24},
  182.   {5,11,17},
  183.   {23,24,24},
  184.   {5,23,24}
  185.   };
  186.  
  187. const int analogInPin = A0;
  188. int sensorValue = 0;
  189. int outputValue =0;
  190.  
  191. void setup()
  192. {
  193.  
  194. Serial.begin(115200);
  195.   pinMode(buttonPlus, INPUT_PULLUP);
  196.   pinMode(buttonMinus, INPUT_PULLUP);
  197. dht.begin();
  198. // seconds, minutes, hours, day of the week, day of the month, month, year  
  199. // NOTE ...  Use this for the inital time setting, once uploaded, comment the line below out and upload again
  200. // this prevent the time restting each time you restart the Arduino.
  201.  
  202. // seconds, minutes, hours, day of the week, day of the month, month, year
  203. // myRTC.setDS1302Time(00, 28, 17, 7, 27, 3, 2016);
  204.  
  205.   pixels.begin();
  206.   pixels.setBrightness(125);
  207.   pixels.show();
  208.  
  209.   TEMPpixels.begin();
  210.   TEMPpixels.setBrightness(100);
  211.   TEMPpixels.show();
  212.  
  213.  
  214.   int secs = myRTC.seconds;
  215.  
  216.   int sunittemp = secs % 10;
  217.   int stentemp = secs / 10;
  218.  
  219.   int munittemp = mins % 10;
  220.   int mtentemp = mins / 10;
  221.  
  222.   int hunittemp = hrs % 10;
  223.   int htentemp = hrs / 10;
  224.   pixels.setBrightness(128);
  225.  
  226. }
  227.  
  228. // seconds, minutes, hours, day of the week, day of the month, month, year
  229. // myRTC.setDS1302Time(00, 28, 17, 7, 27, 3, 2016);
  230. void timePlus(byte tOffset){
  231.   secs = myRTC.seconds;
  232.   mins = myRTC.minutes;
  233.   hrs = myRTC.hours;  
  234.   secs=secs+tOffset;
  235.   if (secs>=60){
  236.     secs=secs-60;
  237.     mins++;
  238.     if(mins==60){
  239.       hrs++;
  240.       if(hrs==24){
  241.         hrs=0;
  242.       }
  243.     }
  244.   }
  245.   myRTC.setDS1302Time(secs, mins, hrs, 7, 29, 5, 2022);
  246. }
  247.  
  248. void timeMinus(byte tOffset){
  249.   secs = myRTC.seconds;
  250.   mins = myRTC.minutes;
  251.   hrs = myRTC.hours;  
  252.   secs=secs-tOffset;
  253.   if (secs<=-1){
  254.     secs=secs+60;
  255.     mins--;
  256.     if(mins==-1){
  257.       mins=59;
  258.       hrs--;
  259.       if(hrs==-1){
  260.         hrs=23;
  261.       }
  262.     }
  263.   }
  264.   myRTC.setDS1302Time(secs, mins, hrs, 7, 29, 5, 2022);
  265. }
  266.  
  267. void tick(){
  268.   secs = myRTC.seconds;
  269.   mins = myRTC.minutes;
  270.   hrs = myRTC.hours;  
  271.   Serial.print(hrs);
  272.   Serial.print(":");
  273.   Serial.print(mins);
  274.   Serial.print(":");
  275.   Serial.print(secs);
  276.   Serial.print("     Temperature: ");
  277.   Serial.print(temp);
  278.   Serial.print(" *C\t ");
  279.   Serial.print("Humidity: ");
  280.   Serial.print(humi);
  281.   Serial.println(" %");
  282. }
  283.    
  284. void loop(){
  285.   currentMillis = millis();
  286.   if (currentMillis - previousMillis >= 1000) {   // Timer every 1 second due to display refresh
  287.     previousMillis = currentMillis;
  288.     tick();
  289.   }
  290.   buttonCheck1();
  291.   buttonCheck2();
  292.  
  293.   if (buttonShort1==true and readingButton1==HIGH){
  294.     buttonShort1=false;
  295.     //Serial.println("Plus Short Press");
  296.     //counter++;
  297.     timePlus(1);
  298.     //Serial.println(counter);
  299.   }
  300.   if (buttonLong1==true){
  301.     buttonLong1=false;
  302.     //Serial.println("Plus Long Press");
  303.     //counter++;
  304.     timePlus(5);
  305.     //Serial.println(counter);
  306.   }
  307.  
  308.  
  309.   if (buttonShort2==true and readingButton2==HIGH){
  310.     buttonShort2=false;
  311.     //Serial.println("Minus Short Press");
  312.     //counter--;
  313.     timeMinus(1);
  314.     //Serial.println(counter);
  315.   }
  316.   if (buttonLong2==true){
  317.     buttonLong2=false;
  318.     //Serial.println("Minus Long Press");
  319.     //counter--;
  320.     timeMinus(5);
  321.     //Serial.println(counter);
  322.   }
  323.  
  324.  
  325.  
  326. sensorValue = analogRead(analogInPin);  
  327. outputValue = map(sensorValue, 915, 1023, 75, 255);
  328. pixels.setBrightness(outputValue);
  329. pixels.show();
  330.  
  331. myRTC.updateTime();
  332.  
  333.  
  334. secs = myRTC.seconds;
  335. mins = myRTC.minutes;
  336. hrs = myRTC.hours;  
  337.  
  338. sunit = secs % 10;
  339. munit = mins % 10;
  340. hunit = hrs % 10;
  341.  
  342. sten = secs / 10;
  343. mten = mins / 10;
  344. hten = hrs / 10;
  345.  
  346.  
  347.  
  348. if (sunit != sunittemp)
  349. {
  350.  // Seconds Unit Itterator
  351.  clearSecondUnits();
  352.  
  353.  for (int i = 0; i < 3; i++)
  354.  {
  355.     pixels.setPixelColor(sunitArray[sunit][i], pixels.Color(255,0,0));
  356.     pixels.show();
  357.   }
  358.  sunittemp = sunit;
  359. }
  360.  
  361. if (sten != stentemp)
  362. {
  363.   if (sten >= 1 || sten == 0)
  364.   {
  365.    clearSecondTens();
  366.     for (int i = 0; i < 3; i++)
  367.     {
  368.       pixels.setPixelColor(stenArray[sten][i], pixels.Color(255,0,0));
  369.       pixels.show();
  370.     }
  371.   }
  372. stentemp = sten;
  373. UpdateTemperature();
  374. }
  375.  
  376.  
  377. ////   Minutes
  378.  
  379. // units
  380. if (munit != munittemp)
  381. {
  382.   clearMinuteUnits();
  383. for (int i = 0; i < 3; i++)
  384.     {
  385.       pixels.setPixelColor(munitArray[munit][i], pixels.Color(0,255,0));
  386.       pixels.show();
  387.     }
  388. munittemp = munit;
  389. }
  390.  
  391. // tens
  392. if (mten != mtentemp)
  393. {
  394.   clearMinuteTens();
  395.   for (int i = 0; i < 3; i++)
  396.   {
  397.     pixels.setPixelColor(mtenArray[mten][i], pixels.Color(0,255,0));
  398.     pixels.show();
  399.     // Serial.println(mins);
  400.   }
  401.  mtentemp = mten;
  402. }
  403.  
  404.  
  405. ///  HOURS
  406.  
  407.  
  408. // units
  409. if (hunit != hunittemp)
  410. {
  411.   clearHourUnits();
  412. for (int i = 0; i < 3; i++)
  413.     {
  414.       pixels.setPixelColor(hunitArray[hunit][i], pixels.Color(0,0,255));
  415.       pixels.show();
  416.     }
  417. hunittemp = hunit;    
  418. }
  419.  
  420. // tens
  421. if (hten != htentemp)
  422. {
  423.   clearHourTens();
  424.   for (int i = 0; i < 3; i++)
  425.   {
  426.     pixels.setPixelColor(htenArray[hten][i], pixels.Color(0,0,255));
  427.     pixels.show();
  428.     // Serial.println(mins);
  429.   }
  430.  htentemp = hten;
  431. }
  432.  
  433.  
  434.  
  435. ///  End loop
  436. }
  437.  
  438.  
  439.  
  440. void clearSecondUnits()
  441. {
  442.   for (int i = 0; i < (sizeof(secondLEDs) / sizeof(secondLEDs[0])); i++)
  443.   {
  444.     // Serial.println(i);
  445.     pixels.setPixelColor(secondLEDs[i], 0);
  446.   }
  447. }
  448.  
  449. void clearSecondTens()
  450. {
  451.   for (int i = 0; i < (sizeof(secondsTensLEDs) / sizeof(secondsTensLEDs[0])); i++)
  452.   {
  453.     pixels.setPixelColor(secondsTensLEDs[i], 0);
  454.   }
  455. }
  456.  
  457. void clearMinuteUnits()
  458. {
  459.   for (int i = 0; i < (sizeof(minuteLEDs) / sizeof(minuteLEDs[0])); i++)
  460.   {
  461.     pixels.setPixelColor(minuteLEDs[i], 0);
  462.   }
  463. }
  464.  
  465. void clearMinuteTens()
  466. {
  467.   for (int i = 0; i < (sizeof(minuteTensLEDs) / sizeof(minuteTensLEDs[0])); i++)
  468.   {
  469.     pixels.setPixelColor(minuteTensLEDs[i], 0);
  470.   }
  471. }
  472.  
  473.  
  474. void clearHourUnits()
  475. {
  476.   for (int i = 0; i < (sizeof(hourLEDs) / sizeof(hourLEDs[0])); i++)
  477.   {
  478.     pixels.setPixelColor(hourLEDs[i], 0);
  479.   }
  480. }
  481.  
  482. void clearHourTens()
  483. {
  484.   for (int i = 0; i < (sizeof(hourTensLEDs) / sizeof(hourTensLEDs[0])); i++)
  485.   {
  486.     pixels.setPixelColor(hourTensLEDs[i], 0);
  487.   }
  488. }
  489.  
  490. void UpdateTemperature()
  491. {
  492.  
  493.     // Reading temperature or humidity takes about 250 milliseconds!
  494.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  495.   // float h = dht.readHumidity();
  496.   humi = dht.readHumidity();
  497.   // Read temperature as Celsius (the default)
  498.   // float t = dht.readTemperature();
  499.   temp = dht.readTemperature();
  500.   // Read temperature as Fahrenheit (isFahrenheit = true)
  501.   // float f = dht.readTemperature(true);
  502.   // int f = dht.readTemperature(true);
  503.  
  504.   // Check if any reads failed and exit early (to try again).
  505.   if (isnan(humi) || isnan(temp)) {
  506.     Serial.println("Failed to read from DHT sensor!");
  507.     delay(500);
  508.     return;
  509.   }
  510.  
  511.   //Serial.print("Temperature: ");
  512.   //Serial.print(t);
  513.   //Serial.print(" *C\t ");
  514.   //Serial.print("Humidity: ");
  515.   //Serial.print(h);
  516.   //Serial.println(" %");
  517.  
  518.   for (int j = 0; j < 6; j++)
  519.     {
  520.       TEMPpixels.setPixelColor(temperatureArray[temp - 5][j], pixels.Color(255,0,125));
  521.  //     Serial.println(t);
  522.  //     Serial.println(temperatureArray[t][j]);
  523.       TEMPpixels.show();
  524.     }
  525.    
  526.  
  527. }
  528.  
  529. void buttonCheck1() {
  530.   readingButton1 = digitalRead(buttonPlus);
  531.   if (readingButton1==LOW){
  532.     if (repeat1==false){
  533.       if ((millis() - lastDebounceTime1) > debounceDelay) {
  534.         buttonShort1=true;
  535.       }
  536.       if ((millis() - lastDebounceTime1) > longPressDelay) {
  537.         //buttonLong1=true;
  538.         buttonShort1=false;
  539.         repeat1=true;
  540.       }
  541.     } //else {
  542.       if (millis()-lastDebounceTime1>repeatDelayDynamic) {
  543.         lastDebounceTime1=millis();
  544.         buttonLong1=true;
  545.         buttonShort1=false;
  546.         repeatDelayDynamic=repeatDelayDynamic-dynamic;
  547.         if (repeatDelayDynamic<10) repeatDelayDynamic=10;
  548.       }
  549.   }
  550.   if (readingButton1==HIGH) {
  551.     lastDebounceTime1=millis();
  552.     repeat1==false;
  553.     repeatDelayDynamic=repeatDelay;
  554.   }
  555. }
  556.  
  557.  
  558. void buttonCheck2() {
  559.   readingButton2 = digitalRead(buttonMinus);
  560.   if (readingButton2==LOW){
  561.     if (repeat2==false){
  562.       if ((millis() - lastDebounceTime2) > debounceDelay) {
  563.         buttonShort2=true;
  564.       }
  565.       if ((millis() - lastDebounceTime2) > longPressDelay) {
  566.         //buttonLong2=true;
  567.         buttonShort2=false;
  568.         repeat2=true;
  569.       }
  570.     } //else {
  571.       if (millis()-lastDebounceTime2>repeatDelayDynamic) {
  572.         lastDebounceTime2=millis();
  573.         buttonLong2=true;
  574.         buttonShort2=false;
  575.         repeatDelayDynamic=repeatDelayDynamic-dynamic;
  576.         if (repeatDelayDynamic<10) repeatDelayDynamic=10;
  577.       }
  578.   }
  579.   if (readingButton2==HIGH) {
  580.     lastDebounceTime2=millis();
  581.     repeat2==false;
  582.     repeatDelayDynamic=repeatDelay;
  583.   }
  584. }
  585.  
  586.  
  587. // END
RAW Paste Data Copied