Advertisement
LshySVK

Otvarac dvierok pre kury V3

Apr 13th, 2023 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.37 KB | Source Code | 0 0
  1. /*
  2.    D3 - Trvalo 5V, (OUTPUT, trvalo HIGH), napajanie pre pull-up rezistory pre endstopy
  3.    D4 - ovladanie H mostiku strana A (OUTPUT)
  4.     D5 - ovladanie H mostiku strana B (OUTPUT)
  5.     D6 - Endstop 1 (INPUT)
  6.     D7 - Endstop 2 (INPUT)
  7.     D8 - S1 (INPUT_PULLUP)
  8.     D9 - S2 (INPUT_PULLUP)
  9.     D10 - NC
  10.     D11 - JP2 (INPUT_PULLUP)
  11.     D12 - JP1 (INPUT_PULLUP)
  12. */
  13.  
  14.  
  15. #include <DS3231.h>
  16.  
  17. #define HbridgeA 4
  18. #define HbridgeB 5
  19. #define EndstopOpened 6
  20. #define EndstopClosed 7
  21. #define Button1 8
  22. #define Button2 9
  23. #define Jumper1 12
  24. #define Jumper2 11
  25.  
  26.  
  27. DS3231 rtc(SDA, SCL);
  28. Time t;
  29.  
  30.  
  31. boolean dayState = 1; //Stav dna (den-1 / noc-0)
  32.  
  33.  
  34. string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  35.  
  36.  
  37. volatile unsigned short cas[2][12][31] = { //cas[rano/vecer][mesiac][den] = minuta
  38.     { //rano
  39.         {461, 461, 461, 461, 460, 460, 460, 460, 459, 459, 458, 458, 457, 456, 456, 455, 454, 454, 453, 452, 451, 450, 449, 448, 447, 446, 444, 443, 442, 441, 439},
  40.         {438, 437, 435, 434, 432, 431, 429, 428, 426, 424, 423, 421, 420, 418, 416, 414, 413, 411, 409, 407, 405, 403, 401, 400, 398, 396, 394, 392, 390, 0, 0},
  41.         {388, 386, 384, 382, 380, 378, 376, 374, 372, 369, 367, 365, 363, 361, 359, 357, 355, 353, 351, 348, 346, 344, 342, 340, 338, 336, 334, 332, 329, 327, 325},
  42.         {323, 321, 319, 317, 315, 313, 311, 309, 306, 304, 302, 300, 298, 296, 294, 292, 290, 288, 287, 285, 283, 281, 279, 277, 275, 273, 272, 270, 268, 266, 0},
  43.         {265, 263, 261, 260, 258, 256, 255, 253, 252, 250, 249, 247, 246, 245, 243, 242, 241, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 227},
  44.         {226, 225, 225, 224, 224, 223, 223, 222, 222, 222, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 222, 222, 222, 223, 223, 224, 224, 225, 225, 0},
  45.         {226, 226, 227, 228, 229, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 243, 244, 245, 246, 247, 249, 250, 251, 252, 254, 255, 256, 258},
  46.         {259, 260, 262, 263, 265, 266, 267, 269, 270, 272, 273, 274, 276, 277, 279, 280, 282, 283, 284, 286, 287, 289, 290, 292, 293, 294, 296, 297, 299, 300, 302},
  47.         {303, 304, 306, 307, 309, 310, 312, 313, 314, 316, 317, 319, 320, 322, 323, 324, 326, 327, 329, 330, 332, 333, 335, 336, 337, 339, 340, 342, 343, 345, 0},
  48.         {346, 348, 349, 351, 352, 354, 355, 357, 358, 360, 361, 363, 364, 366, 367, 369, 370, 372, 374, 375, 377, 378, 380, 381, 383, 385, 386, 388, 389, 391, 393},
  49.         {394, 396, 397, 399, 401, 402, 404, 405, 407, 409, 410, 412, 413, 415, 417, 418, 420, 421, 423, 424, 426, 427, 429, 430, 432, 433, 434, 436, 437, 438, 0},
  50.         {440, 441, 442, 443, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 454, 455, 456, 457, 457, 458, 458, 459, 459, 460, 460, 460, 460, 461, 461, 461, 461}
  51.     },
  52.     { //vecer
  53.         {996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1008, 1009, 1010, 1012, 1013, 1014, 1016, 1017, 1018, 1020, 1021, 1023, 1024, 1026, 1027, 1029, 1030, 1032, 1033, 1035},
  54.         {1036, 1038, 1039, 1041, 1042, 1044, 1046, 1047, 1049, 1050, 1052, 1053, 1055, 1057, 1058, 1060, 1061, 1063, 1064, 1066, 1068, 1069, 1071, 1072, 1074, 1076, 1077, 1079, 1080, 0, 0},
  55.         {1082, 1083, 1085, 1086, 1088, 1090, 1091, 1093, 1094, 1096, 1097, 1099, 1100, 1102, 1104, 1105, 1107, 1108, 1110, 1111, 1113, 1114, 1116, 1118, 1119, 1121, 1122, 1124, 1125, 1127, 1129},
  56.         {1130, 1132, 1133, 1135, 1136, 1138, 1140, 1141, 1143, 1144, 1146, 1148, 1149, 1151, 1153, 1154, 1156, 1157, 1159, 1161, 1162, 1164, 1166, 1167, 1169, 1171, 1172, 1174, 1176, 1177, 0},
  57.         {1179, 1181, 1182, 1184, 1185, 1187, 1189, 1190, 1192, 1194, 1195, 1197, 1198, 1200, 1201, 1203, 1204, 1206, 1207, 1209, 1210, 1212, 1213, 1214, 1216, 1217, 1218, 1220, 1221, 1222, 1223},
  58.         {1224, 1226, 1227, 1228, 1229, 1230, 1230, 1231, 1232, 1233, 1234, 1234, 1235, 1235, 1236, 1236, 1237, 1237, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1237, 0},
  59.         {1237, 1236, 1236, 1235, 1235, 1234, 1234, 1233, 1232, 1231, 1230, 1229, 1228, 1227, 1226, 1225, 1224, 1223, 1222, 1220, 1219, 1218, 1216, 1215, 1214, 1212, 1211, 1209, 1207, 1206, 1204},
  60.         {1203, 1201, 1199, 1197, 1196, 1194, 1192, 1190, 1188, 1186, 1184, 1183, 1181, 1179, 1177, 1175, 1173, 1171, 1169, 1167, 1164, 1162, 1160, 1158, 1156, 1154, 1152, 1150, 1148, 1145, 1143},
  61.         {1141, 1139, 1137, 1135, 1132, 1130, 1128, 1126, 1124, 1121, 1119, 1117, 1115, 1113, 1110, 1108, 1106, 1104, 1102, 1099, 1097, 1095, 1093, 1091, 1089, 1087, 1084, 1082, 1080, 1078, 0},
  62.         {1076, 1074, 1072, 1070, 1068, 1066, 1063, 1061, 1059, 1057, 1055, 1053, 1051, 1050, 1048, 1046, 1044, 1042, 1040, 1038, 1036, 1035, 1033, 1031, 1029, 1028, 1026, 1024, 1023, 1021, 1020},
  63.         {1018, 1016, 1015, 1014, 1012, 1011, 1009, 1008, 1007, 1005, 1004, 1003, 1002, 1001, 999, 998, 997, 996, 995, 995, 994, 993, 992, 991, 991, 990, 989, 989, 988, 988, 0},
  64.         {987, 987, 987, 986, 986, 986, 986, 986, 985, 985, 985, 986, 986, 986, 986, 986, 987, 987, 987, 988, 988, 989, 989, 990, 991, 991, 992, 993, 994, 995, 995}
  65.     }
  66. };
  67.  
  68.  
  69.  
  70.  
  71. void updateDayState() {                    //Funkcia pre zistenie ci je svetlo alebo nie
  72.     t = rtc.getTime(); //Ziska aktualny cas z RTC modulu
  73.     int currentMin = ((t.hour * 60) + t.min);
  74.     int currentDay = (t.date);
  75.     String currentMonthStr = rtc.getMonthStr();
  76.     int currentMonth = 1;
  77.  
  78.     for (int i = 0; i < 12; i++) { //Premeni nazvy mesiacov na cisla (najde v tabulke)
  79.                if (monthStr == month[i]) {
  80.                 currentMonth = i + 1; //Indexy mesiacov su od 1 do 12, pripocitame 1
  81.                }
  82.     }
  83.  
  84.     if ((currentMin >= (cas[1][(currentMonth - 1)][(currentDay - 1)])) && (currentMin <= (cas[2][(currentMonth - 1)][(currentDay - 1)]))) { //Najdolezitejsia podmienka, ak je aktualna minuta medzi minutami v noci, zmeni premenu dayState
  85.         dayState = 1;
  86.     }
  87.     else {
  88.         dayState = 0;
  89.     }
  90. }
  91.  
  92.  
  93. void Hbridge(short state) {
  94.     switch (state) {
  95.     case 1: //Otvaranie dveri
  96.         digitalWrite(HbridgeB, LOW);
  97.         delayMicroseconds(10);
  98.         for (int i = 1; i < 1000; i++) { //Softstart funkcia
  99.             digitalWrite(HbridgeA, LOW);
  100.             delayMicroseconds(1000 - i);
  101.             digitalWrite(HbridgeA, HIGH);
  102.             delayMicroseconds(i);
  103.         }
  104.         break;
  105.     case 2: //Zatvaranie dveri
  106.         digitalWrite(HbridgeA, LOW);
  107.         delayMicroseconds(10);
  108.         for (int i = 1; i < 1000; i++) { //Softstart funkcia
  109.             digitalWrite(HbridgeB, LOW);
  110.             delayMicroseconds(1000 - i);
  111.             digitalWrite(HbridgeB, HIGH);
  112.             delayMicroseconds(i);
  113.         }
  114.         break;
  115.     default: //Vypnutie motoru
  116.         digitalWrite(HbridgeA, LOW);
  117.         digitalWrite(HbridgeB, LOW);
  118.         break;
  119.     }
  120. }
  121.  
  122.  
  123. void updateMotorPos() {
  124.     if ((dayState == 1) && (digitalRead(EndstopOpened) == 0))
  125.         delay(5); //Ak je den a dvere su otvorene nerob nic
  126.     else if ((dayState == 0) && (digitalRead(EndstopClosed) == 0))
  127.         delay(5); //Ak je noc a dvere su zatvorene nerob nic
  128.  
  129.     else if ((dayState == 1) && (digitalRead(EndstopOpened) == 1)) { //Ak je den a dvere niesu otvorene
  130.         digitalWrite(LED_BUILTIN, HIGH);
  131.         Hbridge(1); //Zacni otvarat dvere
  132.         while (digitalRead(EndstopOpened) == 1) { //Cakaj kym neprepne endstop otvorene
  133.             delay(1);
  134.         }
  135.         Hbridge(0); //Vypni motor
  136.         digitalWrite(LED_BUILTIN, LOW);
  137.     }
  138.  
  139.     else if ((dayState == 0) && (digitalRead(EndstopClosed) == 1)) { //Ak je noc a dvere niesu zatvorene
  140.         digitalWrite(LED_BUILTIN, HIGH);
  141.         Hbridge(2); //Zacni zatvarat dvere
  142.         while (digitalRead(EndstopClosed) == 1) { //Cakaj kym neprepne endstop zatvorene
  143.             delay(1);
  144.         }
  145.         Hbridge(0); //Vypni motor
  146.         digitalWrite(LED_BUILTIN, LOW);
  147.     }
  148.     Hbridge(0);
  149.     digitalWrite(LED_BUILTIN, LOW);
  150. }
  151.  
  152.  
  153.  
  154.  
  155. void setup() {
  156.     pinMode(3, OUTPUT);
  157.     digitalWrite(3, HIGH);
  158.     pinMode(HbridgeA, OUTPUT);
  159.     pinMode(HbridgeB, OUTPUT);
  160.     pinMode(EndstopOpened, INPUT);
  161.     pinMode(EndstopClosed, INPUT);
  162.     pinMode(Button1, INPUT_PULLUP);
  163.     pinMode(Button2, INPUT_PULLUP);
  164.     pinMode(Jumper1, INPUT_PULLUP);
  165.     pinMode(Jumper2, INPUT_PULLUP);
  166.  
  167.     rtc.begin();
  168.  
  169.     //Nastavenie času
  170.     //rtc.setDOW(SUNDAY);           //Nastavi den v tyzdni
  171.     //rtc.setTime(17, 55, 30);      //Nastavi cas (24h format)
  172.     //rtc.setDate(19, 05, 2019);    //Nastavi datum
  173.     //Cas poslednej kalibracie hodin: Nedela, 19.05.2019, 17:55:30 (SEČ)
  174.  
  175.     Serial.begin(9600);
  176.  
  177.     if (digitalRead(Jumper2) == HIGH) { //Manualne ovladanie
  178.         while (1) {
  179.             if (digitalRead(Button1) == LOW) {
  180.                 Hbridge(1);
  181.                 while (digitalRead(Button1) == LOW) {
  182.                     delay(1);
  183.                 }
  184.                 Hbridge(0);
  185.             }
  186.             if (digitalRead(Button2) == LOW) {
  187.                 Hbridge(2);
  188.                 while (digitalRead(Button2) == LOW) {
  189.                     delay(1);
  190.                 }
  191.                 Hbridge(0);
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197.  
  198.  
  199.  
  200. void loop() {
  201.     updateDayState(); //Aktualizuje dayState
  202.  
  203.     updateMotorPos(); //Skontroluje stav otvorenia dveri a nastavi spravnu polohu motora
  204.  
  205.     for (int i = 0; i <= 60; i++) { //Caka 60 sekund na dalsiu aktualizaciu
  206.         digitalWrite(LED_BUILTIN, HIGH);
  207.         delay(50);
  208.         digitalWrite(LED_BUILTIN, LOW);
  209.         delay(950);
  210.     }
  211.  
  212.     /*
  213.     //Debugging
  214.     Serial.print("Buttons: ");
  215.     Serial.print(digitalRead(Button1));
  216.     Serial.print(", ");
  217.     Serial.println(digitalRead(Button2));
  218.     Serial.print("Jumpers: ");
  219.     Serial.print(digitalRead(Jumper1));
  220.     Serial.print(", ");
  221.     Serial.println(digitalRead(Jumper2));
  222.     Serial.print("Endstops: ");
  223.     Serial.print(digitalRead(EndstopOpened));
  224.     Serial.print(", ");
  225.     Serial.println(digitalRead(EndstopClosed));
  226.     Serial.print("Actual time and date: ");
  227.     Serial.print(rtc.getDateStr());
  228.     Serial.print("     ");
  229.     Serial.println(rtc.getTimeStr());
  230.     Serial.print("dayState: ");
  231.     Serial.println(dayState);
  232.     delay(1000);
  233.     */
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement