// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// WOKWI: A bipolar stepper motor
// The stepper motor moves 1.8 degrees per step (200 steps per revolution).
// The motor also supports half-stepping (0.9 degrees per step / 400 steps per revolution).
// Pin names
// Name Description
// A- Coil A negative signal
// A+ Coil A positive signal
// B+ Coil B positive signal
// B- Coil B negative signal
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library: give a name, define revolutions and pins, on pins 8 through 11: 8=IN4, 10=IN2, 9=IN3, 11=IN1;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Stepper myStepper(stepsPerRevolution, B-, B+, A+, A-); (IN4, IN2, IN3, IN1)
int buttonPin = 13 ; // Pin 13 ist digital input pin for button
bool buttonPressed; // status buttonPressed is boolean (true/false)
void setup()
{ Serial.begin(9600); // initialize the serial port:
myStepper.setSpeed(60); // set the speed at 60 rpm:
pinMode(buttonPin, INPUT_PULLUP); // button pin is normally pulled up to 5V (high, true), goes to GND (low, false) if pressed
buttonPressed = true; // 'true' means button is not pressed, so 5V (high, true)
}
void loop()
{ /*
Serial.println("clockwise"); // step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(500);
Serial.println("counterclockwise"); // step one revolution in the other direction:
myStepper.step(-stepsPerRevolution);
delay(500);
*/
buttonPressed = digitalRead(buttonPin); // is the button pressed? low/false=pressed (GND), high/true=not pressed (5V)
if (buttonPressed == false) // false = button pressed to GND (low)
{
myStepper.step(stepsPerRevolution / 8); // make 1/8 revolutions
delay(50);
buttonPressed = true;
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*========================================================================
Stepper motor learning on UNO, driver chip ULN3003AN, motor28BYJ-48
Aug. 2023
added: emmgee: Button on pin 13 to GND. If pressed: 1/8 revolution
========================================================================
/*
www.elegoo.com 2018.10.25
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
*/
/*
#include <Stepper.h> // standard library for stepper motors
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution. 2048 equals 360 degrees
const int rolePerMinute = 10; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm (default 15)
// initialize the stepper library: give a name, define revolutions and pins, on pins 8 through 11: 8=IN4, 10=IN2, 9=IN3, 11=IN1;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // Stepper myStepper(stepsPerRevolution, IN4, IN2, IN3, IN1); (B-, B+, A+, A-)
int buttonPin = 13; // Pin 13 ist digital input pin for button
bool buttonPressed; // status buttonPressed is boolean (true/false)
void setup()
{
Serial.begin(115200); // initialize the serial port, Serial.begin(9600);
myStepper.setSpeed(rolePerMinute); // or just: myStepper.setSpeed(10);
pinMode(buttonPin, INPUT_PULLUP); // button pin is normally pulled up to 5V (high, true), goes to GND (low, false) if pressed
buttonPressed = true; // 'true' means button is not pressed, so 5V (high, true)
}
void loop()
{
//
Serial.println("clockwise"); // step one revolution in one direction (positive):
myStepper.step(stepsPerRevolution); // make a full revolution with 2048 steps. Or: myStepper.step(stepsPerRevolution/4);
delay(500);
Serial.println("counterclockwise"); // step one revolution in the other (negative):
myStepper.step(-stepsPerRevolution); // make a full revolution in other (-) direction.
delay(500);
//
buttonPressed = digitalRead(buttonPin); // is the button pressed? low/false=pressed (GND), high/true=not pressed (5V)
if (buttonPressed == false) // false = button pressed to GND (low)
{
myStepper.step(stepsPerRevolution / 8); // make 1/8 revolutions
delay(50);
buttonPressed = true;
}
}
*/