// https://wokwi.com/projects/360355231571992577
// 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

//... these don't need to be two inputs, and don't need to be ezButtons
//ezButton cw (4); //clockwise rotation
//ezButton ccw(5); //counter clockwise rotation

# define ccwPin 4   // input for cw/ccw

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

//...
  pinMode(ccwPin, INPUT_PULLUP);

//...
  inc.setDebounceTime(25);
  dec.setDebounceTime(25);

//...  cw.setDebounceTime(25);
//... ccw.setDebounceTime(25);

  conti.setDebounceTime(25);
  manual.setDebounceTime(25);
  presstorotate.setDebounceTime(25);

}

//...
bool doInc, doDec, stopStart;
int theSpeed;       // the set speed from the inc/dec

//...
bool running;

void loop()
{
  inc.loop();
  dec.loop();
//...  cw.loop();
//...  ccw.loop();
  conti.loop();
  manual.loop();
  presstorotate.loop();


// get the inputs
  doInc = inc.isPressed();
  doDec = dec.isPressed();

  bool ccw = digitalRead(ccwPin);

  if (manual.isPressed()) running = true;
  if (manual.isReleased()) running = false; 



// using the switch flags, set the display, speed and direction
  updatecnt();

  if (running)
    myStepper.setSpeed(ccw ? theSpeed : -theSpeed);
  else myStepper.setSpeed(0);




// tell the motor
   myStepper.runSpeed();
}

void updatecnt ()
{
  static int lastDisplayCount = -1;  // need to update?

  if (doInc) cnt++;
  if (doDec) cnt--;

  if (cnt >= 10) cnt = 9;
  if (cnt < 0) cnt = 0;

  if (lastDisplayCount != cnt) {
    lastDisplayCount = cnt;

    theSpeed = 20 * cnt;

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

    lcd.clear();
    lcd.print("RPM = ");
    lcd.print(cnt);
  }
}

/*

  if (inc.isPressed() && cnt < 10) {
    cnt++;
    update = true;

  }
  else if (dec.isPressed() && cnt > 0) {
    cnt--;


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

    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);
//}
    
*/
A4988
CW/CCW
STOP/RUN