Stepper Motor Objects and millis()


so i've been trying write own object oriented approach controlling stepper motor arduino nano (i know there premade libraries, prefer writing own code).

in attached stepper_motor_class header file, defined method calls singlestep() function every x number of seconds, , y number of steps. code works absolutely fine when x 1000 milliseconds (ie calls singlestep() function every 1000 milliseconds many steps (y) want).

however, instant reduce time 5 milliseconds (or 50 or 100 etc) step counter breaks down. instead of going 1500 steps, jumps out of while loop after doing 28.

i @ wits end figuring out why doing this. can me please?

sorry dont know how upload code :o

#include "stepper_motor_class.h" //class stepper motor

const char motor_pin1 = 6;
const char motor_pin2 = 5;

stepper_motor stepper (motor_pin1, motor_pin2);

//initialize objects begin method
void setup() {
  serial.begin(9600);
  stepper.begin();
  stepper.stepsconstantspeed(50,1000);//put in setup debug
}

void loop() {
}

header file:
#include "arduino.h"

class stepper_motor
{
  public:
  stepper_motor (int pin, int pin2);
  stepper_motor (unsigned long rpm, double steps);
  void begin();
  void singlestep();
  void stepsconstantspeed (unsigned long _rpm, double _steps);

  private:
  int _pin_status;
  int _pin2_status;
  int _done;
  double _steps;
  int _timer;
  unsigned long _rpm;
  int _counter;
  int _pin;
  int _pin2;
  unsigned long _startpoint;
};

//constructor
stepper_motor::stepper_motor (int pin, int pin2)
{
  _pin = pin;
  _pin2 = pin2;
  }

stepper_motor::stepper_motor (unsigned long rpm, double steps)
{
  _rpm = rpm;
  _steps = steps;
}


//**********************************************************************************
//**********************************************************************************

void stepper_motor::begin()//this method initializes pins , variables stepper function
{
  pinmode(_pin, output);//set pin output
  pinmode(_pin2, output);
  digitalwrite(_pin, low);
  digitalwrite(_pin2, low);
  _pin_status = 0;
  _pin2_status = 0;
  _counter = 0;
  _done = 0;
 
}

void stepper_motor::singlestep()//this method moves stepper forward according sequence
{
  if (_pin_status == 0 && _pin2_status == 0 && _done == 0)
    {
      digitalwrite(_pin, low);
      digitalwrite(_pin2, high);
      _pin_status = 0;
      _pin2_status = 1;
      _done = 1;
    }

    if (_pin_status == 0 && _pin2_status == 1 && _done == 0)
    {
      digitalwrite(_pin, high);
      digitalwrite(_pin2, high);
      _pin_status = 1;
      _pin2_status = 1;
      _done = 1;
    }
   
    if (_pin_status == 1 && _pin2_status == 1 && _done == 0)
    {
      digitalwrite(_pin, high);
      digitalwrite(_pin2, low);
      _pin_status = 1;
      _pin2_status = 0;
      _done = 1;
    }
   
    if (_pin_status == 1 && _pin2_status == 0 && _done == 0)
    {
      digitalwrite(_pin, low);
      digitalwrite(_pin2, low);
      _pin_status = 0;
      _pin2_status = 0;
      _done = 1;
    }
   
    _done = 0;
 }

void stepper_motor::stepsconstantspeed (unsigned long _rpm, double _steps)//this method moves motor x steps @ constant speed
{
  _startpoint = millis();
  serial.print (_startpoint);

  while (millis() <= _startpoint + _rpm)//constant speed loop
  {
    if (millis() == _startpoint + _rpm)//will execute step every _rpm milliseconds
    {
       if (_counter <= _steps)//exits while loop when counter = steps
       {
         singlestep();
         _startpoint = millis();
         serial.print (_startpoint);
         _counter ++;
         serial.print ("step");
         serial.print (_counter);
       }
    }
  }
 
  _counter = 0;
 
  serial.print ("im done");
  serial.println();
}








Arduino Forum > Using Arduino > Programming Questions > Stepper Motor Objects and millis()


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???