Accelstepper EEPROM disagreement

There is a part that I can't do in my code: "y kaydedilen konuma gidiliyor" and "z kaydedilen konuma gidiliyor" in the "kaydetme" function. This part was working fine before I detailed it. However, there are some things that I can't integrate right now. I want to go to the positions that I saved in EEPROM when the key is closed, but the direction of the motors is not as I want and they go much more than necessary. I would be very happy if I could explain it. Thanks in advance for your contributions.
So my code works fine except for these parts:

 if (buton == 0 && anahtar_ayar == LOW)
  {
    Serial.print("Y Kaydedilen pozisyona gidiliyor: ");
    Serial.println(savedPos);
    stepper.moveTo(savedPos);
    stepper.runToPosition();
    motor1Pos = savedPos;
    delay(1000);
  }

  if (digitalRead(ButonKaydet2) == HIGH && anahtar_ayar == LOW)
  {
    Serial.print("Z Kaydedilen pozisyona gidiliyor: ");
    Serial.println(savedPos2);
    stepper2.moveTo(savedPos2);
    stepper2.runToPosition();
    motor2Pos = savedPos2;
    delay(1000);
  }

Full Code:

#include <AccelStepper.h>
#include <ezButton.h>
#include <EEPROM.h>

#define dirPin 2
#define stepPin 3

#define dirPin2 5
#define stepPin2 6

AccelStepper stepper = AccelStepper(1, stepPin, dirPin);
AccelStepper stepper2 = AccelStepper(1, stepPin2, dirPin2);

#define ButonKaydet2 16
#define deadZone 50 

ezButton joyButton(9);

int x, y;

const int limit_1 = 7; 
#define LIMIT_SWITCH_PIN 18

int limit1;

const int anahtar = 4;
int anahtar_ayar;

long motor1Pos = 0, savedPos = 0;
long motor2Pos = 0, savedPos2 = 0;

// EEPROM konumları
const int EEPROM_ADDR_Y = 0;
const int EEPROM_ADDR_Z = sizeof(long);

boolean birkez = false;

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

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
 
  pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
  pinMode(ButonKaydet2, INPUT);
  digitalWrite(16, LOW); 
  pinMode(limit_1, INPUT_PULLUP); 
  digitalWrite(18, HIGH); 

  stepper.setPinsInverted(false, false, false);
  stepper2.setPinsInverted(false, false, false);

  stepper.setMaxSpeed(1500);
  stepper.setAcceleration(750);

  stepper2.setMaxSpeed(3000);
  stepper2.setAcceleration(3000);

  // EEPROM'dan pozisyonları oku
  EEPROM.get(EEPROM_ADDR_Y, savedPos);
  motor1Pos = savedPos;  
  stepper.setCurrentPosition(motor1Pos); 
  Serial.print("Y Kaydedilen pozisyon: ");
  Serial.println(savedPos);

  EEPROM.get(EEPROM_ADDR_Z, savedPos2);
  motor2Pos = savedPos2;  
  stepper2.setCurrentPosition(motor2Pos); 
  Serial.print("Z Kaydedilen pozisyon: ");
  Serial.println(savedPos2);

 
}

void loop() 
{
 
  anahtar_ayar = digitalRead(anahtar);
  int buton = joyButton.getState();

  if (anahtar_ayar == HIGH && buton == 0)
  {
    savedPos = motor1Pos;  
    EEPROM.put(EEPROM_ADDR_Y, savedPos);
    Serial.print("Y Pozisyon kaydedildi: ");
    Serial.println(savedPos);
    delay(1000);
  }

  if (anahtar_ayar == HIGH && digitalRead(ButonKaydet2) == HIGH)
  {
    savedPos2 = motor2Pos;  
    EEPROM.put(EEPROM_ADDR_Z, savedPos2);
    Serial.print("Z Pozisyon kaydedildi: ");
    Serial.println(savedPos2);
    delay(1000);
  }

  if (buton == 0 && anahtar_ayar == LOW)
  {
    Serial.print("Y Kaydedilen pozisyona gidiliyor: ");
    Serial.println(savedPos);
    stepper.moveTo(savedPos);
    stepper.runToPosition();
    motor1Pos = savedPos;
    delay(1000);
  }

  if (digitalRead(ButonKaydet2) == HIGH && anahtar_ayar == LOW)
  {
    Serial.print("Z Kaydedilen pozisyona gidiliyor: ");
    Serial.println(savedPos2);
    stepper2.moveTo(savedPos2);
    stepper2.runToPosition();
    motor2Pos = savedPos2;
    delay(1000);
  }
  
  
  delay(100);
  motorhareket();
}

void motorhareket()
{
  joyButton.loop();

  x = analogRead(A0);
  y = analogRead(A1);

  limit1 = digitalRead(limit_1);

  if (digitalRead(LIMIT_SWITCH_PIN) == LOW) 
  {
    stepper2.stop();
    motor2Pos = 0;
    EEPROM.put(EEPROM_ADDR_Z, motor2Pos);  // 0 konumunu EEPROM'a kaydet
    Serial.println("Z pozisyonu EEPROM'a kaydedildi: 0");
    delay(1000);
  }

  if (limit1 == LOW) 
  {
    stepper.stop();
    stepper.setCurrentPosition(0);
    motor1Pos = 0;
    EEPROM.put(EEPROM_ADDR_Y, motor1Pos);  // 0 konumunu EEPROM'a kaydet
    Serial.println("Y pozisyonu EEPROM'a kaydedildi: 0");
    delay(1000);
  }

  while((x > 512 + deadZone) && limit1 == HIGH) 
  {
    stepper.setPinsInverted(true, false, false);
    stepper.setSpeed(1000);
    stepper.move(1000);
    stepper.runSpeedToPosition();
    
    motor1Pos--;

    x = analogRead(A0);
    limit1 = digitalRead(limit_1);

    if (!(x > 512 + deadZone) || limit1 == LOW) 
    {
      stepper.stop();
      break;
    }
  }

  while((y > 512 + deadZone) && digitalRead(LIMIT_SWITCH_PIN) == HIGH) 
  {
    stepper2.setPinsInverted(true, false, false);
    stepper2.setSpeed(2500);
    stepper2.move(1000);
    stepper2.runSpeedToPosition();

    motor2Pos--;

    y = analogRead(A1);
    
    if (!(y > 512 + deadZone) || digitalRead(LIMIT_SWITCH_PIN) == LOW) 
    {
      stepper2.stop();
      break;
    }
  }

  while (x < 512 - deadZone) 
  {
    stepper.setPinsInverted(false, false, false);
    stepper.setSpeed(1000);
    stepper.move(1000);
    stepper.runSpeedToPosition();

    motor1Pos++;

    x = analogRead(A0);

    if (!(x < 512 - deadZone)) 
    {
      stepper.stop(); 
      break;
    }
  }
 
  while (y < 512 - deadZone) 
  {
    stepper2.setPinsInverted(false, false, false);
    stepper2.setSpeed(2500);
    stepper2.move(1000);
    stepper2.runSpeedToPosition();

    motor2Pos++;

    y = analogRead(A1);

    if (!(y < 512 - deadZone)) 
    {
      stepper2.stop();
      break;
    }
  }
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Does your code act on the motors according to the joystick position (if so, is your joystick using a safe "deadzone")?

Yes, exactly. I suspect that I can't go to the saved position because I'm trying to perform and save my movements in a while loop, but I have to do this.

Your sketch is very hard to read and understand, because there are no comments and all variable names are not in english. And you are using the Accelstepper library in a very strange - and sometimes really wrong manner. E.g. The 'setPinsInverted' function is NOT meant to change the direction, and you disturb the other functions that are meant to control directions .

Yes, unfortunately, this happened because I changed the code too much, you are right. I'm just wondering about this right now. How can I increase the number of steps and write them to the eeprom while providing motor movement with an while loop? I can't do anything other than increasing the operation of the while loop.

Why? And which while loop do you mean? There a lot. Usually ( long lasting) while loops should be avoided as much as possible. You already have a continuous while loop: the loop() function. And you must correct the wrong usage of Accelstepper.
To help the helpers you should explain what your sketch is intended to do.

For the joystick... try to use movement that is intentional, and not just fluctuating power through a resistor. One idea would be to make a state change (press or hold a button for example) before moving the joystick. Another idea would be to make larger deadbands for a static joystick, as well as one in motion.

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