Hi there, I am a stark begginner with arduino, and so far have only been taking example code, and modifying it slightly for my own purposes.
Ive got a Nema 17 17HS4401S Stepper motor, and an appropriate L298N motor driver, and the needed power supplies and components.
I have had a project in mind for a long time, it is an autofeed for a small lathe, but basically what I envision is relatively simple.
I need a DPDT switch, such that my stepper motor goes in one direction when switch is flipped to one side, and stepper motor goes in the other direction when the switch is flipped to the other side. And then for the motor to be stationary when the switch is in the off or center position.
And I also want the speed of the motor to be controlled by a potentiometer, from zero to max, maybe 100 rpm or so.
So of course there is the Stepper_speedControl code that is provided as example code by arduino.
#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);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/200 of a revolution:
myStepper.step(stepsPerRevolution / 200);
}
}
And I also have been trying to learn from these youtube robojax tutorials, and he has a tutorial running a 28BYJ-48 Stepper Motor with 2 push buttons. But the code he provides is specific to that stepper motor and the driver he uses, here is the code. He is using a 28BYj-48 motor
I know you guys dont like outside links, but here is the robojax link to the video and code that I was watching to try to learn.Control Stepper motor 28BYj-48 with Arduino and 2 push buttons
I would love to try to combine the functionality of these two blocsk of code. I want to be able to control my NEMA 17 speed with the pot, but also have the direction functionality with some on off switches.
It probably would be pretty simple but I have little experience so far with any code, so I am lost as to how to start this project and combine these concepts.
He does it with push buttons but of course it would be similar with my DPDT switch switched out for his push buttons.
Thank you for any help.
int Pin1 = 10;//IN1 is connected to 10
int Pin2 = 11;//IN2 is connected to 11
int Pin3 = 12;//IN3 is connected to 12
int Pin4 = 13;//IN4 is connected to 13
int switchCW =2;//define input pin for CW push button
int switchCCW =3;//define input pin for CCW push button
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values
int poleStep = 0;
int dirStatus = 3;// stores direction status 3= stop (do not change)
void setup()
{
//Robojax Stepper Motor Code STPB-2
pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2
pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3
pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4
pinMode(switchCW,INPUT_PULLUP);
pinMode(switchCCW,INPUT_PULLUP);
}
void loop()
{
//Robojax Stepper Motor Code STPB-2
if(digitalRead(switchCCW) == LOW)
{
dirStatus =1;
}else if(digitalRead(switchCW) == LOW)
{
dirStatus = 2;
}else
{
dirStatus =3;
}
if(dirStatus ==1){
poleStep++;
driveStepper(poleStep);
}else if(dirStatus ==2){
poleStep--;
driveStepper(poleStep);
}else{
driveStepper(8);
}
if(poleStep>7){
poleStep=0;
}
if(poleStep<0){
poleStep=7;
}
delay(1);
//Robojax Stepper Motor Code STPB-2
}// loop
/*
* @brief sends signal to the motor
* @param "c" is integer representing the pol of motor
* @return does not return anything
*
* www.Robojax.com code June 2019
*/
void driveStepper(int c)
{
//Robojax Stepper Motor Code STPB-2
digitalWrite(Pin1, pole1[c]);
digitalWrite(Pin2, pole2[c]);
digitalWrite(Pin3, pole3[c]);
digitalWrite(Pin4, pole4[c]);
}//driveStepper end here