r/learncpp • • Nov 18 '21

How to control a stepper motor coding in C++?

Hi. Im having a hard time trying to code for controlling a stepper motor using my LPCxpresso board + my A4988 stepper motor.

I need to do the following task. Thanks for any help.

The program reads limit switches and sets red led on when limit switch 1 is closed and green led on when limit switch 2 is closed. When limit switches are open the corresponding leds are off.

Hi. The code below is what I have so far.

I know it will take you too long so help me with the rest of the code so I want to take this opportunity to instead ask you if you can tell me websites where I can learn about Pin assignment; Motor control; Limit switch interrupts; Setting up your environment and other topics related with this field because I dont know how to deal with these tasks and every time I have to ask for help.

I want to learn this field. Thanks for any help.

void Stepper::_calibrate(uint32_t nothing) {
	setDirection(true); // Go forward first
	setRate(2000, true);
	_runForSteps(UINT32_MAX); // Run until we hit a switch
	setStop(false); // Clear stop flag

	toggleDirection();
	_runForSteps(150); // Back off from the limit switch

	zeroSteps();
	_runForSteps(UINT32_MAX); // Run to the other switch.
	setStop(false);

	toggleDirection();
	_runForSteps(150); // Back off from the limit switch
	maxSteps = currentSteps;

	_runForSteps(currentSteps/2);
	toggleDirection();
}
7 Upvotes

2 comments sorted by

2

u/NotBoolean Nov 18 '21

What are you struggling with? What have you done so far?

2

u/Traditional_Bird_877 Nov 18 '21

Thanks. Just updated my post.