Hello, total beginner here... I'm trying to control a stepper motor (5V version of 28BYJ-48). This code runs the motor and it works fine, but what I'm looking to do is to add a momentary push button that can toggle between rotating CCW and CW, because now it only turns CCW. I'm assuming it's quite simple but as a total beginner I have really no clue where to get started.
Here is my code so far, simply just rotates the motor CCW at 2ms between each half-step
int in1 = 8; //ardunio pin 8 is now assigned to in1 on the ULN2003 driver
int in2 = 9; //arduino pin 9 is now assigned to in2 on the ULN2003 driver
int in3 = 10; //arduino pin 10 is now assigned to in3 on the ULN2003 driver
int in4 = 11; //arduino pin 11 is now assigned to in4 on the ULN2003 driver
#define x 2000 //"x" now means whatever number inserted here.
// In this case, the microsecond delay between each step.
// (2000 microseconds equals 2ms)
void setup() {
pinMode(in1, OUTPUT); //pin "in1" is now an output pin
pinMode(in2, OUTPUT); //pin "in2" is now an output pin
pinMode(in3, OUTPUT); //pin "in3" is now an output pin
pinMode(in4, OUTPUT); //pin "in4" is now an output pin
}
void loop() {
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, LOW);
digitalWrite (in4, LOW);
delayMicroseconds(x);
digitalWrite (in1, HIGH);
digitalWrite (in2, HIGH);
digitalWrite (in3, LOW);
digitalWrite (in4, LOW);
delayMicroseconds(x);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
digitalWrite (in3, LOW);
digitalWrite (in4, LOW);
delayMicroseconds(x);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
delayMicroseconds(x);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
delayMicroseconds(x);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
digitalWrite (in3, HIGH);
digitalWrite (in4, HIGH);
delayMicroseconds(x);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delayMicroseconds(x);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delayMicroseconds(x);
}