sketch.ino 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Lib AVR pour sleep_mode...
  2. #include <avr/power.h>
  3. #include <avr/sleep.h>
  4. #include <Stepper.h>
  5. #include <DS3231.h>
  6. #include "../lib/calsol.h"
  7. // Pour steppers
  8. #define STEP_REV 400
  9. #define REV 10
  10. const int stepsPerRevolution = 400;
  11. // 360 / 0.9deg
  12. Stepper myStepper1(stepsPerRevolution, 6, 7, 8, 9);
  13. Stepper myStepper2(stepsPerRevolution, 2, 3, 4, 5);
  14. // Module DS3231 pour l'heure
  15. DS3231 rtc(4, 5);
  16. int interruptPin = 2;
  17. int sens_ouverture = 2; //etat indeterminé
  18. struct Date_t {
  19. unsigned long annee;
  20. unsigned long mois;
  21. unsigned long jour;
  22. };
  23. /*
  24. * Variable globales pour interruptions
  25. */
  26. void ouvrir()
  27. {
  28. sens_ouverture = 1;
  29. }
  30. void fermer() {
  31. sens_ouverture = 0;
  32. }
  33. /********************************
  34. * Fonction d'accès à la porte
  35. * val = 0, fermer la porte
  36. * val = 1, ouvrir la porte
  37. */
  38. void ouvrirPorte(bool val) {
  39. myStepper1.setSpeed(100);
  40. myStepper2.setSpeed(100);
  41. if (val == false) {
  42. for(int i=0; i<=STEP_REV*REV; i++) {
  43. myStepper1.step(1);
  44. myStepper2.step(-1);
  45. }
  46. }
  47. if (val == true) {
  48. for(int i=0; i<=STEP_REV*REV; i++) {
  49. myStepper1.step(-1);
  50. myStepper2.step(1);
  51. }
  52. }
  53. // Stepper OFF
  54. for(int i=2; i<=9; i++) {
  55. digitalWrite(i, LOW);
  56. }
  57. }
  58. void clearSerial() {
  59. Serial.begin(115200);
  60. Serial.write(27); // ESC command
  61. Serial.print("[2J"); // clear screen command
  62. Serial.write(27);
  63. Serial.print("[H"); // cursor to home command
  64. }
  65. void setup() {
  66. /*
  67. * On prépare le terrain pour le PIN qui nécéssite une resistance de PullUp
  68. * L'interruption est déclarée dans le loop()
  69. */
  70. pinMode(interruptPin, INPUT_PULLUP);
  71. rtc.begin();
  72. /*
  73. * The following lines can be uncommented to set the date and time
  74. */
  75. //setRTC();
  76. }
  77. void setRTC() {
  78. rtc.setDOW(THURSDAY); // Set Day-of-Week to SUNDAY
  79. rtc.setTime(9, 54, 0); // Set the time to 12:00:00 (24hr format)
  80. rtc.setDate(20, 4, 2019); // Set the date to January 1st, 2014
  81. }
  82. void loop() {
  83. // Serial.print(rtc.getDOWStr());
  84. clearSerial();
  85. Serial.println("-=-=-=-=-=-=- INITIALIZATION -=-=-=-=-=-=-=-");
  86. Time t;
  87. t = rtc.getTime();
  88. int myval;
  89. myval = sizeof(DateSol_t) / sizeof(DateSol_t[0]);
  90. Serial.println();
  91. Serial.print("=> Taille DateSol_t : ");
  92. Serial.println( myval );
  93. Serial.println();
  94. Serial.print("=> Date : ");
  95. Serial.println(rtc.getDateStr());
  96. Serial.print("=> Heure : ");
  97. Serial.println(rtc.getTimeStr());
  98. Serial.println();
  99. /*
  100. * Attention on met toujours + 1 minute par rapport à l'heure dans le registre
  101. * Changé !
  102. */
  103. for (int i=0 ; i < sizeof(DateSol_t) / sizeof(DateSol_t[0]) ; i++) {
  104. if (t.year == DateSol_t[i][0]) {
  105. if (t.mon == DateSol_t[i][1]) {
  106. if (t.date == DateSol_t[i][2]) {
  107. if (t.hour > DateSol_t[i][3] && t.hour < DateSol_t[i][5] ) {
  108. /*
  109. * On test Annee, mois, jour et minute
  110. * Si boot entre ouverture et fermeture =>set alarm au soir => Fermeture
  111. * Si boot après fermeture mais le même jour => set alarm a j+1 => ouverture
  112. * Si boot avant ouverture du matin => set alarm au matin => Ouverture
  113. *
  114. */
  115. // Cas de figure 1
  116. Serial.println("=> Night (brrrr) is comming... DEV");
  117. Serial.print("=> Setting Alarm1 registers @ ");
  118. Serial.print( DateSol_t[i][5] );
  119. Serial.print(" : ");
  120. Serial.println( DateSol_t[i][6] );
  121. rtc.setAlarm1Time(t.hour, (t.min + 1));
  122. rtc.setControl();
  123. rtc.resetAlarm();
  124. // Attachement d'une interruption sur front descendant de INT0
  125. attachInterrupt(INT0, fermer, FALLING);
  126. }
  127. // Cas de figure 2
  128. else if ( t.hour == DateSol_t[i][5] && t.min > DateSol_t[i][6] ) {
  129. Serial.println("=> Night is engaged, next step tomorrow morning...");
  130. Serial.print("=> Setting Alarm1 registers @ ");
  131. Serial.print( DateSol_t[i+1][3] );
  132. Serial.print(" : ");
  133. Serial.println( DateSol_t[i+1][4] );
  134. rtc.setAlarm1Time(DateSol_t[i+1][3], DateSol_t[i+1][4]);
  135. rtc.setControl();
  136. rtc.resetAlarm();
  137. // Attachement d'une interruption sur front descendant de INT0
  138. attachInterrupt(INT0, ouvrir, FALLING);
  139. }
  140. // Cas de figure 3
  141. else if ( t.hour == DateSol_t[i][5] && t.min < DateSol_t[i][6] ) {
  142. Serial.println("=> Night (brrrr) is comming...");
  143. Serial.print("=> Setting Alarm1 registers @ ");
  144. Serial.print( DateSol_t[i][5] );
  145. Serial.print(" : ");
  146. Serial.println( DateSol_t[i][6] );
  147. rtc.setAlarm1Time(DateSol_t[i][5], DateSol_t[i][6]);
  148. rtc.setControl();
  149. rtc.resetAlarm();
  150. // Attachement d'une interruption sur front descendant de INT0
  151. attachInterrupt(INT0, fermer, FALLING);
  152. }
  153. // Cas de figure 4
  154. else if ( t.hour == DateSol_t[i][3] && t.min < DateSol_t[i][4] ) {
  155. Serial.println("=> Day is comming...");
  156. Serial.print("=> Setting Alarm1 registers @ ");
  157. Serial.print( DateSol_t[i][3] );
  158. Serial.print(" : ");
  159. Serial.println( DateSol_t[i][4] );
  160. rtc.setAlarm1Time(DateSol_t[i][3], DateSol_t[i][4]);
  161. rtc.setControl();
  162. rtc.resetAlarm();
  163. // Attachement d'une interruption sur front descendant de INT0
  164. attachInterrupt(INT0, ouvrir, FALLING);
  165. }
  166. else if ( t.hour == DateSol_t[i][3] && t.min > DateSol_t[i][4] ) {
  167. Serial.println("=> Night (brrrr) is comming...");
  168. Serial.print("=> Setting Alarm1 registers @ ");
  169. Serial.print( DateSol_t[i][5] );
  170. Serial.print(" : ");
  171. Serial.println( DateSol_t[i][6] );
  172. rtc.setAlarm1Time(DateSol_t[i][5], DateSol_t[i][6]);
  173. rtc.setControl();
  174. rtc.resetAlarm();
  175. // Attachement d'une interruption sur front descendant de INT0
  176. attachInterrupt(INT0, fermer, FALLING);
  177. }
  178. else if ( t.hour < DateSol_t[i][3] ) {
  179. Serial.println("=> Day is comming...");
  180. Serial.print("=> Setting Alarm1 registers @ ");
  181. Serial.print( DateSol_t[i][3] );
  182. Serial.print(" : ");
  183. Serial.println( DateSol_t[i][4] );
  184. rtc.setAlarm1Time(DateSol_t[i][3], DateSol_t[i][4]);
  185. rtc.setControl();
  186. rtc.resetAlarm();
  187. // Attachement d'une interruption sur front descendant de INT0
  188. attachInterrupt(INT0, ouvrir, FALLING);
  189. }
  190. else if ( t.hour > DateSol_t[i][3] ) {
  191. Serial.println("=> Night (brrrr) is comming...");
  192. Serial.print("=> Setting Alarm1 registers @ ");
  193. Serial.print( DateSol_t[i][5] );
  194. Serial.print(" : ");
  195. Serial.println( DateSol_t[i][6] );
  196. rtc.setAlarm1Time(DateSol_t[i][5], DateSol_t[i][6]);
  197. rtc.setControl();
  198. rtc.resetAlarm();
  199. // Attachement d'une interruption sur front descendant de INT0
  200. attachInterrupt(INT0, fermer, FALLING);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. /*
  207. * Slepping mode et attente du réveil de l'intérruption.
  208. * TODO
  209. */
  210. if (sens_ouverture == 1) {
  211. Serial.println("-=-=-=-=- CA OUVRE !!!!!! -=-=-=-=-=-=-");
  212. ouvrirPorte(1);
  213. }
  214. if (sens_ouverture == 0) {
  215. Serial.println("-=-=-=-=- CA FERME !!!!!! -=-=-=-=-=-=-");
  216. ouvrirPorte(0);
  217. }
  218. sens_ouverture = 2;
  219. // Configuration du type de sleep
  220. // set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  221. // sleep_enable();
  222. // Activation du mode sleep
  223. // sleep_mode();
  224. //attachInterrupt(INT0, fermer, LOW);
  225. // CPU en mode sleep,
  226. // Attente de l'interruption INT0 qui réveillera le CPU
  227. // Désactivation du mode sleep
  228. // sleep_disable();
  229. /*
  230. Serial.print(" ");
  231. // Send date
  232. Serial.print(rtc.getDateStr());
  233. Serial.print(" -- ");
  234. // Send time
  235. Serial.println(rtc.getTimeStr());
  236. Serial.print("Alarm 1 : ");
  237. Serial.println(rtc.getAlarm1Str());
  238. // Wait one second before repeating :) */
  239. delay (5000);
  240. clearSerial();
  241. }