// 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 (4);
ezButton ccw(5);

//const int cw = 4;
//const int ccw = 5;

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);
  digitalRead(4);
  digitalRead(5);
}

//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;

    //long speed = 200 * cnt;

    //Serial.print("New speed = ");
    //Serial.println(speed);

    //myStepper.setSpeed(speed);
    //lcd.clear();
    //lcd.print("RPM = ");
    //lcd.print(cnt);

    delay(500);
  }
  else if (dec.isPressed() && cnt > 0) {
    cnt--;
    update = true;

    //long speed = 200 * cnt;

    //Serial.print("New speed = ");
    //Serial.println(speed);

    //myStepper.setSpeed(speed);
    //lcd.clear();
    //lcd.print("RPM = ");
    //lcd.print(cnt);

    delay(500);
  }

  //if (( update == true )  &&  (cw.isPressed() )){
  
  //if (cw.isPressed() || (update ==true)){

  if ((digitalRead(4)== HIGH  && (update ==true)) ) {  
    
    //if (update ==true)
  //{  

    long speed = 200 * cnt;

    Serial.print("New speed = ");
    Serial.println(speed);

    //myStepper.setSpeed(-speed);
   lcd.clear();
   lcd.print("RPM = ");
    lcd.print(cnt);
 // }
   //long speed = 200 * cnt;
   myStepper.setSpeed(speed);

    lcd.print("dir = CW ");
   update = false;
}
   
//if (ccw.isPressed() || (update ==true)){

else if ((digitalRead (5) == HIGH && (update ==true))){  
   
    //if (update ==true)
 // {  

    long speed = 200 * cnt;

   Serial.print("New speed = ");
   Serial.println(speed);

    //myStepper.setSpeed(speed);
    lcd.clear();
    lcd.print("RPM = ");
    lcd.print(cnt);
  //}
   
   //long speed = 200 * cnt;
   myStepper.setSpeed(-speed);
   lcd.print("dir = ccw");
   update = false;
}
else if (digitalRead (5) == LOW && (digitalRead(4) ==LOW))
{
  long speed = 0;
  myStepper.setSpeed(speed);
}
    
  }
A4988