manhoosbilli1

Working motor code for incubator

Jun 6th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include <Bounce2.h>
  2.  
  3. //int function = 0;
  4. //bool triggerStart = false;   //trigger to start motor
  5. //bool triggerStop = false;   // trigger to stop motor
  6. //bool goRight = false;
  7. //bool goLeft = false;
  8. //bool isTime = false;
  9.  
  10. const byte motorRightPin = 4;
  11. const byte motorLeftPin = 3;
  12. const byte limitSwitchLeftPin = 7;
  13. const byte limitSwitchRightPin = 8;
  14. const byte triggerPin = 11;
  15.  
  16. //bool limitSwitchLeftState;
  17. //bool limitSwitchRightState;
  18. //bool triggerState;
  19. //bool prevS1State = 0;
  20. //bool prevS2State = 0;
  21. //bool startMotor = 0;
  22.  
  23. //bool wasGoingLeft;
  24. //bool wasGoingRight;
  25. //bool motorStopped;
  26.  
  27. //bool high = LOW; //since switches are inverted. when its low. it will be high
  28. //bool low = HIGH;
  29.  
  30. Bounce limitSwitchLeft = Bounce();
  31. Bounce limitSwitchRight = Bounce();
  32. Bounce triggerButton = Bounce();  //trigger for motor
  33.  
  34. void setup() {
  35.   // put your setup code here, to run once
  36.  
  37.   pinMode(motorRightPin, OUTPUT);
  38.   pinMode(motorLeftPin, OUTPUT);
  39.   digitalWrite(motorRightPin, LOW);
  40.   digitalWrite(motorLeftPin, LOW);
  41.  
  42.   limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  43.   limitSwitchLeft.interval(50);
  44.  
  45.   limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  46.   limitSwitchRight.interval(50);
  47.  
  48.   triggerButton.attach(triggerPin, INPUT_PULLUP);
  49.   triggerButton.interval(25);
  50.  
  51.   Serial.begin(9600);
  52.   //assuming motor is already one one junction
  53.   Serial.println("Incubator starting. motor is now at rest");
  54.  
  55.   delay(2000);
  56. }
  57.  
  58. enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
  59.  
  60. int currentState = STATE_IDLE;
  61.  
  62. void loop() {
  63.   // put your main code here, to run repeatedly:
  64.  
  65.   limitSwitchLeft.update();
  66.   limitSwitchRight.update();
  67.   triggerButton.update();
  68.  
  69.   bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  70.   bool limitSwitchRightState = limitSwitchRight.read();
  71.   bool startMotor = false;
  72.  
  73.   switch (currentState) {
  74.     case STATE_IDLE:
  75.       // motor is off, waiting for trigger button to be pressed
  76.       if (triggerButton.fell())
  77.       {
  78.         Serial.println("button to trigger start motor has been presssed");
  79.         // figure out which way to move
  80.         if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
  81.         {
  82.           Serial.println("Moving right now");
  83.           digitalWrite(motorRightPin, HIGH);
  84.           digitalWrite(motorLeftPin, LOW);
  85.           currentState = STATE_MOVING_RIGHT;
  86.         }
  87.         else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
  88.         {
  89.           Serial.println("Moving left now");
  90.           digitalWrite(motorRightPin, LOW);
  91.           digitalWrite(motorLeftPin, HIGH);
  92.           currentState = STATE_MOVING_LEFT;
  93.         }
  94.         else if (limitSwitchLeftState == HIGH && limitSwitchRightState == HIGH)
  95.         {
  96.           // neither limit switch is tripped, arbitrarily move left
  97.           Serial.println("No limit switches detected, moving left now");
  98.           digitalWrite(motorRightPin, LOW);
  99.           digitalWrite(motorLeftPin, HIGH);
  100.           currentState = STATE_MOVING_LEFT;
  101.          }
  102.        
  103.         else
  104.         {
  105.           Serial.println("Both limit switches detected, program halted");
  106.           digitalWrite(motorRightPin, LOW);
  107.           digitalWrite(motorLeftPin, LOW);
  108.           while (1); // loop forever
  109.         }
  110.       }
  111.       break;
  112.  
  113.     case STATE_MOVING_RIGHT:
  114.       // moving right so only check right limit switch
  115.       if (limitSwitchRightState == HIGH && limitSwitchLeftState == LOW)  
  116.       {
  117.         Serial.println("Motor reached right. motor stopping");
  118.         digitalWrite(motorRightPin, LOW);
  119.         digitalWrite(motorLeftPin, LOW);
  120.         currentState = STATE_IDLE;
  121.       }
  122.       break;
  123.  
  124.     case STATE_MOVING_LEFT:
  125.       // moving left so only check left limit switch
  126.  
  127.       if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
  128.       {
  129.         Serial.println("Motor has reached left. Motor stopping");
  130.         digitalWrite(motorRightPin, LOW);
  131.         digitalWrite(motorLeftPin, LOW);
  132.         currentState = STATE_IDLE;
  133.       }
  134.       break;
  135.   }
  136. }
Add Comment
Please, Sign In to add comment