manhoosbilli1

WORKING MOTOR CODE

Jul 22nd, 2019
222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You are free to modify the code as much as you want and share it with other people if you want. Try to add any improvements and bugs to the original git hub repository so that i can take a look as well, of course this is not a requirement.
  3. do not delete this comment section, as this will help the next reader link to the original post and original content. This is necessary to avoid any confusion later on. Happy tinkering!
  4. Youtube Channel: https://www.youtube.com/channel/UCtvpQinm9lOqgLaEp4WIo-w?
  5. Github Repository: https://github.com/manhoosbilli1/Arduino-Incubator
  6. Arduino-forum thread: https://forum.arduino.cc/index.php?topic=631158.0
  7. // Written by manhoosbilli1 aka kaka, public domain
  8. */
  9.  
  10.  
  11. #include <Bounce2.h>
  12. #include <Wire.h>
  13. #include "RTClib.h"
  14. #include <TimeLib.h>
  15. #include <TimeAlarms.h>
  16.  
  17. AlarmId id;
  18. RTC_DS3231 rtc;
  19.  
  20. #define motorRightPin 4
  21. #define motorLeftPin 3
  22. #define limitSwitchLeftPin 9
  23. #define limitSwitchRightPin 8
  24. int hour_now;
  25. int minute_now;
  26. int second_now;
  27. int month_now;
  28. int year_now;
  29. int day_now;
  30.  
  31.  
  32. bool limitSwitchRightState;
  33. bool limitSwitchLeftState;
  34.  
  35.  
  36.  
  37. bool startMotor = false;
  38. Bounce limitSwitchLeft = Bounce();
  39. Bounce limitSwitchRight = Bounce();
  40.  
  41.  
  42.  
  43. void setup() {
  44. // syncing the time of arduino with time of RTC
  45.  
  46. DateTime now = rtc.now();
  47. hour_now = now.hour();
  48. minute_now = now.minute();
  49. second_now = now.second();
  50. month_now = now.month();
  51. year_now = now.year();
  52. day_now = now.day();
  53.  
  54. setTime(hour_now,minute_now,second_now,month_now,day_now,year_now);
  55.  
  56. pinMode(motorRightPin, OUTPUT);
  57. pinMode(motorLeftPin, OUTPUT);
  58. digitalWrite(motorRightPin, LOW);
  59. digitalWrite(motorLeftPin, LOW);
  60.  
  61. limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  62. limitSwitchLeft.interval(50);
  63.  
  64. limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  65. limitSwitchRight.interval(50);
  66.  
  67. //using alarm library which lets me set alarm and execute a specific function at specifc time
  68.  
  69. Alarm.timerRepeat(10, turnEggFlag); //4 HOURS PER TURN
  70.  
  71.  
  72.  
  73. Serial.begin(57600);
  74. Serial.println("Incubator starting. motor is now at rest");
  75.  
  76. //RTC FUCNTIONS BELOW. necessary
  77.  
  78. if(! rtc.begin())
  79. {
  80. Serial.println("Couldn't find RTC");
  81. while(1);
  82. }
  83.  
  84. if(rtc.lostPower())
  85. {
  86. Serial.println("RTC lost power");
  87. // following line sets the RTC to the date & time this sketch was compiled
  88. //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  89. // This line sets the RTC with an explicit date & time, for example to set
  90. // January 21, 2014 at 3am you would call:
  91. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  92. }
  93.  
  94.  
  95. //RTC FUNCTIONS ABOVE
  96.  
  97. //if motor isn't on one of the switches, move the tray to one of the switches
  98. while(digitalRead(limitSwitchLeftPin) == HIGH && digitalRead(limitSwitchRightPin) == HIGH )
  99. {
  100. digitalWrite(motorRightPin, LOW);
  101. digitalWrite(motorLeftPin, HIGH);
  102. Serial.println("Mtr calibrating");
  103. }
  104.  
  105. digitalWrite(motorRightPin, LOW);
  106. digitalWrite(motorLeftPin, LOW);
  107. Serial.println("Calibration OK");
  108.  
  109. delay(2000);
  110. }
  111.  
  112. enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
  113.  
  114. int currentState = STATE_IDLE; //giving names to cases in switch case
  115.  
  116.  
  117. void loop() {
  118. // initializing time and updating
  119. DateTime now = rtc.now();
  120. hour_now = now.hour();
  121. minute_now = now.minute();
  122. second_now = now.second();
  123. month_now = now.month();
  124. year_now = now.year();
  125. day_now = now.day();
  126. limitSwitchLeft.update();
  127. limitSwitchRight.update(); //reading the debounce state of switches
  128. limitSwitchLeftState = limitSwitchLeft.read(); //switch are input pullup. high state is zero
  129. limitSwitchRightState = limitSwitchRight.read();
  130. turn(); //turn the eggs
  131. Alarm.delay(50); //works as a normal delay to slow down arduino
  132. }
  133.  
  134. void turn(){
  135. if (startMotor == true){
  136. switch (currentState) {
  137. case STATE_IDLE:
  138. if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW){
  139. Serial.println("Moving right now");
  140. digitalWrite(motorRightPin, HIGH);
  141. digitalWrite(motorLeftPin, LOW);
  142. currentState = STATE_MOVING_RIGHT;
  143. }
  144. if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH){
  145. Serial.println("Moving left now");
  146. digitalWrite(motorRightPin, LOW);
  147. digitalWrite(motorLeftPin, HIGH);
  148. currentState = STATE_MOVING_LEFT;
  149. }
  150.  
  151. break;
  152.  
  153. case STATE_MOVING_RIGHT:
  154. // moving right so only check right limit switch
  155. if (limitSwitchRightState == HIGH && limitSwitchLeftState == LOW) {
  156. Serial.println("Motor reached right. motor stopping");
  157. digitalWrite(motorRightPin, LOW);
  158. digitalWrite(motorLeftPin, LOW);
  159. startMotor = false;
  160. currentState = STATE_IDLE;
  161. break;
  162. }
  163. break;
  164.  
  165. case STATE_MOVING_LEFT:
  166. // moving left so only check left limit switch
  167.  
  168. if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW){
  169. Serial.println("Motor has reached left. Motor stopping");
  170. digitalWrite(motorRightPin, LOW);
  171. digitalWrite(motorLeftPin, LOW);
  172. startMotor = false;
  173. currentState = STATE_IDLE;
  174. break;
  175. }
  176. }
  177. }
  178. }
  179.  
  180. void turnEggFlag() {
  181. Serial.println("Turned on the flag");
  182. startMotor = true;
  183. }
RAW Paste Data Copied