Stepper Motor Code

I am working with a group to design an automated control valve. We are using a STEPPERONLINE 19:1 Planetary Gearbox High Torque Nema 17 Stepper Motor and a Sharp IR sensor. While running test with the code we created, we ran into an issue with getting the motor to settle on a fixed point. We believe that the code is ignoring the second for loop in our code and thus causing the issue, but are not sure why. Any feed back would be appreciated:

Part of code with For Loops:

  //Stepper Motor:
 if (setPoint-realPoint >= 0.1){
  digitalWrite(2, HIGH);
  for (i = 0; i <= 500; i++)
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    }
 }


 else if (setPoint-realPoint < 0){
  //delayMicroseconds(60);
  digitalWrite(2, LOW);
  for (i = 0; i <= 500; i++);
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    }    
 }
 }

Entire code:

#include <SharpIR.h>
#include <math.h>
#define logf
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            430 for GP2Y0A41SK   
/*
2 to 15 cm GP2Y0A51SK0F  use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series  use 430
10 to 80 cm GP2Y0A21YK0F  use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F  use 100550


 */
/*
  Smoothing


  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.


  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0


  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe


  This example code is in the public domain.


  http://www.arduino.cc/en/Tutorial/Smoothing
*/


// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;


int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average


int inputPin = A0;


int realPoint = 0;
int setPoint = 0;




void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }


  //Stepper Motor:
  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT); 
  //digitalWrite(2, LOW); // Determines Direction Stepper Motor Spins.


  
}
int i=0;
int f=0;


void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
 readings[readIndex] = 0.0311*(analogRead(inputPin))-2.4814;
 //float pinFloat = readings[readIndex] * 5.0 / 1023.0;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;


  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }


  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.print("\t");
  Serial.println(0.0311*(analogRead(inputPin))-2.4814);
  Serial.print("\t");
    Serial.print(setPoint-(0.0311*(analogRead(inputPin))-2.4814));
  realPoint = 0.0311*(analogRead(inputPin))-2.4814;
  delay(10);        // delay in between reads for stability




  //Stepper Motor:
 if (setPoint-realPoint >= 0.1){
  digitalWrite(2, HIGH);
  for (i = 0; i <= 500; i++)
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    }
 }




 else if (setPoint-realPoint < 0){
  //delayMicroseconds(60);
  digitalWrite(2, LOW);
  for (i = 0; i <= 500; i++);
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    }    
 }
 
/*
  if (error >= 0.5){
  digitalWrite(2, HIGH);
  if (i=0, i<500)
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    i=i+1;
    }
 }


 if (error <= 0.5){
  digitalWrite(2, LOW);
  if (i=0, i<500);
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    i=i+1;
    }
 }


  if (error >= .25){
  digitalWrite(2, HIGH);
  if (i=0, i<250)
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    i=i+1;
    }
 }


 if (error < .25){
  digitalWrite(2, LOW);
  if (i=0, i<250);
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    i=i+1;
    }
 }


  if (error <= .25){
  Serial.print("Done.");
  }
*/  
}

You are using integers

int realPoint = 0;
int setPoint = 0;

so this test with a float

 if (setPoint-realPoint >= 0.1){

does not mean much - it will trigger only if the difference is 1 or above.

an int is also of limited capacity - what’s the range of those values ?

what’s the point of doing this for loop?

  for (i = 0; i <= 500; i++)
    {
    digitalWrite(3,HIGH);
    digitalWrite(3,LOW);
    delayMicroseconds(60);
    }

just do

digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(3000);

(same for the other one)

You seem to be calculating a running average... and not using it for anything! After you calculate 'average', shouldn't you be using that instead of 'realPoint'?

Hi oodemi,

please post a description in normal words how the whole thing shall work.
I emphasize normal words. You seem to be a beginner in programming. So just posting code is not enough.
It might be that you have miscomceptions about how the code works. From the code itself nobody can see if and when where there might be a misconception.

And this is the reason to describe the functionality you want to achieve in normal words.

For analysing what the code is really doing you can add serial output and then watch in the serial monitor.

I just writethis short sentence to put you in my position. It is very likely that from this short sentence you don not know how to do it. That's my situation with your yet too short description.

So please take 10 to 20 minutes time writing a detailed desciption what your testporgram shall do
to speed up getting good answers within a few hours instead of 10 to 20 days.

best regards Stefan

I want to add some comments:

In your code you have “hardcoded” IO-pin 3 by using the number “3”.
This is considered as bad coding style. If - for what ever reason you have to change the IO-pin-number
you have to walk through your complete code adapting all “3” to something else.
And the more IO-Pins you use the harder it gets to remember what was IO-pin number 8, 9, 10 ,11 ???

This is the reason why you sould use identifiers for everything.

const byte dir_Pin  = 2;
const byte step_Pin = 3;

The lines of code explain themself

  pinMode(dir_Pin, OUTPUT); 
  pinMode(step_Pin, OUTPUT);

Whenever you have to change the IO-Pin-number you change it in one single line of code and you changed it everywhere.

here is your code with IO-pin-numbers with constants with self-explaining names

#include <SharpIR.h>
#include <math.h>
#define logf
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            430 for GP2Y0A41SK
/*
  2 to 15 cm GP2Y0A51SK0F  use 1080
  4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series  use 430
  10 to 80 cm GP2Y0A21YK0F  use 1080
  10 to 150 cm GP2Y0A60SZLF use 10150
  20 to 150 cm GP2Y0A02YK0F use 20150
  100 to 550 cm GP2Y0A710K0F  use 100550

  Smoothing

  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/


// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.

const int numReadings = 10;


int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

int realPoint = 0;
int setPoint = 0;

const byte dir_Pin  = 2;
const byte step_Pin = 3;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }

  //Stepper Motor:
  pinMode(dir_Pin, OUTPUT);
  pinMode(step_Pin, OUTPUT);
  //digitalWrite(dir_Pin, LOW); // Determines Direction Stepper Motor Spins.

}

int i = 0;
int f = 0;


void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = 0.0311 * (analogRead(inputPin)) - 2.4814;
  //float pinFloat = readings[readIndex] * 5.0 / 1023.0;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;


  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }


  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.print("\t");
  Serial.println(0.0311 * (analogRead(inputPin)) - 2.4814);
  Serial.print("\t");
  Serial.print(setPoint - (0.0311 * (analogRead(inputPin)) - 2.4814));
  realPoint = 0.0311 * (analogRead(inputPin)) - 2.4814;
  delay(10);        // delay in between reads for stability


  //Stepper Motor:
  if (setPoint - realPoint >= 0.1) {
    digitalWrite(dir_Pin, HIGH);
    for (i = 0; i <= 500; i++)
    {
      digitalWrite(step_Pin, HIGH);
      digitalWrite(step_Pin, LOW);
      delayMicroseconds(60);
    }
  }


  else if (setPoint - realPoint < 0) {
    //delayMicroseconds(60);
    digitalWrite(dir_Pin, LOW);
    for (i = 0; i <= 500; i++);
    {
      digitalWrite(step_Pin, HIGH);
      digitalWrite(step_Pin, LOW);
      delayMicroseconds(60);
    }
  }

  /*
    if (error >= 0.5){
    digitalWrite(2, HIGH);
    if (i=0, i<500)
      {
      digitalWrite(step_Pin,HIGH);
      digitalWrite(step_Pin,LOW);
      delayMicroseconds(60);
      i=i+1;
      }
    }


    if (error <= 0.5){
    digitalWrite(2, LOW);
    if (i=0, i<500);
      {
      digitalWrite(step_Pin,HIGH);
      digitalWrite(step_Pin,LOW);
      delayMicroseconds(60);
      i=i+1;
      }
    }


    if (error >= .25){
    digitalWrite(dir_Pin, HIGH);
    if (i=0, i<250)
      {
      digitalWrite(step_Pin,HIGH);
      digitalWrite(step_Pin,LOW);
      delayMicroseconds(60);
      i=i+1;
      }
    }


    if (error < .25){
    digitalWrite(dir_Pin, LOW);
    if (i=0, i<250);
      {
      digitalWrite(step_Pin,HIGH);
      digitalWrite(step_Pin,LOW);
      delayMicroseconds(60);
      i=i+1;
      }
    }


    if (error <= .25){
    Serial.print("Done.");
    }
  */
}

best regards Stefan