#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define ENCODER_CLK 2
#define ENCODER_DT 3
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const int btn1 = 5;
const int btn2 = 4;
const int led1 = 6;
const int sw = 7;
int cnt = 0;
int enc = 0;
// Initialize the stepper library on pins 8 through 11:
const int motor_pin_1 = 8;
const int motor_pin_2 = 9;
const int motor_pin_3 = 10;
const int motor_pin_4 = 11;
int hf20 = 400;
Stepper myStepper(hf20, motor_pin_1, motor_pin_2, motor_pin_3, motor_pin_4);
void setup() {
myStepper.setSpeed(40);
lcd.init();
lcd.backlight();
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
pinMode(sw, INPUT_PULLUP);
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(led1, OUTPUT);
lcd.setCursor(0,0);
lcd.print("Stepper OwO");
lcd.setCursor(0, 1);
lcd.print("ArkanAsadilHuda");
delay(1000);
lcd.clear();
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
enc += 10;
}
if (dtValue == LOW) {
enc -= 10;
}
}
void loop() {
hf20 = enc;
if (digitalRead(sw) == LOW) {
if (enc + cnt >= 0) {
digitalWrite(led1, LOW);
myStepper.step(hf20);
cnt += hf20;
} else {
lcd.setCursor(0, 1);
lcd.print("Negative number!");
delay(1000);
lcd.clear();
}
} else {
digitalWrite(led1, HIGH);
}
lcd.setCursor(0, 0);
lcd.print("Step:");
lcd.println(cnt);
lcd.setCursor(0, 1);
lcd.print("Sel:");
lcd.println(hf20);
lcd.setCursor(9 ,1);
lcd.print("T:");
lcd.println(enc + cnt);
}