manhoosbilli1

motor code with timed event

Jun 7th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include <Bounce2.h>
  2. #include <Wire.h>
  3. #include "RTClib.h"
  4. RTC_DS3231 rtc;
  5.  
  6. const byte motorRightPin = 4;
  7. const byte motorLeftPin = 3;
  8. const byte limitSwitchLeftPin = 7;
  9. const byte limitSwitchRightPin = 8;
  10. const byte triggerPin = 11;
  11. const int on_hour = 19;
  12. const int on_minute = 35;
  13.  
  14. bool startMotor = false;
  15. Bounce limitSwitchLeft = Bounce();
  16. Bounce limitSwitchRight = Bounce();
  17. Bounce triggerButton = Bounce(); //trigger for motor
  18.  
  19. void setup() {
  20. // put your setup code here, to run once
  21.  
  22. pinMode(motorRightPin, OUTPUT);
  23. pinMode(motorLeftPin, OUTPUT);
  24. digitalWrite(motorRightPin, LOW);
  25. digitalWrite(motorLeftPin, LOW);
  26.  
  27. limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  28. limitSwitchLeft.interval(50);
  29.  
  30. limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  31. limitSwitchRight.interval(50);
  32.  
  33. triggerButton.attach(triggerPin, INPUT_PULLUP);
  34. triggerButton.interval(25);
  35.  
  36. Serial.begin(9600);
  37. //assuming motor is already one one junction
  38. Serial.println("Incubator starting. motor is now at rest");
  39. if(! rtc.begin()) {
  40. Serial.println("Couldn't find RTC");
  41. while(1);
  42. }
  43.  
  44. if(rtc.lostPower()) {
  45. Serial.println("RTC lost power");
  46. // following line sets the RTC to the date & time this sketch was compiled
  47. //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  48. // This line sets the RTC with an explicit date & time, for example to set
  49. // January 21, 2014 at 3am you would call:
  50. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  51. }
  52. while(digitalRead(limitSwitchLeftPin) == HIGH && digitalRead(limitSwitchRightPin) == HIGH )
  53. {
  54. digitalWrite(motorRightPin, LOW);
  55. digitalWrite(motorLeftPin, HIGH);
  56. //lcd.print("Mtr calibrating");
  57. }
  58. digitalWrite(motorRightPin, LOW);
  59. digitalWrite(motorLeftPin, LOW);
  60. //lcd.print("Calibration OK");
  61.  
  62. delay(2000);
  63. }
  64.  
  65. enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
  66.  
  67. int currentState = STATE_IDLE;
  68.  
  69. void loop() {
  70. // put your main code here, to run repeatedly:
  71. DateTime now = rtc.now();
  72. int hour_now = now.hour();
  73. int minute_now = now.minute();
  74. int seconds_now = now.second();
  75.  
  76. limitSwitchLeft.update();
  77. limitSwitchRight.update();
  78. triggerButton.update();
  79.  
  80. bool limitSwitchLeftState = limitSwitchLeft.read(); //switch are input pullup. high state is zero
  81. bool limitSwitchRightState = limitSwitchRight.read();
  82.  
  83.  
  84. if(on_hour == hour_now && on_minute == minute_now)
  85. {
  86. startMotor = true;
  87. } else
  88. if (on_minute < minute_now)
  89. {
  90. startMotor = false;
  91. Serial.println("startmotor trigger has been turned off");
  92. digitalWrite(motorRightPin, LOW);
  93. digitalWrite(motorLeftPin, LOW);
  94. }
  95.  
  96. if (startMotor == true)
  97. {
  98. switch (currentState) {
  99. case STATE_IDLE:
  100. // motor is off, waiting for trigger button to be pressed
  101.  
  102.  
  103. Serial.println("button to trigger start motor has been presssed");
  104. // figure out which way to move
  105. if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
  106. {
  107. Serial.println("Moving right now");
  108. digitalWrite(motorRightPin, HIGH);
  109. digitalWrite(motorLeftPin, LOW);
  110. currentState = STATE_MOVING_RIGHT;
  111. }
  112. else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
  113. {
  114. Serial.println("Moving left now");
  115. digitalWrite(motorRightPin, LOW);
  116. digitalWrite(motorLeftPin, HIGH);
  117. currentState = STATE_MOVING_LEFT;
  118. }
  119.  
  120. break;
  121.  
  122. case STATE_MOVING_RIGHT:
  123. // moving right so only check right limit switch
  124. if (limitSwitchRightState == HIGH && limitSwitchLeftState == LOW)
  125. {
  126. Serial.println("Motor reached right. motor stopping");
  127. digitalWrite(motorRightPin, LOW);
  128. digitalWrite(motorLeftPin, LOW);
  129. startMotor = false;
  130.  
  131. }
  132. break;
  133.  
  134. case STATE_MOVING_LEFT:
  135. // moving left so only check left limit switch
  136.  
  137. if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
  138. {
  139. Serial.println("Motor has reached left. Motor stopping");
  140. digitalWrite(motorRightPin, LOW);
  141. digitalWrite(motorLeftPin, LOW);
  142. startMotor = false;
  143.  
  144. }
  145. break;
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment