Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- D3 - Trvalo 5V, (OUTPUT, trvalo HIGH), napajanie pre pull-up rezistory pre endstopy
- D4 - ovladanie H mostiku strana A (OUTPUT)
- D5 - ovladanie H mostiku strana B (OUTPUT)
- D6 - Endstop 1 (INPUT)
- D7 - Endstop 2 (INPUT)
- D8 - S1 (INPUT_PULLUP)
- D9 - S2 (INPUT_PULLUP)
- D10 - NC
- D11 - JP2 (INPUT_PULLUP)
- D12 - JP1 (INPUT_PULLUP)
- */
- #include <DS3231.h>
- #define HbridgeA 4
- #define HbridgeB 5
- #define EndstopOpened 6
- #define EndstopClosed 7
- #define Button1 8
- #define Button2 9
- #define Jumper1 12
- #define Jumper2 11
- DS3231 rtc(SDA, SCL);
- Time t;
- boolean dayState = 1; //Stav dna (den-1 / noc-0)
- string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- volatile unsigned short cas[2][12][31] = { //cas[rano/vecer][mesiac][den] = minuta
- { //rano
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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}
- },
- { //vecer
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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}
- }
- };
- void updateDayState() { //Funkcia pre zistenie ci je svetlo alebo nie
- t = rtc.getTime(); //Ziska aktualny cas z RTC modulu
- int currentMin = ((t.hour * 60) + t.min);
- int currentDay = (t.date);
- String currentMonthStr = rtc.getMonthStr();
- int currentMonth = 1;
- for (int i = 0; i < 12; i++) { //Premeni nazvy mesiacov na cisla (najde v tabulke)
- if (monthStr == month[i]) {
- currentMonth = i + 1; //Indexy mesiacov su od 1 do 12, pripocitame 1
- }
- }
- 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
- dayState = 1;
- }
- else {
- dayState = 0;
- }
- }
- void Hbridge(short state) {
- switch (state) {
- case 1: //Otvaranie dveri
- digitalWrite(HbridgeB, LOW);
- delayMicroseconds(10);
- for (int i = 1; i < 1000; i++) { //Softstart funkcia
- digitalWrite(HbridgeA, LOW);
- delayMicroseconds(1000 - i);
- digitalWrite(HbridgeA, HIGH);
- delayMicroseconds(i);
- }
- break;
- case 2: //Zatvaranie dveri
- digitalWrite(HbridgeA, LOW);
- delayMicroseconds(10);
- for (int i = 1; i < 1000; i++) { //Softstart funkcia
- digitalWrite(HbridgeB, LOW);
- delayMicroseconds(1000 - i);
- digitalWrite(HbridgeB, HIGH);
- delayMicroseconds(i);
- }
- break;
- default: //Vypnutie motoru
- digitalWrite(HbridgeA, LOW);
- digitalWrite(HbridgeB, LOW);
- break;
- }
- }
- void updateMotorPos() {
- if ((dayState == 1) && (digitalRead(EndstopOpened) == 0))
- delay(5); //Ak je den a dvere su otvorene nerob nic
- else if ((dayState == 0) && (digitalRead(EndstopClosed) == 0))
- delay(5); //Ak je noc a dvere su zatvorene nerob nic
- else if ((dayState == 1) && (digitalRead(EndstopOpened) == 1)) { //Ak je den a dvere niesu otvorene
- digitalWrite(LED_BUILTIN, HIGH);
- Hbridge(1); //Zacni otvarat dvere
- while (digitalRead(EndstopOpened) == 1) { //Cakaj kym neprepne endstop otvorene
- delay(1);
- }
- Hbridge(0); //Vypni motor
- digitalWrite(LED_BUILTIN, LOW);
- }
- else if ((dayState == 0) && (digitalRead(EndstopClosed) == 1)) { //Ak je noc a dvere niesu zatvorene
- digitalWrite(LED_BUILTIN, HIGH);
- Hbridge(2); //Zacni zatvarat dvere
- while (digitalRead(EndstopClosed) == 1) { //Cakaj kym neprepne endstop zatvorene
- delay(1);
- }
- Hbridge(0); //Vypni motor
- digitalWrite(LED_BUILTIN, LOW);
- }
- Hbridge(0);
- digitalWrite(LED_BUILTIN, LOW);
- }
- void setup() {
- pinMode(3, OUTPUT);
- digitalWrite(3, HIGH);
- pinMode(HbridgeA, OUTPUT);
- pinMode(HbridgeB, OUTPUT);
- pinMode(EndstopOpened, INPUT);
- pinMode(EndstopClosed, INPUT);
- pinMode(Button1, INPUT_PULLUP);
- pinMode(Button2, INPUT_PULLUP);
- pinMode(Jumper1, INPUT_PULLUP);
- pinMode(Jumper2, INPUT_PULLUP);
- rtc.begin();
- //Nastavenie času
- //rtc.setDOW(SUNDAY); //Nastavi den v tyzdni
- //rtc.setTime(17, 55, 30); //Nastavi cas (24h format)
- //rtc.setDate(19, 05, 2019); //Nastavi datum
- //Cas poslednej kalibracie hodin: Nedela, 19.05.2019, 17:55:30 (SEČ)
- Serial.begin(9600);
- if (digitalRead(Jumper2) == HIGH) { //Manualne ovladanie
- while (1) {
- if (digitalRead(Button1) == LOW) {
- Hbridge(1);
- while (digitalRead(Button1) == LOW) {
- delay(1);
- }
- Hbridge(0);
- }
- if (digitalRead(Button2) == LOW) {
- Hbridge(2);
- while (digitalRead(Button2) == LOW) {
- delay(1);
- }
- Hbridge(0);
- }
- }
- }
- }
- void loop() {
- updateDayState(); //Aktualizuje dayState
- updateMotorPos(); //Skontroluje stav otvorenia dveri a nastavi spravnu polohu motora
- for (int i = 0; i <= 60; i++) { //Caka 60 sekund na dalsiu aktualizaciu
- digitalWrite(LED_BUILTIN, HIGH);
- delay(50);
- digitalWrite(LED_BUILTIN, LOW);
- delay(950);
- }
- /*
- //Debugging
- Serial.print("Buttons: ");
- Serial.print(digitalRead(Button1));
- Serial.print(", ");
- Serial.println(digitalRead(Button2));
- Serial.print("Jumpers: ");
- Serial.print(digitalRead(Jumper1));
- Serial.print(", ");
- Serial.println(digitalRead(Jumper2));
- Serial.print("Endstops: ");
- Serial.print(digitalRead(EndstopOpened));
- Serial.print(", ");
- Serial.println(digitalRead(EndstopClosed));
- Serial.print("Actual time and date: ");
- Serial.print(rtc.getDateStr());
- Serial.print(" ");
- Serial.println(rtc.getTimeStr());
- Serial.print("dayState: ");
- Serial.println(dayState);
- delay(1000);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement