manhoosbilli1

Untitled

Jun 2nd, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <Bounce2.h>
  2. const byte m1 = 4;
  3. const byte m2 = 3;
  4. const byte s1 = 7;
  5. const byte s2 = 8;
  6. const byte trigger = 11;    //set trigger to a switch just for example purpose. its originally going to be trigger by time
  7. bool s1State;
  8. bool s2State;
  9. bool triggerState;
  10. bool prevS1State = 0;
  11. bool prevS2State = 0;
  12. bool startMotor = 0;
  13.  
  14. bool wasGoingLeft;
  15. bool wasGoingRight;
  16. bool motorStopped;
  17.  
  18. bool high = LOW; //since ONLY switches are inverted. when its low. it will be high
  19. bool low = HIGH;
  20.  
  21. Bounce debouncer1 = Bounce();
  22. Bounce debouncer2 = Bounce();
  23. Bounce debouncer3 = Bounce();  //trigger for motor
  24.  
  25. void setup() {
  26.   // put your setup code here, to run once
  27.  
  28.   pinMode(m1, OUTPUT);
  29.   pinMode(m2, OUTPUT);
  30.  
  31.   pinMode(s1, INPUT_PULLUP);
  32.   debouncer1.attach(s1);
  33.   debouncer1.interval(25);
  34.  
  35.   pinMode(s2, INPUT_PULLUP);
  36.   debouncer2.attach(s2);
  37.   debouncer2.interval(25);
  38.  
  39.   pinMode(trigger, INPUT_PULLUP);
  40.   debouncer3.attach(trigger);
  41.   debouncer3.interval(25);
  42.  
  43.   Serial.begin(9600);
  44.   //assuming motor is already one one switch
  45.   wasGoingRight = true;
  46.   wasGoingLeft =false;
  47.   delay(2000);
  48.  
  49.  
  50. }
  51.  
  52. void loop() {
  53.   // put your main code here, to run repeatedly:
  54.   Serial.println("in loop");
  55.   bool m1State = digitalRead(m1);
  56.   bool m2State = digitalRead(m2);
  57.  
  58.   debouncer1.update();
  59.   debouncer2.update();
  60.   debouncer3.update();
  61.  
  62.   triggerState = debouncer3.read(); //trigger
  63.   s1State = debouncer1.read();   //switch are input pullup. high state is zero
  64.   s2State = debouncer2.read();
  65.  
  66.  
  67.  
  68.   if(debouncer3.fell()) //when trigger button is pressed
  69.    {
  70.     startMotor = true;
  71.     Serial.println("button to trigger start motor has been presssed");
  72.    }
  73.    if(startMotor)
  74.       {
  75.        
  76.          if(wasGoingLeft) {        
  77.           goRight();
  78.           delay(2000);  //wait for both of the switches to be released
  79.           if(s1State == low || s2State == low) {    //if it touches one of the switches
  80.             stopMotor();
  81.             wasGoingRight = true; //to remember next time that it was originally going right
  82.             startMotor = false;  //to get out of function and continue loop
  83.           }
  84.    
  85.          if(wasGoingRight) {
  86.           goLeft();
  87.           delay(2000);  //wait for both of the switches to be released
  88.           if(s1State == low || s2State == low) {    //if it touches one of the switches
  89.             stopMotor();
  90.             wasGoingLeft = true; //to remember next time that it was originally going right
  91.             startMotor = false;
  92.            }    
  93.           }
  94.         }
  95.       }
  96. }
  97.  
  98. //---------------functions
  99.  
  100.    void goLeft() {
  101.     digitalWrite(m1, HIGH);
  102.     digitalWrite(m2, LOW);
  103.     Serial.println("going left");
  104.     wasGoingLeft = true;
  105.     wasGoingRight = false;
  106.    }
  107.  
  108.    void goRight() {
  109.     digitalWrite(m1, LOW);
  110.     digitalWrite(m2, HIGH);
  111.      Serial.println("going right");
  112.      wasGoingLeft = false;
  113.      wasGoingRight = true;
  114.    }
  115.  
  116.    void stopMotor() {
  117.     digitalWrite(m1, LOW);
  118.     digitalWrite(m2, LOW);
  119.      Serial.println("motor stopped");
  120.      motorStopped = true;
  121.    }
Add Comment
Please, Sign In to add comment