Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Control DC motor with Smartphone via bluetooth
- * created by Rui Santos, http://randomnerdtutorials.com
- */
- #include <Bounce2.h>
- // Rotary Encoder Inputs
- #define inputCLK 5
- #define inputDT 6
- #define BUTTON_PIN 2
- Bounce debouncer = Bounce();
- int motorPin1 = 3; // pin 2 on L293D IC
- int motorPin2 = 4; // pin 7 on L293D IC
- int enablePin = 10; // pin 1 on L293D IC
- int state= 0;
- int i=60;
- int buttonPushCounter = 0;
- int lastButtonPushCounter = 0;
- int lastButtonState = 0;
- int kierunek = 0;
- int dir = 0;
- int counter = 0;
- int currentStateCLK;
- int previousStateCLK;
- String encdir ="";
- void setup() {
- // sets the pins as outputs:
- // Set encoder pins as inputs
- pinMode (inputCLK,INPUT);
- pinMode (inputDT,INPUT);
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(enablePin, OUTPUT);
- pinMode(BUTTON_PIN,INPUT_PULLUP);
- Serial.begin (9600);
- // Read the initial state of inputCLK
- // Assign to previousStateCLK variable
- previousStateCLK = digitalRead(inputCLK);
- // Po konfiguracji przycisku, ustawienie działania funkcji Bounce :
- debouncer.attach(BUTTON_PIN);
- debouncer.interval(5); // interwał w ms
- // sets enablePin high so that motor can turn on:
- analogWrite(enablePin, i);
- // initialize serial communication at 9600 bits per second:
- }
- void loop() {
- analogWrite(enablePin, i);
- int value = debouncer.read();
- debouncer.update();
- if (value != lastButtonState) {
- // if the state has changed, increment the counter
- if (value == HIGH) {
- // if the current state is HIGH then the button
- // wend from off to on:
- buttonPushCounter++;
- }
- }
- if (buttonPushCounter != lastButtonPushCounter) {
- kierunek = dir;
- if (kierunek == 1) {
- digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
- digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
- dir = 0;
- }
- // if the state is '2' the motor will turn left
- if (kierunek == 0) {
- digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
- digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
- dir = 1;
- }
- }
- // Read the current state of inputCLK
- currentStateCLK = digitalRead(inputCLK);
- // If the previous and the current state of the inputCLK are different then a pulse has occured
- if (currentStateCLK != previousStateCLK){
- // If the inputDT state is different than the inputCLK state then
- // the encoder is rotating counterclockwise
- if (digitalRead(inputDT) != currentStateCLK) {
- counter --;
- encdir ="CCW";
- } else {
- // Encoder is rotating clockwise
- counter ++;
- encdir ="CW";
- }
- Serial.print(" feed: ");
- Serial.println(i);
- }
- i = counter;
- if (counter > 254) {
- counter=253;
- }
- if (counter < 0) {
- counter=1;
- }
- if (counter > 60) {
- }
- analogWrite(enablePin, i);
- lastButtonState = value;
- lastButtonPushCounter = buttonPushCounter;
- previousStateCLK = currentStateCLK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement