Hello,
I am attempting to setup two push switches, each controlling one direction of rotation for my stepper motor via TB6600. However, I am still having issues with the coding and setup. Note* setup the switches without a pull down resisteor, the Arduino UNO detects the switches but vice versa. Could someone assist me and shed some light as to what went wrong.
// Pin definitions
#define DIR_PIN 2 // Direction control pin (to TB6600 DIR+)
#define STEP_PIN 3 // Step control pin (to TB6600 PUL+)
#define CW_BTN 4 // Clockwise button
#define CCW_BTN 5 // Counter-clockwise button
void setup() {
// Set TB6600 control pins as outputs
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
// Set pushbutton pins as inputs (external pull-downs used)
pinMode(CW_BTN, INPUT);
pinMode(CCW_BTN, INPUT);
}
void loop() {
// Read button states
bool cwPressed = digitalRead(CW_BTN);
bool ccwPressed = digitalRead(CCW_BTN);
if (cwPressed && !ccwPressed) {
digitalWrite(DIR_PIN, HIGH); // Set direction to clockwise
stepMotor();
}
else if (ccwPressed && !cwPressed) {
digitalWrite(DIR_PIN, LOW); // Set direction to counterclockwise
stepMotor();
}
// If both buttons released (or both pressed), do nothing
}
// Step the motor one step (adjust delay for speed)
void stepMotor() {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500); // Adjust for speed
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500); // Adjust for speed
}