Stepper motor control

/*
 Stepper Motor Control - speed control

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.
 A potentiometer is connected to analog input 0.

 The motor will rotate in a clockwise direction. The higher the potentiometer value,
 the faster the motor speed. Because setSpeed() sets the delay between steps,
 you may notice the motor is less responsive to changes in the sensor value at
 low speeds.

 Created 30 Nov. 2009
 Modified 28 Oct 2010
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10,11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  //int knobReading = map(sensorReading, 0, 1023, 10, 2000);
  
 
int knobReading = map(sensorReading, 0, 1023, 10, 100);
// knobReading = constrain(knobReading, 10, 200);
  //int stepDelay = knobReading * 10;
 
  int stepDelay = knobReading;
  //(val * 1.76) + 600
   if (knobReading < 55) {

    if (knobReading < 500)

      
       // drive the stepper FORWARD using stepDelay
          digitalWrite(8, HIGH);
          digitalWrite(9. LOW);
          delayMicroseconds(stepDelay);
          digitalWrite(10, HIGH);
          digitalWrite(11. LOW);
          
          delay(stepDelay);

         

          }else{
            
          
    if (knobReading > 500) 
    
   // drive the stepper REVERS using stepDelay
          digitalWrite(11, LOW);
          digitalWrite(10. HIGH);
          delayMicroseconds(stepDelay);
          digitalWrite(9, LOW);
          digitalWrite(8. HIGH);
          
          }

Iam not sure what I am doing wrong but I am trying to get some 4wire stepper motors to move forward/backwards with a pot so I can up grade to a a couple or more 9 pin joysticks like these

http://www.ebay.com/sch/i.html?_odkw=9+pin+joystick+breakout+ps2+modual+arduino&_osacat=0&_from=R40&_trksid=p2045573.m570.l1313.TR0.TRC0.H0.X9+pin+joystick+breakout+modual+arduino.TRS0&_nkw=9+pin+joystick+breakout+modual+arduino&_sacat=0

for now I am just using a 10k pot on a breadboard to get the code working but no go yet and ideas have one hooked up and would like to add more my drivers all have 4 wire input and 4 wire stepper motors attached

You declare myStepper and then don't use it. The rest of the code is nothing like a stepper driver.

Steppers have 4 (or 8) states you have to cycle between, each step changes to the next in the sequence.
You have one state for forwards and one state for backwards - that's bogus.

You have the Stepper library there and fail to make use of it! Start by adapting one of the examples
that comes with the Stepper library?

Which stepper motors and drivers are you using?

MarkT:
You declare myStepper and then don’t use it. The rest of the code is nothing like a stepper driver.

Steppers have 4 (or 8) states you have to cycle between, each step changes to the next in the sequence.
You have one state for forwards and one state for backwards - that’s bogus.

You have the Stepper library there and fail to make use of it! Start by adapting one of the examples
that comes with the Stepper library?

Which stepper motors and drivers are you using?

I found this to be very helpful sense I am new to this stuff I changed some of the pin outs to fit my setup I am using some of the steppers he has in the video and a large L298N driver with a nema 17 stepper but the small one wont change direction or speed just goes one way at one speed when I get that right I will try to add more steppers to the code

Write a program that makes proper use of the stepper library. If it does not work then post a copy of YOUR code explaining what it actually does.

Post a link to the datasheet for your motor.

What motor power supply are you using (volts and amps) ?

...R
Stepper Motor Basics

Robin2:
Write a program that makes proper use of the stepper library. If it does not work then post a copy of YOUR code explaining what it actually does.

Post a link to the datasheet for your motor.

What motor power supply are you using (volts and amps) ?

…R
Stepper Motor Basics

/*------------------------------------------------------
Two_Steppers.ino     Arduining.com  30 MAY 2015

Driving two steppers concurrently with the Arduino Nano.
stepperX follows potentiometer on analog input 0.
stepperY follows potentiometer on analog input 1.
A timeout is used to turn off coils and save energy in battery
operated applications.

Hardware:
Arduino NANO.
Drivers: ULN2003A
Stepper Motors: 28BYJ48, 5VDC, step angle 5.625 °
                Gear reduction 64:1
                No-load starting frequency:> = 500PPS (4 rpm)
                Coil resistance 60 Ohms.               
 -----------------------------------------------------*/

#include <Stepper.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 2;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

#define  STEPSREV    4096    // 64(fullsteps) * 64 (reduction ratio)
#define  SPEED       4       // motor speed (RPM)

#define  COIL_1X     8
#define  COIL_2X     9
#define  COIL_3X     10
#define  COIL_4X     11

#define  COIL_1Y     7
#define  COIL_2Y     5
#define  COIL_3Y     6
#define  COIL_4Y     4

#define  POT_X       0
#define  POT_Y       1                
#define  TIMEOUT     1000    //Turns off after 1 sec of inactivity.
#define  NOISE       8       //inmunity in steps because analog noisy readings.

// create the instances of the stepper class.
Stepper stepperX(STEPSREV, COIL_1X, COIL_2X, COIL_3X, COIL_4X);
Stepper stepperY(STEPSREV, COIL_1Y, COIL_2Y, COIL_3Y, COIL_4Y);

int potValX,potValY;           // potentiometers analog readings
int Xpos,Ypos;                 // Actual steppers positions(0-4096)->(0-360°)
int newXpos, newYpos;          // New steppers positions
unsigned long stampX, stampY;  // last move time stamped.

//============== functions ====================================================

//Read the potentiometers and map the reading to mach 360 degrees.
void readPots(){
  potValX = analogRead(POT_X);          // read POT_X value (0-1023).
  potValY = analogRead(POT_Y);          // read POT_Y value (0-1023).
  newXpos= map(potValX,0,1023,0,2047);  // Map pot X range to one stepper turn.
  newYpos= map(potValY,0,1023,0,2047);  // Map pot Y range to the stepper turn.
}
 
//-----------------------------------------------------------------------------
// Aproach stepperX to the newX position.
void aproachX(int newX){
  int Xdir = Xpos<newX ? 1 : -1;
  stepperX.step(Xdir);        // move one step in the Xdir direction.
  Xpos += Xdir;               // update Xpos.
  stampX = millis();          // stamp actual time.
  }

//-----------------------------------------------------------------------------
// Aproach stepperY to the newY position.
void aproachY(int newY){
  int Ydir = Ypos<newY ? 1 : -1;
  stepperY.step(Ydir);        // move one step in the Ydir direction.
  Ypos += Ydir;               // update Ypos.
  stampY = millis();          // stamp actual time.
  }

//-----------------------------------------------------------------------------
//Check for inactivity and turn off the steppers coils to save battery.
void CheckTimeout(){
  if((millis() - stampX) > TIMEOUT){   //Turn Off StepperX coils.
    digitalWrite(COIL_1X, LOW);
    digitalWrite(COIL_2X, LOW);
    digitalWrite(COIL_3X, LOW);
    digitalWrite(COIL_4X, LOW);
  }
  if((millis() - stampY) > TIMEOUT){   //Turn Off StepperY coils.
    digitalWrite(COIL_1Y, LOW);
    digitalWrite(COIL_2Y, LOW);
    digitalWrite(COIL_3Y, LOW);
    digitalWrite(COIL_4Y, LOW);
  }    
}


//=================== setup ===================================================
void setup(){
  stepperX.setSpeed(SPEED);  // set the X motor speed.
  stepperY.setSpeed(SPEED);  // set the Y motor speed.
  readPots();
  Xpos = newXpos;
  Ypos = newYpos;
  myservo.attach(29);  // attaches the servo on pin 9 to the servo object
}

//================= main loop =================================================
void loop(){
  readPots();
//if diference is greater than NOISE move steppers.
  if(abs(newXpos - Xpos)> NOISE) aproachX(newXpos);
  if(abs(newYpos - Ypos)> NOISE) aproachY(newYpos);

  CheckTimeout();   //check for inactivity.

  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there


}

I modified it a little this morning after I found out what I did wrong I changed how my small Y access stepper was firing the coils work fine now so I was trying to add servos to the sketch not sure if I doing it right because as I wrote I am new to this and still learning also the nema 17 stepper is a 48oz/in stepper running from a L298n driver dual H bridge from a PC power supply 5v 25amp as is all servos will be and the small stepper motor I will try to get some 9pin joystick moduals from ebay as there cheap and will work better then turning knobs to move the steppers and servos when I get all the code and things work because I am making a robot head to move with me so when I tell story’s to the sick kids in our hospital it will move as if it is telling the story some day to add arms and such thank you for your help

wrongway-feldmen:
I modified it a little this morning after I found out what I did wrong I changed how my small Y access stepper was firing the coils work fine now so I was trying to add servos to the sketch not sure if I doing it right because as I wrote I am new to this and still learning also the nema 17 stepper is a 48oz/in stepper running from a L298n driver dual H bridge from a PC power supply 5v 25amp as is all servos will be and the small stepper motor I will try to get some 9pin joystick moduals from ebay as there cheap and will work better then turning knobs to move the steppers and servos when I get all the code and things work because I am making a robot head to move with me so when I tell story's to the sick kids in our hospital it will move as if it is telling the story some day to add arms and such thank you for your help

Look up "punctuation". I can't make sense of that.

And I am not familiar with 28byj48 stepper motors. They are very different from a bipolar Nema 17 motor. Which are you actually trying to use?

If it is NOT a 28byj then post a link to the motor datasheet (as requested in Reply #3

...R