// Pin Definitions
#define DIR_PIN             8
#define STEP_PIN            9
#define ENABLE_PIN          10
#define POT_PIN             A0
#define BUZZER_PIN          4

#define START_PIN    2    // green on right
#define STOP_PIN     3    // red   on right
#define LIMIT_LEFT   A1   // green on bottom
#define LIMIT_RIGHT  A3   // blue  on bottome
#define REED_SWITCH  A2   // red   on bottom

#define RED_LED             11
#define GREEN_LED           12

// Variables
int motorSpeed = 500; // Connected to potentiometer
bool run;
bool dirCw;

enum { LED_ON = HIGH, LED_OFF = LOW };

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin(115200);
    // Pin modlar─▒
    pinMode (DIR_PIN,            OUTPUT);
    pinMode (STEP_PIN,           OUTPUT);
    pinMode (ENABLE_PIN,         OUTPUT);
    pinMode (BUZZER_PIN,         OUTPUT);

    pinMode (START_PIN,   INPUT_PULLUP);
    pinMode (STOP_PIN,    INPUT_PULLUP);
    pinMode (LIMIT_LEFT,  INPUT_PULLUP);
    pinMode (LIMIT_RIGHT, INPUT_PULLUP);
    pinMode (REED_SWITCH,        INPUT_PULLUP);

    pinMode (RED_LED,            OUTPUT);
    pinMode (GREEN_LED,          OUTPUT);

    digitalWrite (ENABLE_PIN,    LOW); // Engine eniabled

    Serial.println("ready");
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (run)  {
        step ();
        if (   LOW == digitalRead (STOP_PIN)
            || LOW == digitalRead (LIMIT_LEFT)
            || LOW == digitalRead (LIMIT_RIGHT) )  {
              Serial.println(" stop");
              run = false;
              digitalWrite (RED_LED, LED_OFF);
              digitalWrite (GREEN_LED, LED_OFF);
        }
    }
    // Start button control
    if (digitalRead(START_PIN) == LOW) {
        if (! run)  {
          Serial.println ("start");
          run = true;
          if (dirCw)
             digitalWrite (RED_LED, LED_ON);             
          else
             digitalWrite (GREEN_LED, LED_ON);             

          digitalWrite (DIR_PIN, dirCw);
          dirCw = ! dirCw;
        }
    }
}

// -----------------------------------------------------------------------------
// Motor motion function
void step ()
{
    digitalWrite      (STEP_PIN, HIGH);
    delayMicroseconds (motorSpeed);
    digitalWrite      (STEP_PIN, LOW);
    delayMicroseconds (motorSpeed);
}
A4988