Help: Stepper motor to start from a fixed position & Accelstepper

Hello guys, I am currently working on a project that requires stepper motor and relay control from arduino.

  • Everything looks good, but when I load it into Arduino, I get stuck with the Begin Setup screen.
  • I tried checking the code and hardware, everything was fine.
  • If possible, tell me where I am wrong?
  • I appreciate all the help, thanks.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <AccelStepper.h>
//#include "bitmap.h"

#define Pin_Limit 9
//#define Pin_Check 10
#define Pin_Cutting 10
#define Pin_Laser 11
#define Pin_SW 6
#define Pin_DT 7
#define Pin_CLK 8  
#define inLED 2
#define outLED 3
#define OLED_RESET 12
Adafruit_SH1106 display(OLED_RESET);
AccelStepper stepper(1, 5, 4);

long gotoposition[1]; 
volatile long XInPoint=0;
volatile long XOutPoint=0;
volatile long totaldistance=0;
int flag=0; 
int temp=0;
int i,j;
unsigned long switch0=0;
unsigned long rotary0=0;
float setspeed=200;
float motorspeed;
float timeinsec;
float timeinmins;
volatile boolean TurnDetected;  
volatile boolean rotationdirection;  

void Switch()  
{
 if(millis()-switch0>500)
 {
 flag=flag+1;
 }
 switch0=millis();
}

void Rotary()  
{
delay(75);
if (digitalRead(Pin_CLK))
rotationdirection= digitalRead(Pin_DT);
else
rotationdirection= !digitalRead(Pin_DT);
TurnDetected = true;
delay(75);
}

void setup() 
{
  Serial.begin(9600);
  stepper.setMaxSpeed(4000);
  stepper.setSpeed(200);
  
  pinMode(Pin_Limit, INPUT_PULLUP);
 //pinMode(Pin_Check, INPUT_PULLUP);
  pinMode(Pin_SW,INPUT_PULLUP); 
  //pinMode(Pin_CLK,INPUT_PULLUP); 
  //pinMode(Pin_DT,INPUT_PULLUP); 
  pinMode(Pin_CLK,INPUT); 
  pinMode(Pin_DT,INPUT); 
  pinMode(Pin_Laser,OUTPUT);
  pinMode(Pin_Cutting,OUTPUT); 
  pinMode(inLED, OUTPUT);
  pinMode(outLED, OUTPUT);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); 
  display.clearDisplay();
  
  attachInterrupt (digitalPinToInterrupt(6),Switch,RISING); 
  attachInterrupt (digitalPinToInterrupt(8),Rotary,RISING);
   
  // display Boot logo
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,28);
    display.println("GioLangle");
    display.display(); 
    delay(2000);
    display.clearDisplay(); 
  
  Home(); // Move the slider to the initial position - homing
  
}

void Home()
{
  stepper.setMaxSpeed(4000);
  stepper.setSpeed(2000);
  if(digitalRead(Pin_Limit)==1)
  {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,28);
    display.println("Homing");
    display.display(); 
    
  }
  
  while (digitalRead(Pin_Limit)== 1) 
  {
    stepper.setSpeed(-4000);
    stepper.runSpeed();
    
 }
  delay(20);
  stepper.setCurrentPosition(0);
    stepper.moveTo(200);
    while(stepper.distanceToGo() != 0)
    {
      stepper.setSpeed(4000);
      stepper.runSpeed();
    }
    stepper.setCurrentPosition(0);
    display.clearDisplay();
}

void SetSpeed()
{
  display.clearDisplay();
  while( flag==4)
  {
  if (TurnDetected)  
  {
        TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
        if (rotationdirection) 
        { 
         setspeed = setspeed + 30;
        }
        if (!rotationdirection) 
        { 
         setspeed = setspeed - 30;        
         if (setspeed < 0) 
        { 
         setspeed = 0;
        }
        }

          display.clearDisplay();
          display.setTextSize(2);
          display.setTextColor(WHITE);
          display.setCursor(30,0);
          display.print("Speed");
          motorspeed=setspeed/80;
          display.setCursor(5,16);
          display.print(motorspeed);
          display.print(" mm/s");   
          totaldistance=XOutPoint-XInPoint;
          if(totaldistance<0)
            {
              totaldistance=totaldistance*(-1);
            }
            else
            {
              
            }
          timeinsec=(totaldistance/setspeed);
          timeinmins=timeinsec/60;
          display.setCursor(35,32);
          display.print("Time");
          display.setCursor(8,48);
          if(timeinmins>1)
          {
          display.print(timeinmins);
          display.print(" min");
          }
          else
          { 
          display.print(timeinsec);
          display.print(" sec");
          }
          display.display();
  }
          display.clearDisplay();
          display.setTextSize(2);
          display.setTextColor(WHITE);
          display.setCursor(30,0);
          display.print("Speed");
          motorspeed=setspeed/80;
          display.setCursor(5,16);
          display.print(motorspeed);
          display.print(" mm/s");   
          totaldistance=XOutPoint-XInPoint;
           if(totaldistance<0)
            {
              totaldistance=totaldistance*(-1);
            }
            else
            {
              
            }
          timeinsec=(totaldistance/setspeed);
          timeinmins=timeinsec/60;
          display.setCursor(35,32);
          display.print("Time");
          display.setCursor(8,48);
          if(timeinmins>1)
          {
          display.print(timeinmins);
          display.print(" min");
          }
          else
          { 
          display.print(timeinsec);
          display.print(" sec");
          }
          display.display();
  }
 
}

void stepperposition(int n)
{
  stepper.setMaxSpeed(4000);
  stepper.setSpeed(200);
  if (TurnDetected)  
  {
        TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
     if(n==1)
     {
        if (!rotationdirection) 
        { 
          if( stepper.currentPosition()-500>0 )
          {
          stepper.move(-500);
          while(stepper.distanceToGo() != 0)
            {
              stepper.setSpeed(-4000);
              stepper.runSpeed();
            }
          }
            else
            {
                while (stepper.currentPosition()!=0) 
                 {
                      stepper.setSpeed(-4000);
                      stepper.runSpeed();
                 }
            }
        }

        if (rotationdirection) 
        { 
          if( stepper.currentPosition()+500<61000 )
          {
          stepper.move(500);
          while(stepper.distanceToGo() != 0)
            {
              stepper.setSpeed(4000);
              stepper.runSpeed();
            }
          }
          else
          {
            while (stepper.currentPosition()!= 61000) 
             {
                  stepper.setSpeed(4000);
                  stepper.runSpeed();
    
             } 
          }
        }
     }
   }
} 


void loop()
{ 
  //Begin Setup
  if(flag==0)
  {
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,28);
    display.println("Begin Setup");
    display.display(); 
    setspeed=1500;
    digitalWrite(Pin_Laser, HIGH);
  }
  
  //Set X in
  if(flag==1)
  {
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,28);
    display.println("Set X In");
    display.display(); 
    while(flag==1)
    {
    stepperposition(1); 
    }
    XInPoint=stepper.currentPosition();
  }


  //Set X out
  if(flag==2)
  {
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,28);
    display.println("Set X Out");
    display.display(); 
    while(flag==2)
    {
    stepperposition(1);
    Serial.println(stepper.currentPosition());
    }
    XOutPoint=stepper.currentPosition();
    
  }

  //Display Set Speed
  if(flag==3)
  {
    display.clearDisplay();
    display.setCursor(8,28);
    display.println("Set Speed");
    display.display();  
  }
  //Change Speed
  if(flag==4)
  {
    display.clearDisplay();
    SetSpeed();
  }
  //DisplayStart
  if(flag==5)
  {
    display.clearDisplay();
    display.setCursor(30,27);
    display.println("Start");
    display.display();
  }
  //Start
  if(flag==6)
  { 
    digitalWrite(Pin_Laser, LOW);
    display.clearDisplay();
    display.setCursor(20,27);
    display.println("Running");
    display.display();
    Serial.println(XInPoint);
    Serial.println(XOutPoint);
    gotoposition[0]=XOutPoint;
    stepper.setMaxSpeed(setspeed);
    stepper.moveTo(gotoposition);
    stepper.runSpeedToPosition();
    digitalWrite(Pin_Cutting, LOW);
    flag=flag+1;
  }
  //Slide Finish
   if(flag==7)
   {
    display.clearDisplay();
    display.setCursor(24,26);
    display.println("Finish");
    display.display();
   }
  //Return to start
   if(flag==8)
   {
    display.clearDisplay();
    Home();
    flag=0;
   }  
}

There is a lot of program code. I presume it got tested lots of times before it got that big. What was the last thing you changed or added that stopped it working?

Your comment "get stuck with the Begin Setup screen" does not really give any info about what the problem might be. Can you post some of the output from your program that could shed some light on the problem.

...R

Robin2:
There is a lot of program code. I presume it got tested lots of times before it got that big. What was the last thing you changed or added that stopped it working?

Your comment "get stuck with the Begin Setup screen" does not really give any info about what the problem might be. Can you post some of the output from your program that could shed some light on the problem.

...R

Thanks Robin.
A friend of mine used my computer, unfortunately they downloaded something. And my computer got infected with ransomware, I had to reinstall it completely.
Unfortunately I only keep the last backup of it.

  • I will try to strip and rewrite

Robin2:
There is a lot of program code. I presume it got tested lots of times before it got that big. What was the last thing you changed or added that stopped it working?

Your comment "get stuck with the Begin Setup screen" does not really give any info about what the problem might be. Can you post some of the output from your program that could shed some light on the problem.

...R

After loading the code into arduino, mount everything to the test board. after passing the Homing process. Everything is stopped at the screen with flag == 0 Begin Setup

GioLangLe:
Everything is stopped at the screen with flag == 0 Begin Setup

You must understand that I don't have your hardware so I can't run your program even if I wanted to. Consequently I am reliant on you to give me all the information I would have if I was running your program.

Can't you put some Serial.print() statements into the program to let you see what is happening and then post a sample of the output that they produce?

...R