Controlling a stepper motor using button

#define STEPPER_PIN_1 9
#define STEPPER_PIN_2 10
#define STEPPER_PIN_3 11
#define STEPPER_PIN_4 12
#define Button 0

int step_number = 0; // To keep track of the stepper motor's state
bool motor_direction = true; // Motor direction: true for forward, false for reverse

void setup() {
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
pinMode(Button, INPUT); // Set the Button pin as input
}

void loop() {
// Check if the button is pressed (HIGH means pressed on most buttons)
if (digitalRead(Button) == HIGH) {
motor_direction = !motor_direction; // Reverse direction when button is pressed
delay(500); // Debounce delay to avoid multiple direction changes when button is pressed
}

// Run the motor in the current direction indefinitely
OneStep(motor_direction);
delay(2); // Small delay to control motor speed
}

void OneStep(bool dir) {
if (dir) { // If direction is true, move the motor in one direction
switch (step_number) {
case 0:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
}
} else { // If direction is false, move the motor in the opposite direction
switch (step_number) {
case 0:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
}
}

// Increment step number and reset if it exceeds 3
step_number++;
if (step_number > 3) {
step_number = 0;
}
}

More imformation, I am very new to arduino and the c++ programing language, I am trying to switch the direction of the motor every time I press a button however whenever I press the button the whole thing breaks and only 2 of the lights flash on the driver board. The reason I wrote out the steps is because the stepper library doesnt work for me. if you have any solutions that would be appreciated.

thx in advance

dam im an idiot thx


#define Button 0
. . .
pinMode(Button, INPUT); // Set the Button pin as input

  • How is the switch wired ?

  • Pin 0 on an UNO is the RX pin, what Arduino do you have ?

I dont think it is the wireing because previously I used someones code that reversed the direction after one rotation and it worked fine, all I added was a simple button circuit. I will work on a wiring diagram

I have an arduino uno, the button is connected to A0

  • Then use:
    #define Button 14

  • How is the switch wired ?

the 5v and ground is connected to the pos and negitive on the breadboard, the button is connected to the 5v and A0

  • Also add a 10k resistor from A0 to GND.

FYI

  • Wire as S1 is wired, but the circuit is not generally recommended.

ok, my class in ending i will try your suggestions Thank you for helping a slightly stupid noobie

  • Language like this is not needed.
1 Like

It sounds like you may be better off putting the phone down and paying attention to the lesson.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.