Hello, I am very new to this and am looking for a bit of help on writing code for a stepper motor to be controlled by four push button switches. I want the pressing of button one to move the stepper motor to 0 degrees, the pressing of button 2 to move it to 90 degrees (400 micro steps), the pressing of button 3 to move it to 180 degrees (800 micro steps), and the pressing of button 4 to move it to 270 (1200 micro steps). Basically 90 degree, or 400 micro step increments from a start position, each button after button one adding 90 degrees (400 micro steps) to the previous button position. I have an Arduino Mega 2560 R3 and am using an EasyDriver Stepper Motor Driver from Sparkfun. I have attached both diagrams, and also the code I have written so far.
Again, I am incredibly novice at this, and literally any help is welcome.
PB1, 2, 3, and 4 are all set to automatically write HIGH when not pressed. I assigned the a, b, c, and d values to read each, and by saying !HIGH, they automatically read low unless pressed, then they read HIGH, allowing my if (a==1) statement to carryout. I could get rid of the "not" and just go with (a==0) to carryout the command, but I went with the first variation (no real reason, just did).
You are quite correct on that semi-colon comment, thank you, you've already helped me! Clearly proven, I am quite the novice.
Sorry for not simply posting the code, I was unaware how. I have made the semi-colon correction.
//In this sketch I wish to control a stepper motor using my Arduino Mega 2560 R3, utilizing four push button switches.
#define DIR 22 //setting the direction output of stepper (DIR) to pin 22
#define STP 2 //setting steps output of stepper motor (STP) to pin 2
#define PB1 25 //setting input of push button switch one (PB1) to pin 25
#define PB2 26 //setting input of push button switch two (PB2) to pin 26
#define PB3 27 //setting input of push button switch three (PB3) to pin 27
#define PB4 28 //setting input of push button switch four (PB4) to pin 28
void setup()
{
pinMode(DIR, OUTPUT);
pinMode(STP, OUTPUT);
pinMode(PB1, INPUT);
digitalWrite(PB1, HIGH);
pinMode(PB2, INPUT);
digitalWrite(PB2, HIGH);
pinMode(PB3, INPUT);
digitalWrite(PB3, HIGH);
pinMode(PB4, INPUT);
digitalWrite(PB4, HIGH);
}
void loop()
{
byte a=!digitalRead(PB1);
byte b=!digitalRead(PB2);
byte c=!digitalRead(PB3);
byte d=!digitalRead(PB4);
int stepperPosition0=400;
int stepperPosition1=(stepperPosition0+400);
int stepperPosition2=(stepperPosition1+400);
int stepperPosition3=(stepperPosition2+400);
if (a==1) //if PB1 is pressed
{
digitalWrite(STP,stepperPosition0); //stepper motor will go to position 0
}
else if (b==1) //unless PB2 is pressed
{
digitalWrite(STP,stepperPosition1); //then stepper motor will go to position 1
}
else if (c==1) //unless PB3 is pressed
{
digitalWrite(STP,stepperPosition2); //then stepper motor will go to position 2
}
else if (d==1) //unless PB4 is pressed
{
digitalWrite(STP,stepperPosition3); //then stepper motor will go to position 3
}
}
Can anyone tell me if I'm sending the signal to the EasyDriver Stepper Motor Driver correctly so that the stepper motor will actually move? I feel as if the 400 micro step increments are unit-less in the code I have written and my Arduino will not know what to do with that value. Will it essentially ask "400 what?", and if so, how can I assign 400 a unit so it realizes what action to carryout.