Hi guys, In my previous post I asked for code.
What I did :1. I found code for Stepper motor controlling with Arduino with LCD and rotary encoder in curious scientist. Link below :
https://curiousscientist.tech/blog/stepper-motor-direct-control-rotary-encoder2. I connect the parts as per the code and put it in Arduino. After I connect the power supply, it won't work.
3. I don't have a stepper motor. So I didn't connect the motor. I just want to know, Is LCD works or not. Motor side is not a problem It will work when I connect it to driver and Arduino. But at the same time, I have an another doubt. I use a rotary encoder and stepper motor. Is Arduino counts and check the feedback voltage of stepper motor Initially. maybe that's the reason for its not working.
4. Finally I check with youtube video. link below :
5. The code in youtube and the code in google is not same. So I change the google code to Youtube code. its not working.
Can anyone help me please.
I attached some reference images.Code in Youtube video ://AccelStepper
#include <AccelStepper.h>
AccelStepper stepper(1, 9,

;// 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);
//------------------------------------------------------
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
//------------------------------------------------------
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(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING);
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)
{
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(RotateCounter);
}
}