Stepper motor help

Hello everyone, I am new at this. I made a code for a stepper motor for a project. I need to control it x revolutions to one direction then push the button and make it go x revolutions to the oposite direction. this is because I designed a furniture that holds a tv that you can use it from both sides of the furniture, therefore the direction of rotation always must be the same, left to right then right to left, so the tv wires wont twist.

this is the code I wrote, the thing is that its unreliable, some times goes right to left, then left to right, but many times makes the rotation to the same direction twice or more times or goes totaly random.

I read somewhere to use resistors, I did trie to put a resistor into the button but it didn´t work. and it has to work with only one push button so I can take this to iot cloud and make it work from a cellphone.

somebody can help me?.

this is my code

#include <Stepper.h>
int stepsPerRevolution=2048;
int motSpeed=10;
int dt=500;
Stepper myStepper(stepsPerRevolution, 8,10,9,11);

int buttonPin=2;
int motDir=1;
int buttonValNew;
int buttonValOld=1;



void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
pinMode(buttonPin,INPUT);
digitalWrite(buttonPin,HIGH);
}

void loop() {  
buttonValNew=digitalRead(buttonPin);
if (buttonValOld==1 && buttonValNew==0){
  motDir=motDir*(1);
  myStepper.step(stepsPerRevolution);  
  }
delay;
buttonValNew=digitalRead(buttonPin);
if (buttonValOld==1 && buttonValNew==0){
  motDir=motDir*(-1);
  myStepper.step(-stepsPerRevolution);  
  }
delay;
  }

Please post a little schematics. Pen and paper is fine. How that button is connected raises questions.
The powering of the stepper is interesting to check.

Please post the actual code you are using. What are the lines that just say delay; supposed to do. Don't you usually put parenthesis and a number after delay to tell it how long?

Be sure to set the value of buttonValOld somewhere.

@arqjuanmrossi - I suspect this was to be the delay time mentioned above ( @Delta_G )... try replacing delay; with delay(dt);

thanks to everyone, the button is one pin connected to pin 2 and the other to ground, I made this code based on pauls Mcwhorter schematics for a stepper then I copied the lines for the buttons. As I said I am just a begginer with this, Its been just a month since I started learning things. the delay I put it so it would stop, I don´t know if its really needed.
the thing is if someone could help me to fix it so it can go one turn and one turn back, it just needs to go one or half turn since its te rotation needed for the tv.

Then this:

Should be:
pinMode(buttonPin, INPUT_PULLUP);

and you should expect the button to read LOW when pressed and HIGH when not pressed.

Ok, but aren't you supposed to put some parenthesis behind it with a value? Don't just plop random things into your code. That's how you get into a mess you can't get out of. At least look at an example of the function you want to use and see how to use it.

1 Like

I made a schematics, but don´t know how to upload it to the forum, I sent it to you trought the mail that I recived from arduino forum.

thats the actual code, I put the delay thinking it would stop the stepper between the lines.

that may have been a mistake, or something else, because i started with pauls mcwhorter code for stepper where it has a delay time from right to left I guess...
what I want to do is to make the stepper go one way, then push the button and make it go back , and so on. the goal would be to be able to make this work with the iot cloud server and operate it from the phone app. so you can turn the tv arround from anywhere without getting up or pushing an actual button on the furniture.
if someone of you knows how to do that coding please help me because I know very little at this time.... I can turn on and off relays and see temperature sensors from the iot but I don´t have a clue how to make it work with the stepper...

thanks to everyone!!

Why don't you address some of the help you have received? Like providing a time for the delay. Or setting the pinMode correctly.

I tried some of the things that everyone told me including your advise.
now I got it working but the second time I pressed the button it went crazy.

this is the code I am using now.


#include <Stepper.h>
int stepsPerRevolution=2048;
int motSpeed=10;
int dt=500;
Stepper myStepper(stepsPerRevolution, 8,10,9,11);

int buttonPin=2;
int motDir=1;
int buttonValNew;
int buttonValOld=1;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin,HIGH);
}

void loop() {
buttonValNew=digitalRead(buttonPin);
if (buttonValOld==1 && buttonValNew==0){
motDir=motDir*(1);
myStepper.step(stepsPerRevolution);
}
buttonValNew=digitalRead(buttonPin);
if (buttonValOld==1 && buttonValNew==0){
motDir=motDir*(-1);
myStepper.step(-stepsPerRevolution);

delay;
}
}
````

I saw, that some times he uses delay, and I thought that that was to make some sort of diference with lines in coding; like I said I am totally newbie

Please show me the example where you saw someone using delay without the parenthesis and the number.

That statement as you've written it does nothing. Nothing at all. You can just remove that.

Please press control-T on your code before you post it. You'll find that it's a lot easier to understand when you get the indentation to match the nesting. Since the IDE will do the formatting for you, there's no real reason not to do it.

After the first time through what is buttonValOld? Shouldn't it have the old button value? But you only ever set it to 1 before the program runs. You probably need to put a line somewhere in loop to set it equal to the current button state.

Your original code was correct, but the old way, and maybe confusing to some.

pinMode(buttonPin,INPUT); // not needed, because the pin is already an INPUT at bootup
digitalWrite(buttonPin,HIGH); // enable internal pull up

// replaced with

digitalWrite(buttonPin, INPUT_PULLUP); // enable pull up on an input pin, all in one line

// this is doing things twice

pinMode(buttonPin, INPUT_PULLUP); // enable pull up
digitalWrite(buttonPin,HIGH); // enable pull up

Leo..

Don't guess about those things. There's no need to guess, there's documentation for all these functions - when you don't understand a function or a command, look it up rather than trying to guess what it means. That way you never make any progress in understanding what you're doing.

The same accounts for all other functions there.

One very likely issue you still have is button bounce - where a button when pressed makes contact, breaks it again, and after a few bounces finally comes to reset. That takes a millisecond or two, which is fast for us humans but a very long time for an Arduino. Do check out this link, even a simple button is not as simple as you probably think it is!

There are some points to consider with your code.
You are using the stepper.h library which blocks while the stepper is moving. This has pros and cons:

  • + You don't need debouncing, because after the first contact when you press the button and the motor starts, any bouncing happens while the stepper is already turning and thus is ignored.
  • + You don't need state change detection, if you don't press the button longer than the rotation lasts (and that may take a while). That means you don't need buttonValOld ( which you in fact really don't use so far)
  • - You cannot stop the movement of the stepper while it is rotating. If that is a problem for you, you should change to another library.

Your motDir is set but never used. You can use it to remember the last direction of movement, so you can alternate the direction of movement with each button press.

Consider (untested):

    buttonValNew = digitalRead(buttonPin);
    if (buttonValNew == LOW) {
        // button has been pressed, start movement
        if ( motDir == 1) { // direction of rotation?
            myStepper.step(stepsPerRevolution);
            motDir = 0;
        } else {
            myStepper.step(-stepsPerRevolution);
            motDir = 1;
        }
        delay(500);  // if you want a delay after movement
    }

thanks so much I am going to try it tonight!