If statement not being executed

Hi there,

Currently using a Nano.

I am trying to use a button (a step pad from Spirit Halloween) to move a step motor (28BYJ-48) back and forth 3 times then stop until the button is pushed again. So far I have managed to make the step pad work (using the pin13 LED as an indicator) and have managed to make the motor move but can't seem to get the code to work together.

I have tried reading and troubleshooting by myself to no avail. In order to properly see what state the Nano believes its in I added an "else" to turn on the pin13 LED when the pad is not pressed. When running the program I simply get the LED constantly on whether the button is pressed or not.

This is my current code:

#include <Stepper.h>

const int stepsPerRevolution = 2038;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
  (buttonState = digitalRead(buttonPin));

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
   // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);
  
  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);

  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);
  }
   else {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
}

If its not something obvious in the code itself I will happily post a schematic of how I have everything wired. And yes, I am aware I have not added anything to stop the motor once the sequence is complete, I'll get there one day.

yes post the schematics

That turns it on. Where do you turn it off?

a7

My guess is that your button pin is floating when not pressed, and as @alto777 mentioned you never turn the LED off so once on it stays that way.

Regarding the button pin... you need to ensure that it is always connected to VCC or GND. This is done with pull-up/pull-down resistors. The easiest way is to use the Arduino's internal pull-up resistors.

If you define the pin as

  pinMode(buttonPin, INPUT_PULLUP);

Then connect the other side of the button to GND... and to check it's pressed you look for...

  if (buttonState == LOW) 

Killzone_kid, I'm currently working on drawing something up.

a7, I feel real dumb. I added digitalWrite(ledPin, LOW); as the first line of the "if" statement. It does seem the button is working but I'm still not getting motor movement. I can feel a slight vibration of the motor trying to do anything but am confused why it wouldn't be able to move when the switch triggers it.

Sounds like you have a power supply issue...

Yeah, no one ever made a stupid mistake like that! :expressionless:

I changed it to INPUT_PULLUP, which usually never hurts. IRL I use a real pullup resistor, so sue me.

As @red_car Sounds like you have a power supply issue...

so we need to see a schematic diagram. HAnd drawn, photo and post is really the fastest. Include all componnets and how they are powered.

a7

Yeah, thats kind of what I thought. Not sure why the voltage would drop off though. I'm going to run it off of a different DC power source and drive it through ULN2003AN but I took that out of line in order to simplify when troubleshooting.

Oof. That’s an attempt. I KNOW I KNOW I should not be powering the step motor off of the usb port on the nano. I’m quite sure going through the motor driver board with external power source will fix this but that’s a problem for the morning.

Thank you all for being incredibly helpful

1 Like

This can’t be right, you are shorting power supply

You need to move the resistor to the spot shown, and 10k is better.

when your button (steppad) is pressed, that's what happens

5V and GND gets connected....

Or change it so the switch is wired to ground, and the pin is INPUT_PULLUP or use the resistor (10K) to make the pin read HIGH when the button is not pressed.

Very more common. Invert the switch logic at some point.

a7

Okay, sorry ya’ll. You were all very right and my drawing was wrong. I have been using breadboards so staring at them long enough has led me to make some mistakes.

The switch/LED seem to be working just fine. Now when I add the motor driver in it’s like the arduino doesn’t want to talk to it. Here’s an updated drawing with the motor driver inline.

It’s not super legible but on the driver pins IN1-IN4 are connected to the arduino’s pins D8-D11 in that order. Also the 5 wires and connector on the motor are connected. There is only one way to insert them.

I’ve metered and I seem to be 5v at the source and going into motor driver than a fluctuating voltage around 2.5 v going between the ground and each of the 4 legs. I also checked to make sure that the reading was in fact the same between the legs and the ground on the arduino and everything was consistent. The motor itself simply got hot so I disconnected things in order to not fry the motor. I’ve tried a different motor and different driver but same issue. I also plugged the original motor directly into the arduino and it still works just fine.

Also for troubleshooting purposes I’m running the basic stepper_one revolution code and as it works plugged directly into the arduino I now don’t think the coding is the problem (I know, wrong forum).

Any thoughts? Sorry again to be a pain but thanks for your patience with me.

You are still shorting 5v and ground on your new drawing

Okay I went back and found a drawing or the setup on a breadboard and did that. It now looks like this and the button functions:

ANYWAY I'm still at a dead end with my motor but I will take that question elsewhere so I'm not continually bugging you folks. Thanks for helping me learn!

Less than forty percent of the people who will read this thread have started in the last 14 hours.

Anyway, are you saying you have code, which will run some original motor, just fine?

That is the code and schematic you should test with and share here.

And if you are trying to combine two working ideas, post the code of the other part and describe its hardware and schematic.

Or not. :wink:

a7

Okay! So the code I wrote originally at the beginning of the post seems to be working in that I can feel the motor try to move 6 times before stopping when the button is pressed, but only if it’s attached directly to D7-D11 (though only D8-D11 are at work in the code.

Also I can see that the LED in pin 13 stays on for the duration of the motor move then turns off.

As said before the thought was that there wasn’t enough power so I am trying to drive it through a motor driver (ULN2003AN) with a 28BYJ-48 step motor. I have it powered through a 5v dc power supply. The motor drivers LEDs turn on and stay constant and the motor itself just gets hot (I have been pulling it out before it’s supernova but I do have a spare motor and spare motor driver that are exactly the same thing).

The behavior is consistent across the primary and the spare board and the primary and the spare motor leading me to believe it’s not the physical hardware.

I've taken the wiring directly from this:


Which came from 28BYJ-48 Stepper Motor with ULN2003 + Arduino (4 Examples)

This is an image of what I have right now and I am positive the wiring is consistent with the original schematic.





As I said I'm using the same code (with the added LED off) but I'm posting it here just so its all in one place.

#include <Stepper.h>

const int stepsPerRevolution = 2038;  // 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);

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
   digitalWrite(ledPin, HIGH);
   // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(500);
  delay(750);
  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(500);
  delay(750);
  }
   else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

I swear I'm trying to troubleshoot on my own but I've just hit a wall.

Seems very fast for a 28BYJ-48 stepper. What happens if you move much slower?

I just moved the speed down to 10 and still nothing