Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
- //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
- //This code belongs to the following tutorial video: https://youtu.be/YGnxGJmxHlo
- //AccelStepper
- #include <AccelStepper.h>
- AccelStepper stepper(1, 9, 8);// pulses Digital 9 (CLK); Direction Digital 8 (CCW)
- //16x2 LCD
- #include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- //Defining pins
- const int RotaryCLK = 2; //CLK pin on the rotary encoder
- const int RotaryDT = 4; //DT pin on the rotary encoder
- const int RotarySW = 3; //SW pin on the rotary encoder (Button function)
- //Defining variables
- int ButtonCounter = 0;
- int RotateCounter = 0;
- //Statuses
- int CLKNow;
- int CLKPrevious;
- int DTNow;
- int DTPrevious;
- // Time
- float TimeNow1;
- float TimeNow2;
- void setup()
- {
- Serial.begin(9600); //can be skipped, if you won't use the serial terminal. I kept it for debugging
- //------------------------------------------------------
- lcd.init(); // initialize the lcd
- lcd.init();
- lcd.backlight();
- //------------------------------------------------------
- lcd.setCursor(0,0); //Defining positon to write from first row,first column .
- lcd.print("Rotary encoder");
- lcd.setCursor(0,1);
- lcd.print("Stepper stepping"); //You can write 16 Characters per line .
- delay(5000); //wait 1 sec
- //------------------------------------------------------
- //internal pullups
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- //Store states
- CLKPrevious = digitalRead(RotaryCLK);
- DTPrevious = digitalRead(RotaryDT);
- attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE); //attachinterrupt for the rotation
- attachInterrupt(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING); //attachinterrupt for the click (button)
- stepper.setMaxSpeed(1000); //SPEED = Steps / second
- stepper.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
- TimeNow1 = millis(); //Start time
- }
- void loop()
- {
- printLCD();
- RunTheMotor();
- }
- void buttonPressed()
- {
- TimeNow2 = millis();
- if(TimeNow2 - TimeNow1 > 1000) // if two clicks are closer to each other than 1s, they are neglected. sort of a debouncing.
- {
- ButtonCounter++; //increase the counter
- }
- TimeNow1 = millis();
- //You can add something here, like resetting the RotateCounter (e.g. redefine 0 position)
- }
- void rotate()
- {
- CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin
- // If last and current state of CLK are different, then a pulse occurred
- if (CLKNow != CLKPrevious && CLKNow == 1){
- // If the DT state is different than the CLK state then
- // the encoder is rotating CCW so increase
- if (digitalRead(RotaryDT) != CLKNow) {
- RotateCounter++;
- } else {
- // Encoder is rotating CW so decrease
- RotateCounter--;
- }
- }
- CLKPrevious = CLKNow; // Store last CLK state
- }
- void printLCD()
- {
- //lcd.clear();
- lcd.setCursor(0,0); // Defining positon to write from first row, first column .
- lcd.print("Clicks: ");
- lcd.setCursor(8,0);
- lcd.print(" ");
- lcd.setCursor(8,0);
- lcd.print(ButtonCounter); //Print the number of button clicks
- lcd.setCursor(0,1); // Defining positon to write from second row, first column .
- lcd.print("Position: ");
- lcd.setCursor(10,1);
- lcd.print(" ");
- lcd.setCursor(10,1);
- lcd.print(RotateCounter); //Print the number of pulses
- }
- void RunTheMotor() //function for the motor
- {
- stepper.enableOutputs(); //enable pins
- stepper.moveTo(-1*RotateCounter); //-1 is to match the rotation of the encoder with the rotation of the stepper
- while(stepper.distanceToGo() != 0)
- {
- stepper.runToNewPosition(-1*RotateCounter);
- }
- }
Add Comment
Please, Sign In to add comment