Help with script, working but with problems

Hello all, I am struggling with this script using TB6600, Arduino UNO R3 and a Sanyo Stepper Motor.

I need the script to do the following:
Click of a button move motor clockwise, delay a few seconds then move motor counterclockwise. And finally have an emergency stop button just in case.

The script is working but some times the start button doesnt work and the emergency stop doesnt do its thing.

Hope somebody can help here. Thanks

#include <AccelStepper.h>

const byte stepPin    = 8;
const byte dirPin     = 9;
const byte enablePin  = 10;

int EMERGENCY_STOP_PIN = 3; 
const byte buttonPin   = 4;

long moveSteps = 5000;

bool trigger_forward = false ;
bool trigger_backward = false;

unsigned long time = 0 ;

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

bool lastButtonState  = LOW;
bool isPressed        = false;

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

   pinMode(enablePin, OUTPUT);
   pinMode(buttonPin, INPUT_PULLUP);
   digitalWrite(enablePin, LOW);
 
   stepper.setMaxSpeed(1000); 
   stepper.setAcceleration(1000);

   pinMode (EMERGENCY_STOP_PIN, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(EMERGENCY_STOP_PIN), emergencyStop, CHANGE);

   trigger_forward = true ;  

}

void emergencyStop() {
stepper.stop();
Serial.println(F("The stepper motor is STOPPED"));
}

void loop()
{
 
static unsigned long timer = 0;
unsigned long interval = 50;  // check switch 20 times per second

if (millis() - timer >= interval){

timer = millis();
//Serial.println("Timer:");
//Serial.println(timer);
      
bool buttonState = digitalRead(buttonPin);

Serial.println("buttonState:");
Serial.println(buttonState);
Serial.println(lastButtonState);

   if (buttonState != lastButtonState)
      {
   
  if (buttonState == HIGH){
  isPressed = true;

  Serial.println(F("Button Start Pressed"));
  }

      }

    // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;

   }
  
  if(isPressed == true){

  
if ( millis () - time >= 1000 && trigger_forward) // if triggered set the forward motion going
{

trigger_forward = false ;
trigger_backward = true ;
isPressed = false;

time = millis() ;
Serial.println("Time1:");
Serial.println(time);

stepper.moveTo(1000);
Serial.println("The stepper moving FORWARD....");


}

}

if(isPressed == false){

//Serial.println 2(millis ());
if (millis ()- time >= 10000 && trigger_backward) // if return is due, do it
{

trigger_backward = false ;
trigger_forward = true;

time = millis();
Serial.println("Time2:");
Serial.println(time);

stepper.moveTo(-1000) ;
Serial.println("The stepper moving BACKWARDS....");

}

}

stepper.run();  // must be called very often
                   // ideally once every loop iteration
}

Check up this function! It's likely a blocking function, running until the job is done. Therefore the stop command is not accepted until the previous command is finished.
I only use a little Accelstepper but not like You do. There are helpers knowing more and other libraries to use. Mobatool?

Here is an example using the MobaTools stepper library and a state machine. I believe that it does what you want.

The MobaTools library is available for installation via the IDE library manager. The library documentation is here >> https://github.com/MicroBahner/MobaTools/blob/master/MobaTools-243-en.pdf

Change the pins to suit your setup. Note that micostepping (x4) is selected, change to suit. This code has been successfully tested on real hardware.

// turn stepper one turn on start button activation (active LOW),
// pause and return to 0.  Emergency stop stops motor and will not
// continue till reset.
// by groundFungus aka c.goulding

#include <MobaTools.h>

const byte ledPin = A0;
const byte startPin = 9;
const byte emergencyPin = 10;
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;

const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;

MoToStepper stepper( STEPS_REVOLUTION, STEPDIR );

byte mode = 0;
bool isStopped = false;

unsigned long timer = 0;  // pause timer
unsigned long interval = 5000;  // pause at position, ms

void setup()
{
   Serial.begin(115200);
   pinMode(ledPin, OUTPUT);
   pinMode(startPin, INPUT_PULLUP);
   pinMode(emergencyPin, INPUT_PULLUP);
   stepper.attach( stepPin, dirPin );
   stepper.attachEnable(enablePin, 0, 1);
   stepper.setSpeed(1000);  // rpm /10
   stepper.setRampLen(10);
   stepper.setZero();
}

void loop()
{
   if (digitalRead(emergencyPin) == LOW)  // EMERGENCY stop
   {
      stepper.stop();
      Serial.println("EMERGENCY STOP");
      digitalWrite(ledPin, HIGH);
      isStopped = true;
      while (1); // endless loop 4E4
   }

   if (isStopped == false)
   {

      static unsigned long loopTimer = 0;
      unsigned long loopInterval = 20;
      if (millis() - loopTimer >= loopInterval)
      {
         loopTimer = millis();

         // look for start switch
         if (mode == 0) // look for start switch
         {
            static bool lastStartPinState = HIGH;
            //Serial.println("mode = 0");
            bool startPinState = digitalRead(startPin);
            if (startPinState != lastStartPinState)
            {
               if (startPinState == LOW)
               {
                  mode = 1;
               }
               //lastStartPinState = startPinState;
            }
            lastStartPinState = startPinState;
         }

         // start move to one rev
         else if ( mode == 1)
         {
            //Serial.println("mode = 1");
            stepper.moveTo(STEPS_REVOLUTION); // one rev
            mode = 2;
         }

         // end move to one rev, call pause
         else if ( mode == 2)
         {
            //Serial.println("mode = 2");
            if (stepper.distanceToGo() == 0)
            {
               timer = millis();
               mode = 3;
            }
         }

         // pause
         else if ( mode == 3)
         {
            //Serial.println("mode = 3");
            if (millis() - timer > interval)
            {
               mode = 4;
            }
         }

         // pause done, move back to zero
         else if ( mode == 4)
         {
            //Serial.println("mode = 4");
            stepper.moveTo(0);
            mode = 0;
         }
      }
   }
}

There is no facility to home the motor so it will start where ever it is at reset.

Thanks, will try stepper.move(). Looks like the only other available..

Great, thanks. I Will try it.

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