manhoosbilli1

Untitled

Jun 4th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Bounce2.h>
  2. int function = 0;
  3. bool triggerStart = false;   //trigger to start motor
  4. bool triggerStop = false;   // trigger to stop motor
  5. bool goRight = false;
  6. bool goLeft = false;
  7. bool isTime = false;
  8.  
  9. const byte m1 = 4;
  10. const byte m2 = 3;
  11. const byte s1 = 7;
  12. const byte s2 = 8;
  13. const byte trigger = 11;
  14. bool s1State;
  15. bool s2State;
  16. bool triggerState;
  17. bool prevS1State = 0;
  18. bool prevS2State = 0;
  19. bool startMotor = 0;
  20.  
  21. bool wasGoingLeft;
  22. bool wasGoingRight;
  23. bool motorStopped;
  24.  
  25. bool high = LOW; //since switches are inverted. when its low. it will be high
  26. bool low = HIGH;
  27.  
  28. Bounce debouncer1 = Bounce();
  29. Bounce debouncer2 = Bounce();
  30. Bounce debouncer3 = Bounce();  //trigger for motor
  31.  
  32. void setup() {
  33.   // put your setup code here, to run once
  34.  
  35.   pinMode(m1, OUTPUT);
  36.   pinMode(m2, OUTPUT);
  37.  
  38.   pinMode(s1, INPUT_PULLUP);
  39.   debouncer1.attach(s1);
  40.   debouncer1.interval(25);
  41.  
  42.   pinMode(s2, INPUT_PULLUP);
  43.   debouncer2.attach(s2);
  44.   debouncer2.interval(25);
  45.  
  46.   pinMode(trigger, INPUT_PULLUP);
  47.   debouncer3.attach(trigger);
  48.   debouncer3.interval(25);
  49.  
  50.   Serial.begin(9600);
  51.   //assuming motor is already one one junction
  52.    Serial.println("Incubator starting. motor is now at rest");
  53.    
  54.   delay(2000);
  55.  
  56.  
  57. }
  58.  
  59. void loop() {
  60.   // put your main code here, to run repeatedly:
  61.  
  62.   bool m1State = digitalRead(m1);
  63.   bool m2State = digitalRead(m2);
  64.  
  65.   debouncer1.update();
  66.   debouncer2.update();
  67.   debouncer3.update();
  68.  
  69.   triggerState = debouncer3.read(); //trigger
  70.   s1State = debouncer1.read();   //switch are input pullup. high state is zero
  71.   s2State = debouncer2.read();
  72.   //startMotor = 0;   //turn off this trigger in order to not loop back.
  73.  
  74.  
  75.  if(debouncer3.fell()) //when trigger button is pressed
  76.    {
  77.     startMotor = true;
  78.     Serial.println("button to trigger start motor has been presssed");
  79.    }
  80.  
  81.  if(startMotor == 1)
  82.    {
  83.     switch(function)
  84.     {
  85.     case 0:   //checks if time
  86.       isTime = 1;
  87.       function = 1;
  88.       Serial.println("At case 0. checking if its time");
  89.      
  90.  
  91.     break;
  92.  
  93.     case 1:   //its time so its going to determine where it is
  94.     if(isTime == 1)
  95.     {
  96.       Serial.println("trigger is pressed proceeding to case 1");
  97.       if(s1State == 1 && s2State == 0)   //if one of the switch is high. label is left
  98.       {
  99.        
  100.         goRight =1;   //its left right now. so need to go right
  101.         goLeft = 0;
  102.         Serial.println("At case 1. determind motor is towards left and going right now");
  103.         function = 2;
  104.       }
  105.       if(s1State == 0 && s2State == 1)  //if one of the switch is high. label is right
  106.       {
  107.         goLeft = 1;    //is at right. go left
  108.         goRight = 0;
  109.         Serial.println("At case 1. determind motor is towards right and going left now");
  110.         function = 2;
  111.       }
  112.     }
  113.     break;
  114.  
  115.     case 2:    //once direction to go is determined. go that way
  116.     if(goRight== 1)   //at s1  or left
  117.     {
  118.       Serial.println("At case 2. Going Right");
  119.       digitalWrite(m1, HIGH);
  120.       digitalWrite(m2, LOW);
  121.       function = 3;
  122.      
  123.     }
  124.     if(goLeft == 1)    //at s2 or right
  125.     {
  126.       Serial.println("At case 2. Going left");
  127.       digitalWrite(m1, LOW);
  128.       digitalWrite(m2, HIGH);
  129.       function = 3;
  130.     }
  131.     break;
  132.  
  133.  
  134.  
  135.  
  136.     case 3:    //going to check if the motor has reached the desired switch yet
  137.     if(goRight == 1)   //if motor is going towards right so we are going to monitor the right switch only
  138.     {
  139.       if(s2State == 1)     //go right until s2 has become high
  140.       {
  141.         digitalWrite(m1, LOW);
  142.         digitalWrite(m2, LOW);
  143.         Serial.println("case 3. Motor reached right. motor stopping");
  144.         //stop motor
  145.         function = 4;
  146.       }
  147.     }
  148.  
  149.     if(goLeft == 1)  //if motor is going left
  150.     {
  151.       if(s1State == 0) //check to see if left switch is touched
  152.       {
  153.         digitalWrite(m1, LOW);
  154.         digitalWrite(m2, LOW);
  155.         Serial.println("At case 3.motor has reached left. Motor stopping");
  156.        
  157.         function = 4;
  158.        
  159.       }
  160.     }
  161.     break;
  162.  
  163.     case 4:
  164.     Serial.print("At case 4.motor should have stopped. pressing the trigger will repeat the cycle");
  165.     Serial.println("exiting the switch case and waiting for the trigger");
  166.     isTime = 0;  
  167.     function = 0;
  168.     break;
  169.   }
  170. }
  171. }
Add Comment
Please, Sign In to add comment