manhoosbilli1

Untitled

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