Hello everyone looking to see if anyone can help figure out a way to make this work.
This is on a milling machine, currently i have 1 potentiometer to control the speed of a stepper motor in clockwise and counterclockwise directions. I want to add a second potentiometer to split up the directions to their own speeds. Pot 1 will control speed clockwise, pot 2 will control speed counter clockwise.
This is the part of the code currently with one Potentiometer
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static int sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
int analog_value = 0; // Holds raw analog value.
static float previouscurrent_speed = 0.05; //declares "currentMillis" as to reflect Arduino millis
unsigned long currentMillis = millis();
if (digitalRead(LEFT_PIN) == 0) {
sign = 1; lcd.setCursor(12, 0);
lcd.print(" CW ");
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1; lcd.setCursor(12, 0);
lcd.print(" CCW");
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0; lcd.setCursor(12, 0);
lcd.print("STOP");
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(A1);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
Here is how im trying to implement the second potentiometer, ive tried this code and it dont work.. maybe someone has a better idea on how to implement this?
static float current_speed2 = 0.0; static float current_speed = 0.0;
static int analog_read_counter2 = 1000;
static int analog_read_counter = 1000;
static int sign = 0;
int analog_value2 = 0;
int analog_value = 0;
previouscurrent_speed2 = 0.05;
previouscurrent_speed = 0.05;
unsigned long currentMillis = millis();
if (digitalRead(LEFT_PIN) == 0) {
sign = 1; lcd.setCursor(12, 0);
lcd.print(" CW ");
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1; lcd.setCursor(12, 0);
lcd.print(" CCW");
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0; lcd.setCursor(12, 0);
lcd.print("STOP");
}
if (analog_read_counter2 > 0) {
analog_read_counter2--;
}
else {
analog_read_counter2 = 3000;
analog_value2 = analogRead(A2);
stepper1.runSpeed();
current_speed2 = -1 * (((analog_value2 / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
stepper1.setSpeed(current_speed2);
}
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
analog_value = analogRead(A1);
stepper1.runSpeed();
current_speed = 1 * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
stepper1.setSpeed(current_speed);
}