Motor Stepper With Push Button
Motor Stepper With Push Button
*
* 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 speed of Stepper Motor using L298N
Module
* with 3 push buttons to slow it down, speed it up, start or stop the motor
* Direction of rotation is set in code
* Video code: STLPB-01 on YouTube
*
* Watch video for details: https://youtu.be/csf_G1pi0ps
*
If you found this tutorial helpful, please support me so I can continue creating
make 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 download 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.
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>
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()
{
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 themotor 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