The code provided by @greendragon includes returning from the Arduino C++ loop but to where? Apparently back to itself according to what I found online. And apparently an exit() in the loop stops everything.
It's just a return statement, not a call to the exit function It performs an early return from the loop function. Calling exit would be an interesting test! Probably just a reset.
@greendragon, another interesting test would be to remove the Arduino completely and just test the Adafruit board. Hold the STEP pin high and see the result.
Also, what do the board lights indicate?
The one who has the most fun, wins!
(Prefix: I'm posting this before I finished writing it because it was taking too much time. Apologies for any omissions.)
A Mess of Tests, A Pile of Results
I came up with 5 tests. Since I don't have the Adafruit driver board to test with, I don't feel the tests are conclusive. However, I think the results do indicate the validity of the sketch algorithms, excluding the support library.
Since the Adafruit driver board is marketed as "All you need is two output pins, no timers, PWM or real-time microcontroller" , I strongly suspect people would notice and complain if you had to use the stepper library or the sketch didn't work. I suspect a circuit error in the cause.
Component Required:
- (1) x Elegoo Uno R3
- (1) x 830 tie-points breadboard
- (1) x ULN2003 stepper motor driver module
- (1) x Stepper motor
- (1) x 9V1A Adapter
- (1) x Power supply module
- (6) x F-M wires (Female to Male DuPont wires)
- (1) x M-M wire (Male to Male jumper wire)
Elegoo User Manual Wiring diagram
To keep the file count down I combined all the tests into a single module and separated them with namespaces.
Test 1: Elegoo
Elegoo stepper motor example sketch.
Verifies working components.
namespace Elegoo {
//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.
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 15; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper myStepper(stepsPerRevolution, 9, 11, 8, 10);
void setup() {
myStepper.setSpeed(rolePerMinute);
// initialize the serial port:
Serial.begin(9600);
Serial.println("Elegoo example");
Serial.print("Stepper version: ");
Serial.println(myStepper.version());
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
}
Test 2: Adafruit
Adafruit sample using the Elegoo stepper motor and Arduino library
Verifies Adafruit sketch behavior
namespace Adafruit {
// microstep mode, default is 1/8 so 8;
// ex: 1/16 would be 16
const int microMode = 8;
// full rotation * microstep divider
const int steps = 256 * microMode;
bool CWDIR{true};
// Adjustable range of 28BYJ-48 stepper
// is 0~17 rpm
const int RPM{ 15 };
Stepper myStepper(steps, 9, 11, 8, 10);
void setup()
{
Serial.begin(9600);
Serial.println("Adafruit example");
Serial.print("Stepper version: ");
Serial.println(myStepper.version());
myStepper.setSpeed(RPM);
}
void loop()
{
// change direction every loop
CWDIR = !CWDIR;
for (int x = 0; x < steps; x++) {
myStepper.step((CWDIR ? 1 : -1));
//delay(2);
}
delay(1000); // 1 second delay
}
}
Test 3: Stepper_oneRevolution
GreenDragon's sketc (aka, Arduino Stepper motor example)
Test sketch algorithm
namespace Stepper_oneRevolution {
const int stepsPerRevolution = 6400; // 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, 10, 11);
//Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper myStepper(stepsPerRevolution, 9, 11, 8, 10);
boolean running = true; // Global variable
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
Serial.println("Dragon 0.1 example");
Serial.print("Stepper version: ");
Serial.println(myStepper.version());
}
int i = 0;
void loop() {
if (!running) {
return; // Exit loop() if running is false
}
// Your main loop logic here
myStepper.step(-stepsPerRevolution);
delay(2);
i++;
Serial.println(i);
// Example condition to stop the loop
if (i == 5) {
running = false;
}
}
} // namespace Stepper_oneRevolution
Test 4: Dragon 0.2
Modified GreenSragon sketch. Alters parameters and adds print statments
Verifies working behavior
namespace Dragon_0_2 {
// adjust print
// increase delay
// adjust steps/rev
// adjust speed (rpm)
const unsigned long DELAYMS{ 25UL };
const long RPM{ 15L };
// change this to fit the number of steps
// per revolution for your motor
//const int stepsPerRevolution = 6400;
const int stepsPerRevolution{ 2048 };
// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 10, 11);
//Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper myStepper(stepsPerRevolution, 9, 11, 8, 10);
boolean running{ true };
void setup() {
myStepper.setSpeed(RPM);
// initialize the serial port:
Serial.begin(9600);
Serial.println("Dragon 0.2 example");
Serial.print("Stepper version: ");
Serial.println(myStepper.version());
}
int i{0};
void loop() {
if (!running) {
return; // Exit loop() if running is false
}
Serial.println(i);
// Your main loop logic here
myStepper.step(-stepsPerRevolution);
delay(DELAYMS);
i++;
// Example condition to stop the loop
if (i == 5) {
running = false;
}
}
} // namespace Dragon_0_2
Test 5: Dragon 0.3
Degenerate skecth that calls exit.
What happens if call exit?
namespace Dragon_0_3 {
// call exit
const unsigned long DELAYMS{ 25UL };
const long RPM{ 15L };
// change this to fit the number of steps
// per revolution for your motor
//const int stepsPerRevolution = 6400;
const int stepsPerRevolution{ 2048 };
// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 10, 11);
//Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper myStepper(stepsPerRevolution, 9, 11, 8, 10);
boolean running{ true };
void setup() {
myStepper.setSpeed(RPM);
// initialize the serial port:
Serial.begin(9600);
Serial.println("Dragon 0.3 example");
Serial.print("Stepper version: ");
Serial.println(myStepper.version());
}
int i{0};
void loop() {
if (!running) {
//return; // Exit loop() if running is false
exit(1);
}
Serial.println(i);
// Your main loop logic here
myStepper.step(-stepsPerRevolution);
delay(DELAYMS);
i++;
// Example condition to stop the loop
if (i == 5) {
running = false;
}
}
} // namespace Dragon_0_3
The one who has the most fun, wins!
I contacted Adafruit this morning and the following is for them -
The Stepper.h library does support 2-pin 'step & direction' drivers. But it is a rather basic library. The AccelStepper library would be a better way to go.
Below is a very basic code just to test and it works. Even that 1/2 revolution problen is gone.
#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 10, 11);
void setup()
{
stepper1.setMaxSpeed(300.0);
stepper1.setAcceleration(100.0);
stepper1.moveTo(1000000);
}
void loop()
{
//
stepper1.run();
}
I would like to say thank you to everyone in the forum who tried to help me.
😀 😀 😀 😀 😀
I contacted Adafruit this morning and the following is for them
Did they say anything about their sketch not working?
The one who has the most fun, wins!
My Goal:
A script that will allow me to do the following -
1. choose the number of full rotations of a wheel and then the stepper stops
example 1 = 1 full rotation, 2 = 2 full rotations, 3 = 3 full rotations
2. control forwards or backwards
3. control the speed of the wheel
I am using a nema 17 motor 8HS11-0204S ( 200 steps for a full revolution - I think)
Below is the code I have so far and it does not work! Note I am new to coding so if you could just keep it simple it would be greatly appreciated. Thanks
// defines pins const int dirPin = 10; const int stepPin = 11; int numberOfSteps = 200; int millisbetweenSteps = 250; // milliseconds - or try 1000 for slower steps void setup() { pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); } void loop() { digitalWrite(dirPin, LOW); for (int n = 0; n < numberOfSteps; n++) { digitalWrite(stepPin, HIGH); delay(2); digitalWrite(stepPin, LOW); delay(millisbetweenSteps); } }
You can edit your code in the following way and try.
// Pin definitions
const int dirPin = 10;
const int stepPin = 11;
// User settings
int rotations = 3; // Set how many full rotations
bool directionForward = true; // true = forward, false = backward
int stepDelay = 5; // Delay between steps in milliseconds (smaller = faster)
// Constants
const int stepsPerRevolution = 200;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
// Set direction
digitalWrite(dirPin, directionForward ? HIGH : LOW);
// Calculate total number of steps
int totalSteps = rotations * stepsPerRevolution;
// Perform the steps
for (int i = 0; i < totalSteps; i++) {
digitalWrite(stepPin, HIGH);
delay(stepDelay);
digitalWrite(stepPin, LOW);
delay(stepDelay);
}
// Stop after one run
while (true); // Do nothing forever
}
void loop() {
// Nothing here — everything happens once in setup()
}
