I need help with Stepper motor

Hello everyone,

I've done a fair amount of searching and have tried a bunch of different things but I can't seem to understand what's going on.

I want the stepper motor to run non-loop using a selector switch, have the stepper motor run for 20s and turn off while the switch is held down, and with the selector switch to another position, have the stepper motor turn back 20s and go forward 40. second

Do you have an example?
I've been thinking for 5 days please help
Many thanks for any help you guys can give.

int PUL=10; 
int DIR=9; 
int ENA=8; 
int SW = 2;
void setup() {
 pinMode (PUL, OUTPUT);
 pinMode (DIR, OUTPUT);
 pinMode (ENA, OUTPUT);
 pinMode (SW, INPUT_PULLUP);
}

void loop() {
 if (digitalRead(SW) == 0)
  {
  for (int i=0; i<6400; i++) 
  {
    digitalWrite(DIR,HIGH);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL,LOW);
    delayMicroseconds(50);
  }
  delay(200);
    for (int i=0; i<6400; i++) 
  {
    digitalWrite(DIR,LOW);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL,LOW);
    delayMicroseconds(50);
  }
  delay(200);
}

Hello feeldog
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

1 Like

Do not cross-post. Other thread removed.

1 Like

Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

1 Like

what should happen if the switch is released before 20 sec?

1 Like

consider

#undef MyHW
#ifdef MyHW
int PUL = 13;
int DIR = 12;
int ENA = 11;
int SW  = A1;

#define Period      4000
#define StepDelay   100

#else
int PUL = 10;
int DIR =  9;
int ENA =  8;
int SW  =  2;

#define Period      20000
#define StepDelay   2
#endif

unsigned long msecLst;
unsigned long msec;

enum { ST_IDLE, ST_FOR, ST_REV };
int state = ST_IDLE;

int butState;

// -----------------------------------------------------------------------------
void
step (void)
{
    digitalWrite (PUL,HIGH);
    delay (StepDelay);
    digitalWrite (PUL,LOW);
    delay (StepDelay);
}

// -----------------------------------------------------------------------------
void loop () {
    msec = millis ();

    if (ST_IDLE != state)  {
        if ((msec - msecLst) > Period)
            state = ST_IDLE;
        else
            step ();
    }

    int but = digitalRead (SW);
    if (butState != but)  {
        butState = but;
        delay (10);         // debounce

        if (LOW == but && ST_IDLE == state)  {
            msecLst = msec;
            state   = ST_FOR;
            digitalWrite (DIR, HIGH);
        }
        else if (HIGH == but && ST_IDLE == state)  {
            msecLst = msec;
            state   = ST_REV;
            digitalWrite (DIR, LOW);
        }
        else 
            ; // ???????????
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    pinMode (PUL, OUTPUT);
    pinMode (DIR, OUTPUT);
    pinMode (ENA, OUTPUT);

    pinMode (SW, INPUT_PULLUP);
    butState = digitalRead (SW);

    digitalWrite (ENA,HIGH);
}
1 Like

Thanks very much.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.