#include <AccelStepper.h>
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
 
//const int pin_Backlight = 10;
 
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
#define HALFSTEP 8
 
// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2 11     // IN2 on the ULN2003 driver 1
#define motorPin3  12     // IN3 on the ULN2003 driver 1
#define motorPin4  13     // IN4 on the ULN2003 driver 1
 
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
char ledState;
void setup() {
  stepper1.setMaxSpeed(1000);
  stepper1.setAcceleration(2000);
    //stepper1.setSpeed(2000);
    //stepper1.moveTo(2048);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(" ULN2003   ");
  lcd.setCursor(0, 1);
  lcd.print("  STEP_MOTOR  ");
 
}//--(end setup )---
 
void loop() {
 
    //Change direction when the stepper reaches the target position
 
    int x;
  x = analogRead(0);
    if (x < 60) {
      lcd.setCursor(15, 0);
      lcd.print("+");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() - 4096);
        }
    }
    else if (x < 200) {
      lcd.setCursor(15, 0);
      lcd.print("+");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() - 1000);
        }
    }
    else if (x < 400) {
      lcd.setCursor(15, 0);
      lcd.print("-");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() + 1000);
        }
 
    }
    else if (x < 600) {
      lcd.setCursor(15, 0);
      lcd.print("-");
        if(stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() + 4096);
        }
    }
    else if (x < 800) {
      lcd.print("Select");
    }
    //if (Serial.available() > 0)
    //{
    //  ledState = Serial.read();
 
    //  switch (ledState)
    //  {
    //  case '0':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(stepper1.currentPosition() + 4096);
    //      }
    //      lcd.setCursor(0, 0);
    //      lcd.print("ULN2003 STEPMOTOR");
    //      /*lcd.setCursor(0, 0);
    //      lcd.print("Position: +500");*/
    //     lcd.setCursor(0, 1);
    //      lcd.print("PROG: 0");
    //      ledState = ' ';
    //      break;
    //  }
    //  case '1':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(stepper1.currentPosition() - 4096);
    //      }
    //      lcd.setCursor(0, 0);
    //      lcd.print("Position: -500");
    //      lcd.setCursor(0, 1);
    //      lcd.print("PROG: 1");
    //      ledState = ' ';
    //      break;
 
    //  }
    //    case '2':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(-stepper1.currentPosition());
    //      }
    //      lcd.setCursor(0, 1);
    //      lcd.print("PROG: 2");
    //      ledState = ' ';
    //      break;
    //  }
    //  }
 
    //}
  stepper1.run();
}