STPLPB-01: Controlling Stepper Motor Speed with an L298N Module and Arduino
This is code to control the speed of a bipolar four-wire stepper motor with an L298N module and three push-button switches.319-STPLPB-01: Controlling Stepper Motor Speed with an L298N Module and Arduino
语言: C++
/*
*
* Original code from Arduino IDE: Stepper Motor Control - one revolution
* Written by Ahmad Shamshiri on April 30, 2020 in Ajax, Ontario, Canada
*
* This Arduino code allows you to control the speed of a stepper motor using an L298N module
* with 3 push buttons to slow it down, speed it up, start or stop the motor.
* Direction of rotation is set in the code.
* Video code: STLPB-01 on YouTube
*
* Watch video for details: https://youtu.be/csf_G1pi0ps
*
* See wiring diagram and full explanation of the code: http://robojax.com/L/?id=62
You can get the wiring diagram and full explanation of this code from my Arduino Course at Udemy.com.
Learn Arduino step by step with all libraries, codes, and wiring diagrams all in one place.
Visit my course now: http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can be my patron and have early access to my videos here: http://robojax.com/L/?id=63
or make a donation using PayPal: http://robojax.com/L/?id=64
*
* Code is available at http://robojax.com/learn/arduino
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Stepper.h>
//Robojax.com L298N Stepper Speed STLPB-01
const int speedPBInc =2;//define input pin
const int stopPB=3;//define input pin for Stop push button
const int speedPBDec =4;//define input pin
const int motorPin[] ={8, 9, 10, 11};
const int direction =1;//0 for CW, 1 for CCW;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
int speedStep = 5;
int stepMinimum = 5;
int stepMaximum = 100;
int stopType =0;//0=fully stopped , 1=hold (consumes energy)
int currentSpeed=60;
int currentSPR=stepsPerRevolution;
#define START 1 //
#define STOP 0
#define CW 1
#define CCW -1
int motorStopState=STOP;//change if you need to
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, motorPin[0], motorPin[1], motorPin[2], motorPin[3]);
void setup() {
//Robojax.com L298N Stepper Speed STLPB-01
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(speedPBInc, INPUT_PULLUP);
pinMode(stopPB,INPUT_PULLUP);
pinMode(speedPBDec,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(stopPB), stopMotor, FALLING);
}
void loop() {
//Robojax.com L298N Stepper Speed STLPB-01
updateState();
if(!motorStopState)
{
currentSPR =0;
}else{
currentSPR =stepsPerRevolution;
}
myStepper.setSpeed(currentSpeed);
if(direction ==1)
{
myStepper.step(-currentSPR);
}else{
myStepper.step(currentSPR);
}
}//loop
/*
* updateState()
* @brief reads push buttons and updates values
* @param none
* @return no return
* Written by Ahmad Shamshiri for robojax.com
* on Nov 01, 2019 at 18:10 in Ajax, Ontario, Canada
*/
void updateState()
{
//Robojax.com L298N Stepper Speed STLPB-01
if(digitalRead(stopPB) ==LOW)
{
motorStopState =1-motorStopState;// stop the motor
if(motorStopState ==STOP)
{
stopMotor();
}
delay(500);
}
if(digitalRead(speedPBInc) ==LOW)
{
motorStopState = START;
currentSpeed += speedStep;
if( currentSpeed >=stepMaximum ) currentSpeed =stepMaximum ;
}
if(digitalRead(speedPBDec) ==LOW)
{
motorStopState = START;
currentSpeed -= speedStep;
if( currentSpeed <stepMinimum ) currentSpeed =stepMinimum ;
}
//Robojax.com L298N Stepper Speed STLPB-01
}//updateState end
/*
* stopMotor()
* @brief turns the motor OFF
* @param none
* @return no return
* Written by Ahmad Shamshiri for robojax.com
* on May 02, 2020 in Ajax, Ontario, Canada
*/
void stopMotor()
{
//Robojax.com L298N Stepper Speed STLPB-01
if(stopType ==0)
{
for(int i=0; i<4; i++)
{
digitalWrite(motorPin[i], LOW);
}
}
//Robojax.com L298N Stepper Speed STLPB-01
}//stopMotor() end
文件📁
没有可用的文件。