// https://wokwi.com/projects/360180413127401473
// https://forum.arduino.cc/t/control-direction-of-stepper-motor-using-toggle-switch/1106579
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>
#include <AccelStepper.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
ezButton inc (9); //increase speed
ezButton dec (10); //decrease speed
ezButton cw (4); //clockwise rotation
ezButton ccw(5); //counter clockwise rotation
ezButton conti (6); // stepper rotates continuously
ezButton manual (7); // stepper rotates only when presstorotate button is pressed
ezButton presstorotate (8); //stepper rotates when button is pressed
int cnt = 0;
int incPrev, decPrev, speedPrev, val1, val2;
// Define stepper motor connections and steps per revolution:
const int dirPin = 2;
const int stepPin = 3;
AccelStepper myStepper(1, stepPin, dirPin);
//#define stepsPerRevolution 10
long speed;
void setup()
{
Serial.begin(115200);
Serial.println("\nHello Step World!\n");
//lcd
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("alto777");
lcd.setCursor(0, 1);
lcd.print("rum runner too!");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
myStepper.setMaxSpeed(10000);
digitalRead(4);
digitalRead(5);
digitalRead (6);
digitalRead(7);
digitalRead(8);
}
void loop()
{
inc.loop();
dec.loop();
cw.loop();
ccw.loop();
conti.loop();
manual.loop();
presstorotate.loop();
updatecnt();
myStepper.runSpeed();
}
void updatecnt ()
{
static bool update; // need to update?
if (inc.isPressed() && cnt < 10) {
cnt++;
update = true;
delay(500);
}
else if (dec.isPressed() && cnt > 0) {
cnt--;
update = true;
delay(500);
}
if ((digitalRead(4)== HIGH && digitalRead(6)== HIGH && (update ==true)) ) {
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
myStepper.setSpeed(speed);
lcd.print(" dir = CW ");
update = false;
}
else if ((digitalRead (5) == HIGH && digitalRead(6)== HIGH && (update ==true))){
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
myStepper.setSpeed(-speed);
lcd.print(" dir = ccw");
update = false;
}
else if ((digitalRead(4)== HIGH && digitalRead(7)== HIGH && (update ==true)) ) {
if (digitalRead(8)==HIGH) //stepper rotates only when button is pressed
{
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
myStepper.setSpeed(speed);
lcd.print(" dir = CW ");
update = false;
}
}
else if ((digitalRead (5) == HIGH && digitalRead(7)== HIGH && (update ==true))){
if (digitalRead(8) == HIGH) //stepper rotates only when button is pressed
{
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
myStepper.setSpeed(-speed);
lcd.print(" dir = ccw");
update = false;
}
}
//else if (digitalRead (5) == LOW && (digitalRead(4) ==LOW))
//{
//long speed = 0;
// myStepper.setSpeed(speed);
//}
}