Looking for help on a stepper sketch

I have got this sketch together, I’m trying to learn the language. I want it to control a stepper to go in with one pin going high, and out with a different pin going high. It seems to just skip over the button conditions. As it runs, the stepper just goes in and out with no response from button presses. What I’m I missing here. I tried reading as much as I could find, but I couldn’t find something similar enough to make sense of it. Any help is much appreciated! I’m loving the community and love how fun exploring the possibilities of this great project.

My Sketch:

#include <Stepper.h>

const int stepsPerRevolution = 392; // Set number of steps:

// Set pin numbers for digital outputs:
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);

void setup() {
// set the speed at 40 to 80 rpm (safe speeds are 40 to around 80 rpm):
myStepper.setSpeed(40);
// initialize the serial port:
Serial.begin(9600);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);

}

void loop() {
int sensorVal1 = digitalRead(7);
Serial.println(sensorVal1);
if (sensorVal1 == HIGH);
Serial.println(“clockwise”);
myStepper.step(stepsPerRevolution);
int sensorVal2 = digitalRead(8);
Serial.println(sensorVal2);
if (sensorVal2 == HIGH);
Serial.println(“counterclockwise”);
myStepper.step(-stepsPerRevolution);

}

If statements should not have a semicolon.

If you want the if to control more than one statement, you will need some of these { }

Please post a hand drawn wiring diagram and links to the motor, motor driver and motor power supply.

Please use codetags when you post a Sketch. You can select the whole Code ( or nothing!) in the IDE, rightclick into the edit window and select ‘Copy for Forum’. Than you can paste it into your post and the code tags are inserted automatically.

The code lines you want to be dependent from your if statement must be enclosed in {} and follow the if statement without a semikolon in between. As it is now, all statements are executed always, and that’s why your stepper runs always.

You use ‘INPUT_PULLUP’. This is normaly done if a switch is connected between the pin and Gnd ( You don’t need an additional resistor in this case ). But the pin is read LOW if the button is pressed. Therefor you must check for LOW in the if statement if you want to recognize when the button is pressed.

Robin2’s simple stepper program tutorial shows how to control a stepper with 2 button switches. May be helpful.

As @wildbill says
missing { }
should be

if (sensorVal2 == HIGH) {
  Serial.println(“counterclockwise”);
  myStepper.step(-stepsPerRevolution);
}

Also suggest you move to AccelStepper and a non-blocking task based program.
See my tutorial on Multi-tasking in Arduino which has a complete Stepper control example.
For button inputs see my Debouncing Switches in Arduino
multitaskingDiagramSmall

Wow, thanks so much for all the quick replies! I will follow you all’s instructions and let you know if I have it solved. Thanks so much, sorry for not copying it correctly!

So I’m still kinda confused. I’m trying to use a L298N stepper driver wired to digital pins 3,4,5,6. A switch that is a dual momentary (up, down) normally open, center pin to ground . And I don’t know how to apply the examples to fit my driver…

The L298 is a poor brushed DC motor driver, and even a worse stepper motor driver.
Your stepper motor might not be compatible with this dinosaur.
Post a link to your stepper motor.
Leo…

1 Like

Hi, @bimmerguy
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

To add code please click this link;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What is your stepper, can you post a link to specs/data please?
What model Arduino are you using?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

#include <DebouncedSwitch.h>
#include <Stepper.h>

const int stepsPerRevolution = 370;  // change this to fit the number of steps per revolution
int SCW = 8;
int SCCW = 9;
int stepCount = 0;
DebouncedSwitch sw1(SCW);
DebouncedSwitch sw2(SCCW);
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  pinMode(SCW, INPUT);
  pinMode(SCCW, INPUT);
  myStepper.setSpeed(400);
  Serial.begin(9600);  
}


void loop() {
  // step one step:
  sw1.update();
  sw2.update();
  if (sw1.isDown()) {
    myStepper.step(20);
  }
  if (sw2.isDown()) {
    myStepper.step(-20);
  } else {
    myStepper.step(0);
  }
}

This is code I was just trying but doesn’t seem to work. I am using an UNO to the L298N, it drives it just fine using the “Stepper-OneRevolution” example. I can make it go in and out, but am having trouble with using switches to control it’s in and out steps.
20210502_105530|690x388

Your diagram shows the stepper on pins 3,4,5,6. Your code has a different opinion :thinking:

Sorry, I changed the pins in the code. So the diagram is correct. I copied the code from when I first put it together. But the pins for the stepper is the only change made.

Please post your latest code.

#include <DebouncedSwitch.h>
#include <Stepper.h>

const int stepsPerPush = 20;  // change this to fit the number of steps per revolution
int SCW = 8;
int SCCW = 9;
int stepCount = 0;
DebouncedSwitch sw1(SCW);
DebouncedSwitch sw2(SCCW);
Stepper myStepper(stepsPerPush, 3, 4, 5, 6);
void setup() {
  pinMode(SCW, INPUT);
  pinMode(SCCW, INPUT);
  myStepper.setSpeed(400);
  Serial.begin(9600);  
}

void loop() {
  // step one step:
  {
  sw1.update();
  sw2.update();
  if (sw1.isDown());
    myStepper.step(20);
  }
{
  sw1.update();
  sw2.update();
  if (sw2.isDown());
    myStepper.step(-20);
  }
}

Ig it still isn’t working, I suggest that you comment out the stepper bits and replace them with some serial.print messages to verify that your switches are doing what you expect.

@wildbill - Thank you! After debugging my switch problems. I ended up figuring it out. So, thanks to everyone! I’m starting to learn a lot more from it, trying different things out. This is what ended up working for me…

#include <Stepper.h>
const int stepsPerPush = 20;
const int sw1 = 9;
const int sw2 = 10;
int sw1State = 0;
int sw2State =0;
int lastsw1State = 0;
int lastsw2State = 0;
// initialize the stepper library on pins 3 through 6:
Stepper myStepper(stepsPerPush, 3, 4, 5, 6);

void setup() {
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  // set the speed at 500 rpm:
  myStepper.setSpeed(500);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  sw1State = digitalRead(sw1);
  if (sw1State != lastsw1State) {
    if (sw1State == HIGH) {
      Serial.println("CW");
      myStepper.step(stepsPerPush);
    } else {
      Serial.println("No Change");
    }
 }
  sw2State = digitalRead(sw2);
  if (sw2State != lastsw2State) {
    if (sw2State == HIGH) {
      Serial.println("CCW");
      myStepper.step(-stepsPerPush);
    } else {
      Serial.println("No Change");
    }
   }
  }