AccelStepper help

Hello,
Im trying to make a stepper control using AccelStepper library. My two stepper motors are nema 23 astrosyn unipolar 9.9kgf 6wires controlled by a tip122 circuit. It works very well, although im having several difficulties on how to code these motors.
Please , keep in mind that im not a coder, Im a hobbist trying to make things move. :slight_smile:
I made a sketch based upon arduino examples (calibrate, accelstepper, analogread) and I tried to make it work mixing some lines of each code. Here is what I´ge got (please, keep in mid that Im not a coder.I´ve been studying programming since june).

#include <AccelStepper.h>
//#include <AFMotor.h>
AccelStepper stepper1 (4,2,3,4,5); // Defaults to 4 pins on 2, 3, 4, 5 
AccelStepper stepper2(4,9,10,11,12);
// two stepper motors one on each port
//AF_Stepper motor1(200, 1);
//AF_Stepper motor2(200, 2);
int vertpotPin =0;
int horzpotPin=1;
int potValuevert;
int potValuehorz;
float horzspeed;
float vertspeed;
int sensorValue=0;
int sensorMax = 0;  
int sensorMin = 1023;
int valh;
int valv;
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  stepper1.moveTo(200);
 //(stepper1.distanceToGo() == 0);
	//stepper1.moveTo(-stepper1.currentPosition());

  stepper1.setSpeed (valv);
  
  // stepper1.run();
   
}
void backwardstep1() {  
  stepper1.moveTo(200);
   //AccelStepper stepper1(BACKWARD, SINGLE);
    if (stepper1.distanceToGo() == 0)
	stepper1.moveTo(-stepper1.currentPosition());
    stepper1.setSpeed (valv);
   // stepper1.run();
}
// wrappers for the second motor!
void forwardstep2() {  
  stepper2.moveTo(200);
   //AccelStepper stepper2(FORWARD, SINGLE);
    stepper2.setSpeed (valh);
    //(stepper2.distanceToGo() == 0);

   // stepper2.run();
}
void backwardstep2() {  
  stepper2.moveTo(200);
 //AccelStepper stepper2(BACKWARD, SINGLE);
  
  stepper2.moveTo(-stepper1.currentPosition());
 // stepper2.run();
}

// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
//AccelStepper stepper1(forwardstep1, backwardstep1);
//AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
    stepper1.runToNewPosition(0);
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(100.0);
    //stepper1.setSpeed(50);
 
    stepper2.runToNewPosition(0); 
    stepper2.setMaxSpeed(200.0);
    stepper2.setAcceleration(100.0);
    //stepper2.setSpeed(50);
      
 valv = map(potValuevert, 0, 1023, 0, 200);
 valh = map(potValuehorz, 0, 1023, 0, 200);


  // Calibrar por tres segundos
  while (millis() < 1000) {
    sensorValue = analogRead(vertpotPin);
    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
}
  
void loop()
{
  stepper1.run();
  stepper2.run();
   
  
  potValuevert=analogRead (vertpotPin);
     if (vertpotPin <sensorMax){
               stepper1.setSpeed (valv);
               backwardstep1();
           
              }
    else if (vertpotPin>sensorMax){
               stepper1.setSpeed (valv);
                forwardstep1();
              
              }
 

	
    potValuehorz=analogRead (horzpotPin);
 
    if (horzpotPin <sensorMax){
      stepper1.setSpeed (valh);
                         backwardstep2();
              }

    else if (horzpotPin>sensorMax){
      stepper1.setSpeed (valh);
                        forwardstep2();
               }
   
    
      
}

The result is: one stepper makes a complete turn , and nothing happens to the other one.I´d be very glad if I could get some help understanding how to make it turn back and forward controlled by a joystick using accelstepper library.
Thank you for your time.

The code in loop only refers to stepper1, you copied/pasted and forgot to change stepper1 to stepper2

OK,

I tried this coide and Arduino keeps hang.Whats wrong with this code?Dunno what to do:This sketch should rotate the stepper clockwise and anticlockwise.:frowning:

#include <Stepper.h>


  const int stepsPerRevolution = 200;  
  Stepper myStepper(stepsPerRevolution, 6,11,7,9);   
  int sensorReading = analogRead(0);
   
void setup() {
   
}
void loop() {
      

 int motorSpeed =  map(sensorReading, 400, 720, 0, 100);
 int motorSpeed2 = map(sensorReading, 0, 320, 0, 100);


 
           
  analogRead(0);
 if (sensorReading<320){ 
   myStepper.step(stepsPerRevolution/100);
    myStepper.setSpeed(motorSpeed);
 }
  else if (sensorReading >400){
      myStepper.step(-stepsPerRevolution/100);
    myStepper.setSpeed(motorSpeed2); 

  }
}
  1. Remove the line "int sensorReading = analogRead(0);". [You shouldn't call library functions when initialising global variables.]

  2. In loop() change "analogRead(0);" to "int sensorReading = analogRead(0);". [You were doing a fresh read of the pin and throwing the result away.] Also, put this line right at the beginning of loop().

  3. Shouldn't you make the setSpeed calls before the step calls, instead of after?

Thank you again DC42.Programming is a new universe to me. Everyday I learn a bit reading this forum and tutorials around the web. I made a totally new sketch and now seems to work. Although the stepper doesn´t run smoothly, now i can control it forward and reverse.Now I need to solve the "smoothness" of these unipolar motors.

Thank you again.