So I have to make a Stepper turn in 4 directions North, East, South, West (0º, 90º, 180º, 270º), with 4 different PushButtons.
My major doubt is how do I know how many steps are each degrees? Should i do the code as this:
if (digitalRead (ButtonNorth) == HIGH) {
myStepper.step (0) ;
}
if (digitalRead (ButtonEast) == HIGH) {
myStepper.step (90);
}
The data sheet for your stepper should list the steps per revolution spec for your motor. Chances are that it will not be an even number, though. For instance, for a 200 step motor it is 1.8 degrees per step or 0.555 steps per degree.
Welcome to the Arduino forum. You have a very familiar problem for newcomers to the stepper motor.
First, you have not told us how many steps per revolution your stepper motor makes. Without that information, you cannot even consider programming.
But, more fundamentally, you are not able to know where the stepper is located when you power it up. Until you can determine that, the rest of the programming is wasted effort.
Paul
Those snippets don't make sense. It all depends on your stepper, the complete sketch and if and which libraries you are using.
Show your sketch how you wrote it so far and don't forget to use the code tags when posting code. And as already mentioned, a stepper has no absolute position.
And it always a good idea to tell a little bit more about your project in general. That helps us to help you.
So if you will supply for us the stepper part number or a link to its data sheet we can better help you. Also what stepper driver that you are using and which Arduino board.
Read the forum guidelines to see how to get the most from this forum.
So its a 28BYJ-48 5V Stepper Motor
Arduino Uno R3
And here is the sketch with the buttons
I don't see any sketch...
To begin your program, forget all about the stepper motor and concentrate on being able to read each button and report it being pressed on the serial monitor part of the IDE.
Paul
Ok so I already did that but i dont know what to do with stepsperrevolution and how to make the stepper go north, east, west, south. Also you talked about the stepper location when i power it up, i would like it to start at north (0º) but how do i initialize him like that?
Here is the code I have until now
#include <Stepper.h>
int BotaoNorte = 7;
int BotaoEste = 6;
int BotaoSul = 5;
int BotaoOeste = 4;
Stepper myStepper(stepsPerRevolution, 8,10,9,11);
const int stepsPerRevolution = ??;
void setup()
{
Serial.begin(9600);
pinMode(BotaoNorte, INPUT);
pinMode(BotaoEste, INPUT);
pinMode(BotaoSul, INPUT);
pinMode(BotaoOeste, INPUT);
myStepper.setSpeed(??);
}
void loop()
{
if (digitalRead (BotaoNorte) == HIGH) {
Serial.println (" Botao Norte" );
myStepper.step (??);
}
if (digitalRead (BotaoEste) == HIGH) {
Serial.println (" Botao Este");
myStepper.step (??);
}
if (digitalRead (BotaoSul) == HIGH) {
Serial.println (" Botao Sul");
myStepper.step (??);
}
if (digitalRead (BotaoOeste) == HIGH) {
Serial.println ("Botao Oeste");
myStepper.step (??);
}
}
Well, you can manually move the stepper there before you power it up.
Or you can use some sort of switch to use in conjunction with the stepper motor and step the motor until it triggers the switch. Then you will know the stepper is at 0 degrees.
Paul
I've consulted the datasheet of the Stepper I'm using but I dont find the steps per revolution spec for my motor but for example, if it is a 200 step. 200/360 = 0.555 steps per degree, so for North position its 0, for east its 90*0.555 = 49.95,...
So in the code i put stepsPerRevolution = 200, when it goes to east i put my.Stepper.step (49.95) ?
These small stepper include a gear and usually have 2048 or 4096 steps / rev. But it's not always exact. Some have sligtly different steps/rev because of different gears.
Usually this written in the datasheet as steps/rev for the motor itself ( 64 steps/rev or 5.625° per step ) and the gear ratio.
The buitin stepper lib has some drawbacks for your usage. Because you can only tell it how much steps the stepper should do, you must always know where it is at the moment.
You can use my MobaTools lib instead ( you can install it by means of the library manager. It can compute the steps necessary for the degrees, and it remembers where the stepper is positioned.
With this library you can do something like this ( it compiles, but it's untested ):
#include <MobaTools.h>
int BotaoNorte = 7;
int BotaoEste = 6;
int BotaoSul = 5;
int BotaoOeste = 4;
const int stepsPerRevolution = 2048;
MoToStepper myStepper(stepsPerRevolution,FULLSTEP);
void setup()
{
Serial.begin(9600);
pinMode(BotaoNorte, INPUT);
pinMode(BotaoEste, INPUT);
pinMode(BotaoSul, INPUT);
pinMode(BotaoOeste, INPUT);
myStepper.attach(8,9,10,11);
myStepper.setSpeed(300); // speed in rpm*10
}
void loop()
{
if (digitalRead (BotaoNorte) == HIGH) {
Serial.println (" Botao Norte" );
myStepper.write (0);
}
if (digitalRead (BotaoEste) == HIGH) {
Serial.println (" Botao Este");
myStepper.write (90);
}
if (digitalRead (BotaoSul) == HIGH) {
Serial.println (" Botao Sul");
myStepper.write (180);
}
if (digitalRead (BotaoOeste) == HIGH) {
Serial.println ("Botao Oeste");
myStepper.write (270);
}
}
Thank you a lot!
If you could clarify me about a question I have:
For example, if in the datasheet it says 64 steps/rev why do we put "stepsPerRevolution = 2048" ?
You must consider the gear ratio too.
Ok mate, thank you a lot for your help, I really appreciate it!
Your'e welcome 
I did a little testing now, and my 28BYJ-48 cannot come up with the speed I used in the sketch. Maybe you have to experiment a little bit if you want to get it as fast as possible. And you may need a little speed ramp when starting/stopping . This works with my equipment:
myStepper.setSpeed(150); // speed in rpm*10
myStepper.setRampLen(100);
Remember, the motor is called a stepper motor because it only move in discrete steps. So no fractional steps.
For your project, consider you only EVER move 90 degrees. But you can move multiple of those 90 degrees. If I was writing the program, I would write a function to move the stepper the number of steps for 90 degrees. Then if needed, call the function 1 or more times to get your required position.
Paul
That would be much more complicated. You must debounce the buttons and do an edge detection. And you must remember the actual position for yourself.
With MoToStepper it is much more easy, because the write function positions the stepper to absolute degrees ( measured from the starting/reference point ). If you e.g. press the button for 180 degrees, it will go there, no matter how often and how long you press that button. No debouncing or edge detection is necessary. It's similar to driving a servo, but without the limitation of 0...180 degree.