Problem with TB6600 Stepper driver

So this is the first time here.
Lovely sentence and often written here.
I have a problem with missing steps.
My setting is a NEMA 23 max amps 3A a rotary encoder and a TB6600 wich is set zu 1600 steps and 2,3amps.
The task I have in mind is to drive over the home position in every direction.
The problem now is the I missing steps in a way. The home position is moving.
I did some test and I'm lost. Is it the code or is it the setting or all together?
On the TBB6600 I connected the - Drive and -Pull with GND of my 5V system and +Pull and Drive withe Pin 8 and 9.
Now the code. I'm not a total greenhorn but it is my first project. So be not too hard withe it.

//------------------------------------------------------------------------------
//Sputteranlage RS485 Verbindung Slave
//OLED mit Encrementalgeber
//Verfasst am 4.3.2022

//------------------------------------------------------------------------------
//LIBRARIS
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

#include "OneButton.h"
//------------------------------------------------------------------------------
//Motor
// Befehle Für Motorsteurung
// stepper.move(10); bedeutet er geht 10 schritte nach forne weis aber nicht mehr wo er genau ist
// stepper.moveTo(10); bedeutet er geht genau auf position 10 also absolut
// stepper.setCurrentPosition(10), bedeutet wo er gerade steht ist jetzt die Position 10 absolut
AccelStepper stepper(1, 9, 10); // PUL- ist auf Pin 9 Arduino;DIR- ist auf Arduino Pin8

//------------------------------------------------------------------------------
//OLED 
#define OLED_RESET 4 // not used 
Adafruit_SSD1306 display(OLED_RESET);
#if(SDD1306_LCDHEIGHT != 64)
#endif
//------------------------------------------------------------------------------
//Taster
OneButton button1(6, true);   // Speicher von Position Grüne Tasten zwei
OneButton button2(7, true);   // ROT von Speicher 3 auf 1
OneButton button3(10, true);  // Blau von Speicher 3 auf 2
OneButton button5(11, true);  //Inialisieren
# define NotHalt 12
# define endAN 3
volatile byte LED_on = LOW;
//------------------------------------------------------------------------------
// Drehgeber
const int encoderCLK = 2; //CLK an endcoder war auf 2
const int encoderDT  = 4; //DT an endcoder war auf 3
int CLKNow;         //Status
int CLKPrevious;    //Status
int DTNow;
int DTPrevious;
volatile int encoderPos  = 0;        // a counter for the dial

// Relai für Motor
# define K1 A1
# define LED 13

//------------------------------------------------------------------------------
// Globale Varable
int wegGesamt = 1600;     //Einmal 360 Grad drehung=3200 schritte
int fasterDre = 10;       // SChneller drehgeber
int PoAnzeige = 0;
int encoderM  = 0;
int StepUp    = 0;
int StepDw    = 0;
 
bool teachStart  = false; //Teachen
bool techDone    = false; //Teachen abschliessen

bool spSteig     = false; // steigende Werte ablegen
bool spFall      = false; // fallende Werte ablegen
bool spSave      = false; // Speichern
bool PoUP        = false; // Aufsteigend fahren Ortte Anfahren
bool PoDown      = false; // Abfallende speicher Orte Anfahren

bool CountFast   = false; /// Drehgebr schneller zaehlen
bool CountSlow   = false; // Langsam Zählen
bool MotorEin    = false; // Drehen Aktiv
bool runallowed  = false; //Motor fahren erlaubt


//------------------------------------------------------------------------------
// Timer
float oldTimerDisp   = 0; //Aufruf des Display
float countfastTimer = 0; //drehgeschwindigkeit
float restfastTimer  = 0; // dregschwindigkeit zurücksetzen
void setup() 
{
  //---------------------------------------------------------------------------------------------------------------
  //DISPLAY
  //---------------------------------------------------------------------------------------------------------------
  Serial.begin(9600);  // output
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);

  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(1,0);
  display.println("Pos / Sap");
  display.display();
  
  //---------------------------------------------------------------------------------------------------------------
  //Eingänge SPS
  //---------------------------------------------------------------------------------------------------------------
  //Taster 1-3
  button1.attachClick(clickSpeicher);         // Speicher von Position Grüne Tasten zwei
  button2.attachClick(clickSteigen);          // Rot von Speicher 3 auf 1
  button2.attachDoubleClick(doublDriveSteigen);
  button3.attachClick(clickFallend);          // Blau von Speicher 3 auf 2
  button3.attachDoubleClick(doublDriveFallend);
  button5.attachClick(clickTechSta);          // Initialisieren
  button5.attachDoubleClick(doubleclickNewInt);
  
  //NotHalt
  pinMode(NotHalt, INPUT_PULLUP);
  
  //Inkrementgeber
  pinMode(encoderCLK, INPUT_PULLUP);
  pinMode(encoderDT , INPUT_PULLUP);
  //pinMode(endAN     , INPUT_PULLUP); 

  attachInterrupt(digitalPinToInterrupt(encoderCLK), encoder_interrup, CHANGE); // encoder pin on interrupt 0 (pin 2)
 // attachInterrupt(digitalPinToInterrupt(endAN), NullPo ,FALLING);
  //Aktuelle Werte von drehgeber
  CLKPrevious = digitalRead(encoderCLK);
  DTPrevious  = digitalRead(encoderDT);

  //Ausgaenge
  //Motor
  stepper.setMaxSpeed(60);     //Geschwindigkeit = schritte / sekunden//60
  stepper.setAcceleration(75);  //Beschleunigung = Schritt/ sekunden ^2//75 eingestellt 
  stepper.disableOutputs();
  //Wichitg TB6600 haben wir auf hohe Schrittzahl pro Voller Umdrehung gestellt
  //stepper.disableOutputs();
  //Relais
  pinMode(K1, OUTPUT);
  pinMode(LED,OUTPUT);
  CountSlow = true;

}

void loop() 
{
  //Relais
  digitalWrite(K1, HIGH);
  //------------------------------------------------------------------------------
  // Tasterabfrage ueber onButton.h library
  button1.tick(); // Speichern
  button2.tick(); // Aufsteigend mit Übertrag
  button3.tick(); // Abfahllend mit Übertrag
  button5.tick(); // Initalisierung
  //------------------------------------------------------------------------------
  // Display
  
  if(millis() - oldTimerDisp > 200)
  {
    if(encoderPos == -1)
    {
      PoAnzeige = 1599;
    }
    else if(encoderPos == 1601)
    {
      PoAnzeige = 1;
    }
    else if (encoderPos == 1600)
    {
      PoAnzeige = 0;
    }
    else if ((encoderPos >=0) && ( encoderPos <= wegGesamt))
    {
      PoAnzeige = encoderPos;
    }
    
    dummy_display();
    oldTimerDisp = millis();
  }
  
  //------------------------------------------------------------------------------
  // Motor Drehen ueber drehgeber auf Positionen
  if ((1600 >= encoderPos) && (encoderPos >= 0) && (runallowed == false))
  {
    RunTheMotorDrehgeber();
  }
  if((encoderPos == 1601) )
  {
    Serial.println("Up");
    RunMotorUpNull();
    RunTheMotor();
  }
  if (encoderPos == -1)
  {
    Serial.println("DOWn");
    RunMotorDownNull();
    RunTheMotor();
  }
    

}
//------------------------------------------------------------------------------
//Endschalter wird Eingelernt zum erstenmal
//------------------------------------------------------------------------------
void clickTechSta()
{
   stepper.enableOutputs();            //fahr frei
   stepper.setAcceleration(60);
   stepper.setMaxSpeed(100);
   stepper.moveTo(0);  
}
//------------------------------------------------------------------------------
//Erneut Initialisierung
//------------------------------------------------------------------------------
void doubleclickNewInt()
{
  Serial.println("auf null");
  stepper.disableOutputs();
  stepper.move(100);
  teachStart = false;
  techDone   = false;
}
//------------------------------------------------------------------------------
//Speicher auswählen
//------------------------------------------------------------------------------
void clickSpeicher()
{
  spSave = true;
}
//------------------------------------------------------------------------------
//Steigend Speicher auswählen
//------------------------------------------------------------------------------
void clickSteigen()
{
  spSteig = true;
}
//------------------------------------------------------------------------------
//Fallender Speicher auswählen
//------------------------------------------------------------------------------
void clickFallend()
{
  spFall = true;
}
//------------------------------------------------------------------------------
//Steigend Werte Anfahren
//------------------------------------------------------------------------------
void doublDriveSteigen()
{
  PoUP = true;
}
//------------------------------------------------------------------------------
//Fallende Werte Anfahren
//------------------------------------------------------------------------------
void doublDriveFallend()
{
  PoDown = true;
}
//------------------------------------------------------------------------------
//Fallende Werte Anfahren
//------------------------------------------------------------------------------
void dummy_display()
{
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(1,0);
  display.println("Bereit");
  display.setCursor(1, 17);
  display.println(PoAnzeige);
  display.setCursor(62, 17);
  display.display();
  
}
//------------------------------------------------------------------------------
//Gabellichtschranke
/*
void NullPo()
{
  LED_on =! LED_on;
  encoderM = encoderPos;
  Serial.println(encoderM);
  digitalWrite(LED,LED_on);  
}*/
//------------------------------------------------------------------------------
//Endcoder Derichtung Rechts links mit Zähler
// Interrupt dürfen nicht getrennt werden immer nur eine Funktion für zwei Richtungen
//Pin2 nach Rechts und Pin 3 nach links
//------------------------------------------------------------------------------

void encoder_interrup()
{ 
 int rest = 0;
  CLKNow    = digitalRead(encoderCLK);
  //DTNow     = digitalRead(encoderDT);
  //------------------------------------------------------------------------------
  //Zaehlen langsam
  if ((CLKNow != CLKPrevious) && (CLKNow == 1) )
  {
    //Langsamdrehen Links
    if ((digitalRead(encoderDT) != CLKNow) && (CountSlow == true) && (runallowed == false))
    { // dreht er links
      if((encoderPos <= wegGesamt) && (encoderPos >= 0))
      {
        encoderPos++; 
      }
        
    }
    // Langsamdrehen  Rechts
    else if ((encoderPos >= 0) && (CountSlow == true) && (runallowed == false))
    { //dreht er rechts
      encoderPos--;
    }
   
  }
  CLKPrevious = CLKNow;
  DTPrevious  = DTNow;

}
//------------------------------------------------------------------------------
//Motor Drehbeber Bewegung
void RunTheMotorDrehgeber()
{
  
      //hier habe ich gerade geändert 3201 auf 3200
      stepper.enableOutputs();            //fahr frei
      stepper.moveTo(encoderPos);    //Richtungswechsel so das dreh geber mit motor stimmt
      while(stepper.distanceToGo() != 0)  //Warten auf stehenbleiben Position
      {
        stepper.runToNewPosition(encoderPos);
      }
 
}
//------------------------------------------------------------------------------
//Motor ueber Null
void RunMotorUpNull()
{// Habe hier das einfache agoment in dei weil grade gesetzt
    //nochmal laufen lasen
    
    switch (StepUp)
    {
     //------------------------------------------------------------------------------
     //Start
     case 0:
      runallowed = true;
      stepper.enableOutputs();            //fahr frei
      stepper.setAcceleration(60);
      stepper.setMaxSpeed(100);
      stepper.move(1);    //Richtungswechsel so das dreh geber mit motor stimmt
      StepUp = 1;
     break;
    //------------------------------------------------------------------------------
    //Warten auf position
    case 1:
    if(stepper.distanceToGo() == 0)
    {
      runallowed = false;
      encoderPos = 1;
      stepper.setCurrentPosition (encoderPos);
      StepUp = 0;
    }
    break;
    }
      
  
}
//------------------------------------------------------------------------------
//Motor auf 
void RunMotorDownNull()
{
  
  switch (StepDw)
  {
   //------------------------------------------------------------------------------
   //Start
   case 0:
   Serial.println("da");
   runallowed = true;
   stepper.enableOutputs();            //fahr frei
   stepper.setAcceleration(60);
   stepper.setMaxSpeed(100);
   stepper.move(-1);    //Richtungswechsel so das dreh geber mit motor stimmt
   StepDw = 1;
   break;    
  //------------------------------------------------------------------------------
  //POS
  case 1:
   if(stepper.distanceToGo() == 0)
    {
      Serial.println("ha");
      runallowed = false;
      encoderPos = 1599;
      stepper.setCurrentPosition (encoderPos);
      StepDw = 0;
    }
   break;
  }
      
}
//------------------------------------------------------------------------------
//Motor auf 
void RunTheMotor()
{
  if (runallowed == true)
  {
    stepper.enableOutputs();  //freigabe
    stepper.run();
  }
  else if (runallowed == false)
  {
    stepper.disableOutputs();
    stepper.stop();
  }
}

Please post a link to the datasheet of the encoder.
Did You try to attach a piece of tape to rhe stepper axle and see how that moves? Suppose the encoder code is tricking You?

So sorry.
Thanks for the re post.
The roter Encoder is one of amazon AZDelivery KY-040.
I soldered already a another roter Encoder on it wich has less noise.
About the strep I put a pointer on it so I can sea it better.
What I also tested was to move from 0 to 1600 and beck to 0 .
The rotary does as few bounces but that's not the problem because it cams to the start point.
So I think that's working and so I'm surprised that the if I put in the tow function at the end and the restriction at encoder_interrup that is not propperly does.
I'm not sure about the the tow Funktion RunMotorUpNull and RunMotorDownNull on the end.
The reason is that there I write a new position to the accelstepper library.
I think that there is the mistake that for now reason the Funktion is to fast and the motor to slow. Or in a way the RunTheMotorDrehgeber function is still at work and so does drive the motor to fare.

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