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");
}
}
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.
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.
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
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..