// 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);
ezButton dec (10);
ezButton cw (6);
ezButton ccw(7);
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);
delay(223); // 1000 - 777
digitalWrite(dirPin, HIGH);
// ezButton for real buttons
inc.setDebounceTime(15);
dec.setDebounceTime(15);
cw.setDebounceTime(15);
ccw.setDebounceTime(15);
}
unsigned long lCoiunter;
void loop()
{
inc.loop();
dec.loop();
cw.loop();
ccw.loop();
/*
Serial.println(lCoiunter); lCoiunter++;
delay(50);
if (inc.isPressed()) Serial.println("inc pressed!");
*/
updatecnt();
myStepper.runSpeed();
// delay(50); // OK, why not.
}
void updatecnt ()
{
static bool update; // need to update?
if (inc.isPressed() && cnt < 10) {
cnt++;
update = true;
}
else if (dec.isPressed() && cnt > 0) {
cnt--;
update = true;
}
if (( update == true ) && (cw.isPressed() )){
long speed = 20 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
myStepper.setSpeed(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
update = false;
}
else if (( update == true ) && (ccw.isPressed() ))
{
long speed = 20 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
myStepper.setSpeed(-speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
update = false;
}
/*
Serial.print(cnt);
Serial.print(" count, and update is ");
Serial.println(update ? "TURE" : "FASLE");
*/
}