#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
const int maxSpeed = 1000;
const int motor1_in1 = 10;
const int motor1_in2 = 9;
const int motor1_in3 = 8;
const int motor1_in4 = 7;
const int motor2_in1 = 6;
const int motor2_in2 = 5;
const int motor2_in3 = 4;
const int motor2_in4 = 3;
const int btn1 = A0;
const int btn2 = A1;
const int btn3 = A2;
const int btn4 = A3;
LiquidCrystal_I2C lcd(0x27, 16, 2);
//AccelStepper stepper1(AccelStepper::FULL4WIRE, motor1_in1, motor1_in2, motor1_in3, motor1_in4);
//AccelStepper stepper2(AccelStepper::FULL4WIRE, motor2_in1, motor2_in2, motor2_in3, motor2_in4);
AccelStepper stepper1(AccelStepper::FULL2WIRE, motor1_in1, motor1_in2);
AccelStepper stepper2(AccelStepper::FULL2WIRE, motor2_in1, motor2_in2);
unsigned long timeLcd, timeBtn;
int speed1, speed2;
void setup() {
stepper1.setMaxSpeed(maxSpeed);
stepper2.setMaxSpeed(maxSpeed);
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(btn3, INPUT_PULLUP);
pinMode(btn4, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.clear();
}
void handleButtons(){
if(millis() - timeBtn > 100){
timeBtn = millis();
speed1 = digitalRead(btn1) == HIGH ? speed1 - 1 : speed1;
speed1 = digitalRead(btn2) == HIGH ? speed1 + 1 : speed1;
speed2 = digitalRead(btn3) == HIGH ? speed2 - 1 : speed2;
speed2 = digitalRead(btn4) == HIGH ? speed2 + 1 : speed2;
speed1 = constrain(speed1, 0, 100);
speed2 = constrain(speed2, 0, 100);
}
}
void handleLcd(){
if(millis() - timeLcd > 100){
timeLcd = millis();
char buff[20];
sprintf(buff, "Motor1: %3d %%", speed1);
lcd.setCursor(0, 0);
lcd.print(buff);
sprintf(buff, "Motor2: %3d %%", speed2);
lcd.setCursor(0, 1);
lcd.print(buff);
}
}
void loop() {
stepper1.setSpeed(map(speed1, 0, 100, 0, maxSpeed));
stepper2.setSpeed(map(speed2, 0, 100, 0, maxSpeed));
stepper1.runSpeed();
stepper2.runSpeed();
handleButtons();
handleLcd();
}