Advertisement
Guest User

arduino

a guest
Oct 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. /*
  2.  * Control DC motor with Smartphone via bluetooth
  3.  * created by Rui Santos, http://randomnerdtutorials.com
  4. */
  5. #include <Bounce2.h>
  6.  // Rotary Encoder Inputs
  7.  #define inputCLK 5
  8.  #define inputDT 6
  9. #define BUTTON_PIN 2
  10. Bounce debouncer = Bounce();
  11.  
  12.  
  13. int motorPin1 = 3; // pin 2 on L293D IC
  14. int motorPin2 = 4; // pin 7 on L293D IC
  15. int enablePin = 10; // pin 1 on L293D IC
  16. int state= 0;
  17. int i=60;
  18. int buttonPushCounter = 0;
  19. int lastButtonPushCounter = 0;
  20. int lastButtonState = 0;
  21. int kierunek = 0;
  22. int dir = 0;
  23. int counter = 0;
  24. int currentStateCLK;
  25. int previousStateCLK;
  26. String encdir ="";
  27.  
  28. void setup() {
  29.    // sets the pins as outputs:
  30.   // Set encoder pins as inputs  
  31.     pinMode (inputCLK,INPUT);
  32.     pinMode (inputDT,INPUT);
  33.     pinMode(motorPin1, OUTPUT);
  34.     pinMode(motorPin2, OUTPUT);
  35.     pinMode(enablePin, OUTPUT);
  36.     pinMode(BUTTON_PIN,INPUT_PULLUP);
  37.  
  38.     Serial.begin (9600);
  39.     // Read the initial state of inputCLK
  40.    // Assign to previousStateCLK variable
  41.    previousStateCLK = digitalRead(inputCLK);
  42.    
  43.  
  44.   // Po konfiguracji przycisku, ustawienie działania funkcji Bounce :
  45.   debouncer.attach(BUTTON_PIN);
  46.   debouncer.interval(5); // interwał w ms
  47.  
  48.  
  49.     // sets enablePin high so that motor can turn on:
  50.     analogWrite(enablePin, i);
  51.     // initialize serial communication at 9600 bits per second:
  52.    
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. void loop() {
  60.    
  61.     analogWrite(enablePin, i);
  62.     int value = debouncer.read();
  63.        debouncer.update();
  64.     if (value != lastButtonState) {
  65.            // if the state has changed, increment the counter
  66.            if (value == HIGH) {
  67.            // if the current state is HIGH then the button
  68.            // wend from off to on:
  69.            buttonPushCounter++;
  70.  
  71.    }
  72.     }
  73.  
  74.   if (buttonPushCounter != lastButtonPushCounter) {
  75.  kierunek = dir;
  76.        
  77.     if (kierunek == 1) {
  78.         digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
  79.         digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
  80.         dir = 0;
  81.     }
  82.     // if the state is '2' the motor will turn left
  83.     if (kierunek == 0) {
  84.         digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
  85.         digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
  86.          dir = 1;
  87.     }
  88.   }
  89.  
  90.     // Read the current state of inputCLK
  91.    currentStateCLK = digitalRead(inputCLK);
  92.    
  93.    // If the previous and the current state of the inputCLK are different then a pulse has occured
  94.    if (currentStateCLK != previousStateCLK){
  95.        
  96.      // If the inputDT state is different than the inputCLK state then
  97.      // the encoder is rotating counterclockwise
  98.      if (digitalRead(inputDT) != currentStateCLK) {
  99.        counter --;
  100.        encdir ="CCW";
  101.  
  102.        
  103.      } else {
  104.        // Encoder is rotating clockwise
  105.        counter ++;
  106.        encdir ="CW";
  107.  
  108.        
  109.      }
  110.  
  111.      Serial.print(" feed: ");
  112.      Serial.println(i);
  113.    }
  114.     i = counter;
  115.    
  116.    
  117.     if (counter > 254) {
  118.      counter=253;
  119.     }
  120.     if (counter < 0) {
  121.     counter=1;
  122.     }
  123.  
  124.     if (counter > 60) {
  125.     }
  126.    
  127.      
  128.    
  129.  
  130.            
  131.  analogWrite(enablePin, i);
  132.  lastButtonState = value;
  133.  lastButtonPushCounter = buttonPushCounter;
  134.  previousStateCLK = currentStateCLK;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement