Stepper isn't rotating + debouncing method [CLOSED]

Hi!
Any idea why this stepper (28BYJ-48) is not rotating?
I used a debouncing method (still improving this method) to make a push button act like a ON/OFF button, but when it's ON the motor doesn't react at all. Also I want to control the speed of this motor and display this on a LCD. It is just a start project.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#include <Stepper.h>
const int stepsPerRevolution = 200;  // 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, 8, 9, 10, 11);
int stepCount = 0;  // number of steps the motor has taken

unsigned long previousMillis = 0;
int M1 = 3; // On/Off
int flag = 0;

void setup() {
  pinMode(M1, INPUT);
  Serial.begin(9600);
  lcd.init();
  lcd.clear();
  lcd.backlight();
} // setup *****************************************************************
void loop() {
  uint32_t currentMillis = millis();  // Millis times uses to debounce the button
  const uint32_t BOUNCETIMEOUT = 20;  // Debounce time in milliseconds

#define buttonPressed LOW       // When the button is pressed the input will be low, this is to remove the confusion this migth cause.
  bool currentButtonState = digitalRead(M1);     // Reads the current state of the button and saves the result in a bool
  static bool lastButtonState;
  static uint32_t lastMillis;         // Start of the debounce timeout

  if ((lastButtonState != currentButtonState)) { // Checks to see if the button has been pressed or released, at this point the button has not been debounced

    if (currentMillis - lastMillis >= BOUNCETIMEOUT) {  // Checks to see if the state of the button has been stable for at least bounceTimeout duration
      lastButtonState = currentButtonState;
      // check if you press the SET button and increase the menu index
      if (currentButtonState == buttonPressed)
      {
        if (flag == 0) {           // and the status flag is LOW
          flag = 1;
          while (flag == 0) {
            int sensorReading = analogRead(A1); // here is a potentiometer 
            // map it to a range from 0 to 100:
            int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
            // set the motor speed:
            if (motorSpeed > 0) {
              myStepper.setSpeed(motorSpeed);
              // step 1/100 of a revolution:
              myStepper.step(stepsPerRevolution / 100);
              lcd.setCursor(0, 1);
              lcd.print(motorSpeed);
            }
          }
          Serial.println("ON");
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("ON");
        }
        else {
          flag = 0;
          Serial.println("OFF");
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("OFF");
        }
      }
    }
  } // if

  else {
    lastMillis = currentMillis;    // Saves the current value of millis in last millis so the debounce timer starts from current millis
  }
} // loop ******************************************************

Just looking through the peephole here, and

that right there is a recipe for nothing happening in the body of the while statement.

But more important, you should separate button handling from motor running.

Having that while hogging the processor inside your denouncing logic is only going to get more and more tangled and hard to add to.

Divide and rule. Divide and conquer.

Set a "flag" variable in your button logic. Use the value of the flag to run or not run the motor in another section of code.

a7

consider

#undef MyHW
#ifdef MyHW
int M1 = A1;

#else
int M1 = 3; // On/Off
#endif

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>

LiquidCrystal_I2C lcd (0x27, 16, 2);

int       stepCount;
const int StepsPerRevolution = 200;
Stepper   myStepper (StepsPerRevolution, 8, 9, 10, 11);

bool butState;

// -----------------------------------------------------------------------------
void setup () {
    pinMode (M1, INPUT_PULLUP);
    butState = digitalRead (M1);
    Serial.begin (9600);

    lcd.init ();
    lcd.clear ();
    lcd.backlight ();
}

// -----------------------------------------------------------------------------
void loop () {
    int sensorReading = analogRead (A1); // here is a potentiometer
    int motorSpeed    = map (sensorReading, 0, 1023, 0, 100);
    myStepper.setSpeed (motorSpeed);

    bool but = digitalRead (M1);
    if ((butState != but)) { // button state change
        butState = but;
        delay (10);         // debounce

        if (LOW == but) {   // pressed
            myStepper.step (StepsPerRevolution / 100);
            stepCount++;
            Serial.println (stepCount);

            lcd.clear ();
            lcd.setCursor (0, 0);
            lcd.print (stepCount);
        }
    }
}

Hi,
Consider writing some code that JUST controls the stepper before adding LCD and other stuff.

Have you tried the stepper library examples to get your hardware sorted and running?

Do you have a DMM?

Can you please post a circuit diagram, not a Fritzy, not a cut and paste from another site, a hand drawn image will be fine of YOUR project, that is reverse engineer your hardware?
Please include power supplies, component names and pin labels.

An image(s) of your project will help.

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

I know that I have a lot of programming clumsiness.
Somehow I got the desired result, setting a flag as you suggest, but for another case, using hardware interrupts on a button that is for On/Off. In the On state, I wanted to be able to adjust the rotation speed and display something on an LCD.
In the case of this stepper, I have to rethink things. I'll be back,

Well, I am following the examples from Arduino:
https://docs.arduino.cc/learn/electronics/stepper-motors
And the circuit for the stepper would be the one below, the difference is that I am using a Nano board.
IN4->D11
IN3->D10
IN2->D9
IN1->D8

Also, I have a push button connected to D3 and a potentiometer connected to A1.
I will try something else.

Hi,

Show your switch and pot.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Try
IN4->D11
IN3->D9
IN2->D10
IN1->D8

Or change this line:

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

to

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

The point of the stepper motor driver is to allow the Arduino to control it and get power from a separate supply.

You appear to be using your Nano for power which would explain why it isn’t working.

I am using a separate 5V power supply for the stepper.

In your diagram, you do not. It is not fair to us, to post incorrect or incomplete diagrams. Please update and post an accurate diagram.

I strongly urge you to post a "real" schematic as requested in post #4. It will remove the temptation to take short cuts like sketching in a power line that's not correct. Also leaving out a pile of devices...

We shouldn't have to comb through your posts and mentally annotate your diagram to arrive at a correct view.

I am sorry for this inconvenience about the schematic.
The stepper is missing, but it is connected as follows:
IN4->D11
IN3->D10
IN2->D9
IN1->D8

I didn't have time to work on the program, but I'll try something else tomorrow.

Then please add it. This is becoming tiresome. Otherwise, the schematic is a welcome improvement.

I don't believe you can normally run I2C from D4,D5. Those are not I2C capable. Also, Vin should never be connected to a 5V power source. The minimum for that is absolute 7V, nominal 9V.

Power supplies are missing from the diagram. Requested earlier on.

Referring to the power supply, I use a single 5V power supply for everything. So I don't use the regulator on the Arduino as a power supply. Okay, so I should use a 9V supply for the Vin pin.
As for the circuit, in the reply above I posted it and the stepper is connected as I mentioned (ULN2003 is the driver) and as examples I follow these: https://docs.arduino.cc/learn/electronics/stepper-motors .
In these examples I do not see references to I2C.

The I2C is how your LCD display is connected. But, in the diagram, you connected the display to data pins, not I2C pins A4, A5.

Please re-read reply #11.

I think you have realized that regarding LCD I2C it is just an unwanted mistake, which has no effect on the programming problem.

Hi,
Have you been able to get the stepper moving at all?
Have you a basic bit of code that JUST rotates the stepper, NOTHING else in the code?
Forget the LCD for the moment.
Use serial prints to help debug.

What I am getting at is have you developed your code in stages and got each stage working before going onto the next.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:
Have you looked at the stepper libraries available and ther example codes that they provide.

I found another source of information that describes the 28BYJ-48 stepper in a good way.

I can get to an application that allows me to control or set a desired angle. I will put this stepper into operation again when I have more free time. Until then, I'm still thinking about what I would like this stepper to do.

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