Stepper Library Initialization Breaks Code

I am using an Arduino Uno with two buttons and a stepper motor; when the program is run without the stepper motor code it can detect the on/off states, but once the Stepper object is created, the entire program doesn't work anymore.

I have tried re-installing and changing the library version to no avail. The code did previously work but suddenly stopped, with it randomly working again.

Line of concern:

Stepper red(STEPS, 2, 5, 3, 6);
#include <Stepper.h>
#define STEPS 4096

Stepper red(STEPS, 2, 5, 3, 6); // Adding this breaks the code, the Serial Monitor no longer outputs anything. Removing it allows for the program to function as intended.

const int openPin = A5;
const int closePin = A3;
int openState = 0;
int closeState = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(openPin, INPUT_PULLUP);
   pinMode(closePin, INPUT_PULLUP);
}

void loop() {
  openState = digitalRead(openPin);
    closeState = digitalRead(closePin);
  if(openState == LOW) {
      Serial.print("OPEN");

    }
    if (closeState == LOW) {
      Serial.print("CLOSE");

    }
}
1 Like

That conveys no useful information. What does "not work" mean? Are there errors and/or warnings grnerated? Do you have compiler warnings enabled in the File, Preferences menu? Is any thing printed in serial monitor?

Nothing is printed in the Serial Monitor, no warnings in the compiler (they are enabled). The code works as expected without that one line, pressing the 2 different buttons prints "OPEN" or "CLOSE". It's just when that one line is added to add motor functionality that the code does absolutely nothing.

I anything connected to pins 2,3,5,6 ? Please show a schematic.
Apart from setting some internal variables the only thing this line does ist setting these pins to OUTPUT.
In my setup it works perfectly with this line included.

Here is the actual picture of the wiring. There is a schematic below (2nd reply) that may show a better picture.

The schematic is almost the same; I am using a 4-pin stepper motor, whereas the schematic shows a DC motor.

I don't like Fritzings ...
What did you connect to Vin? and how do you power your UNO?
Does is run if you disconnect these 4 pins from the UNO?

The UNO is powered by both the USB connector and the barrel jack adapter— the VIN connects to the power for the stepper motor. Disconnecting the 4 pins yields no result.

Mine, too.

Could you send your code and setup for me to attempt to replicate myself? Thanks.

You could try connecting the the stepper IN1 to IN4 to the pins set in the code or set the code to the pins you are actually using, I ALWAYS use digital pins for my 9 steppers

mick in glen innes 2370

Not sure how this relates to Interfacing w/ Software on the Computer and hence our topic has been moved to a more suitable location on the forum.

Powering the Uno from USB and drawing motor power from the V-in pin draws current backwards through the 5volt regulator. It will fail, if it already hasn't done so.
Connect motor power to the 5volt pin (assuming it's a 5volt 28BYJ-48).
Leo..

1 Like

That is not apparent on the photo.

Just don't use the "Barrel jack" or "Vin" for any actual project (that is, which involves connecting something to the Arduino other than the PC).

You do have the 5 V version of the stepper? (There are 5 V and 12 V versions.)

1 Like

It's exactly your code and a Arduino Nano with no connections to the stepper pins, powerd only by USB.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.