Hey guys.
kind of embarrassing that its come to this. I've been scouring the web for a couple hours now and tried a few things but cant seem to get this motor1.step() funtion to work. Brain is fried at this point and I'm just staring with no new ideas.
I know that you need a constructor for classes outside of the .ino file and I've seen how to make those work but nothing inside the file. was planning on finishing up the class and exporting/translating it after i was done working with it inside the main file. Maybe this is a public /private conflict? I'm kind of a newbie at c++ and i know I'm missing something really basic. Any help would be greatly appreciated.
also i know the program as a whole is not going to work yet so dont look into it that deeply. just need to get this issue resolved to keep going.
#define PUL_PIN_1 2
#define DIR_PIN_1 3
#define ENA_PIN_1 4
class StepperMotorDriver {
private:
byte pul_Pin; //pulse pin on motor driver
byte dir_Pin; //direction pin on motor driver
byte ena_Pin; //enable pin on motor driver
int pul_state = LOW; //
int direction; // 1 is CW and 2 is CCW
int steps_per_revolution; // enter how many steps are in one revolution. can be changed on stepper motor driver with switches
unsigned long previous_mircos = 0;
int speed; // interval between steps in microseconds
public:
StepperMotorDriver(int steps_per_revolution, byte pul_Pin, byte dir_Pin, byte ena_Pin) { // constructor for class
this->steps_per_revolution = steps_per_revolution;
this->pul_Pin = pul_Pin;
this->dir_Pin = dir_Pin;
this->ena_Pin = ena_Pin;
init();
}
void init() { // set pin type and all pins to LOW
pinMode(pul_Pin, OUTPUT);
pinMode(dir_Pin, OUTPUT);
pinMode(ena_Pin, OUTPUT);
digitalWrite(pul_Pin, LOW);
digitalWrite(dir_Pin, LOW);
digitalWrite(ena_Pin, LOW);
}
void setSpeed(int speed) {
this->speed = speed;
}
void step(byte pulPin, unsigned long previous_micros, int speed, int pul_state) {
unsigned long current_micros = micros(); // used count micros for speed without using delay()
if (current_micros - previous_micros >= speed / 2) {
previous_micros = current_micros;
if (pul_state == LOW) {
pul_state = HIGH;
}
else {
pul_state = LOW;
}
}
digitalWrite(pul_Pin, pul_state);
}
};
void setup () {
Serial.begin(115200);
Serial.setTimeout(10);
Serial.println("I am awake.");
StepperMotorDriver motor1(400, PUL_PIN_1, DIR_PIN_1, ENA_PIN_1); // set up stepper motor.
}
void loop () {
motor1.step();
}
OUTPUT:
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino: In function 'void setup()':
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:65:15: error: no matching function for call to 'StepperMotorDriver::step()'
motor1.step();
^
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:41:10: note: candidate: void StepperMotorDriver::step(byte, long unsigned int, int, int)
void step(byte pulPin, unsigned long previous_micros, int speed, int pul_state) {
^~~~
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:41:10: note: candidate expects 4 arguments, 0 provided
exit status 1
Compilation error: no matching function for call to 'StepperMotorDriver::step()'
You are creating an instance of StepperMotorDriver named motor1 within the setup() function so its scope is limited to that function
Create it as s global instance outside of a function
I tried creating it just a line above the setup() function.
OUTPUT:
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino: In function 'void loop()':
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:70:15: error: no matching function for call to 'StepperMotorDriver::step()'
motor1.step();
^
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:41:10: note: candidate: void StepperMotorDriver::step(byte, long unsigned int, int, int)
void step(byte pulPin, unsigned long previous_micros, int speed, int pul_state) {
^~~~
C:\Users\Sat-L\OneDrive\Documents\Arduino\Stepper_branch_moveto_angle_class_oriented\Stepper_branch_moveto_angle_class_oriented.ino:41:10: note: candidate expects 4 arguments, 0 provided
exit status 1
Compilation error: no matching function for call to 'StepperMotorDriver::step()'
Here is a revised version of your sketch that compiles, not that I am sure that it works
#define PUL_PIN_1 2
#define DIR_PIN_1 3
#define ENA_PIN_1 4
class StepperMotorDriver
{
private:
byte pul_Pin; //pulse pin on motor driver
byte dir_Pin; //direction pin on motor driver
byte ena_Pin; //enable pin on motor driver
int pul_state = LOW; //
int direction; // 1 is CW and 2 is CCW
int steps_per_revolution; // enter how many steps are in one revolution. can be changed on stepper motor driver with switches
unsigned long previous_mircos = 0;
int speed; // interval between steps in microseconds
public:
StepperMotorDriver(int steps_per_revolution, byte pul_Pin, byte dir_Pin, byte ena_Pin)
{ // constructor for class
this->steps_per_revolution = steps_per_revolution;
this->pul_Pin = pul_Pin;
this->dir_Pin = dir_Pin;
this->ena_Pin = ena_Pin;
init();
}
void StepperMotorDriver::init()
{ // set pin type and all pins to LOW
pinMode(pul_Pin, OUTPUT);
pinMode(dir_Pin, OUTPUT);
pinMode(ena_Pin, OUTPUT);
digitalWrite(pul_Pin, LOW);
digitalWrite(dir_Pin, LOW);
digitalWrite(ena_Pin, LOW);
}
void StepperMotorDriver::setSpeed(int speed)
{
this->speed = speed;
}
void StepperMotorDriver::step(byte pulPin, unsigned long previous_micros, int speed, int pul_state)
{
unsigned long current_micros = micros(); // used count micros for speed without using delay()
if (current_micros - previous_micros >= speed / 2)
{
previous_micros = current_micros;
if (pul_state == LOW)
{
pul_state = HIGH;
}
else
{
pul_state = LOW;
}
}
digitalWrite(pul_Pin, pul_state);
}
};
StepperMotorDriver motor1(400, PUL_PIN_1, DIR_PIN_1, ENA_PIN_1); // set up stepper motor.
void setup()
{
Serial.begin(115200);
Serial.setTimeout(10);
Serial.println("I am awake.");
}
void loop()
{
motor1.step(0, 0, 0, 0); //change values to match requirements
}
It cleared all of the OUTPUT errors but i believe that is changing all of the pulPin, previous_micros, speed, and pul_state to 0. which destroys the function of step()
motor1.step(0, 0, 0, 0); //change values to match requirements
Did you take note of the comment ?
1 Like
What the error message and @UKHeliBob are telling you is there is no step() function with no parameters.
no i missed that but the idea is to have and independent function that steps and everything is done inside the class. i feel as though its too much repetition if called like that. for example: i already called the pul_Pin out earlier and it makes no sense to call it out every time i want to move motor1.
exactly. do i have to make the variables i was passing in public to avoid this?
I was about to ask you about that
You don't need to pass parameters to the step() function that are private and, therefore, known to the instance of the class
If you don't want to pass parameters to the function then write it so that they are not required
Define another step() function with no parameters that does the action you want. There can be multiple definitions of a function with different parameters and the compiler will sort out which one to call.
thanks guys. i may be back if i cant figure out how to make it work after revising step().
Since everything you were trying to pass is just member variables, there's no reason to have any arguments. All of those variables are already available in the function.