Hello everyone,
I recently started my first project. I am building a workbench that has a table that can hide away inside to store my 3d printer when not in use. It will use 4 NEMA23 steppers each controlled with a TB6600 driver. It will need to move the steppers a specific number of turns that are yet to be determined. I was able to piece together same code to run each stepper. I am having some issues with making a switch work. Here is the code I have:
//definepins for buttons
#define buttonPinUp 11
#define buttonPinDown 12
//define pins for direction output
#define directionPin1 2
#define directionPin2 4
#define directionPin3 6
#define directionPin4 8
//define pins for step output
#define stepPin1 3
#define stepPin2 5
#define stepPin3 7
#define stepPin4 9
//define steps per revolution 1/32 steps = 6400
#define stepsPerRevolution 6400
void setup() {
// put your setup code here, to run once:
pinMode (buttonPinUp, INPUT_PULLUP);
pinMode (buttonPinDown, INPUT_PULLUP);
pinMode (directionPin1, OUTPUT);
pinMode (stepPin1, OUTPUT);
pinMode (directionPin2, OUTPUT);
pinMode (stepPin2, OUTPUT);
pinMode (directionPin3, OUTPUT);
pinMode (stepPin3, OUTPUT);
pinMode (directionPin4, OUTPUT);
pinMode (stepPin4, OUTPUT);
digitalWrite (stepPin1 , LOW);
digitalWrite (directionPin1 , LOW);
digitalWrite (stepPin2 , LOW);
digitalWrite (directionPin2 , LOW);
digitalWrite (stepPin3 , LOW);
digitalWrite (directionPin3 , LOW);
digitalWrite (stepPin4 , LOW);
digitalWrite (directionPin4 , LOW);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 4 * stepsPerRevolution; i++) {
if (digitalRead (buttonPinUp) == LOW && digitalRead (buttonPinDown) == HIGH) {
digitalWrite (directionPin1, LOW) ;
digitalWrite (stepPin1, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin1, LOW) ;
digitalWrite (directionPin2, LOW) ;
digitalWrite (stepPin2, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin2, LOW) ;
digitalWrite (directionPin3, LOW) ;
digitalWrite (stepPin3, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin3, LOW) ;
digitalWrite (directionPin4, LOW) ;
digitalWrite (stepPin4, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin4, LOW) ;
} else if (digitalRead (buttonPinUp) == HIGH && digitalRead(buttonPinDown) == LOW) {
digitalWrite (directionPin1, HIGH) ;
digitalWrite (stepPin1, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin1, LOW) ;
digitalWrite (directionPin2, HIGH) ;
digitalWrite (stepPin2, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin2, LOW) ;
digitalWrite (directionPin3, HIGH) ;
digitalWrite (stepPin3, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin3, LOW) ;
digitalWrite (directionPin4, HIGH) ;
digitalWrite (stepPin4, HIGH) ;
delayMicroseconds (25) ;
digitalWrite (stepPin4, LOW) ;
}
while (1){
}
}
}
There may be a more elegant solution to the code, but this is what I was able to piece together from a few youtube videos, and looking at code in some forum posts to get a general idea of what each line does.
I can get the motors to move without the code for the buttons, but I cant get the buttons to work . I would like to have the motors move to position on a single press of the momentary switch (press and release "up" button, motors turn CW X amount of steps and hold, press and release "down" button, motors turn CCW X amount of steps and hold). Is that possible?
