Hi forummembers,
I'm new here on the forum. I have some experience with Arduino. I have the following problem. Maybe someone can help me. I have a controller with 2 stepper motors. They each have their TB6600 as a driver. Each motor has its own on/off switch and its own 10K potentiometer to control the speed. The speed of the motors is displayed on the screen from 0-100%. The system works very well except that the 2 potentiometers affect each other. If I adjust one, then both motors accelerate or decelerate. When I work with only one motor, everything is as it should be. The connections have been checked multiple times and I can't find any errors in them. Can someone help me figure out how to fix this problem? Thank you for any input!!
Michel
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include <Stepper.h>
#define directionPin1 2
#define stepPin1 3
#define potentiometerPin1 A0
#define switchPin1 7
#define directionPin2 4
#define stepPin2 5
#define potentiometerPin2 A1
#define switchPin2 6
LiquidCrystal_I2C lcd(0x27, 16, 2);
int previousPotValue1 = 0;
int previousPotValue2 = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed M1: ");
lcd.setCursor(12, 0);
lcd.print(" % ");
lcd.setCursor(0, 1);
lcd.print("Speed M2: ");
lcd.setCursor(12, 1);
lcd.print(" % ");
// Serial.begin(9600);
pinMode(switchPin1, INPUT_PULLUP); // Schakelaar 1 heeft een pull-up weerstand
pinMode(switchPin2, INPUT_PULLUP); // Schakelaar 2 heeft een pull-up weerstand
pinMode(directionPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
digitalWrite(directionPin1, LOW);
pinMode(directionPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
digitalWrite(directionPin2, LOW);
}
void updateLCD(float speedPercent1) {
lcd.setCursor(10, 0);
lcd.print(speedPercent1, 0);
}
void updateLCD1(float speedPercent2) {
lcd.setCursor(10, 1);
lcd.print(speedPercent2, 0);
}
void loop() {
// Stepper motor 1
int potValue1 = analogRead(potentiometerPin1);
int potValue2 = analogRead(potentiometerPin2);
if (digitalRead(switchPin1) == LOW) {
float speedPercent1 = map(potValue1, 0, 1023, 100, 10); // Calculate speed percentage for motor 1
//Check if potentiometer 1 value has changed by more than 2%
if (abs(potValue1 - previousPotValue1) > 10) {
updateLCD(speedPercent1);
previousPotValue1 = potValue1;
}
int delayTime1 = map(potValue1, 0, 1023, 100, 1000); // Adjust these values according to your motor and preference
digitalWrite(stepPin1, HIGH);
delayMicroseconds(delayTime1);
digitalWrite(stepPin1, LOW);
// delayMicroseconds(delayTime1);
} else {
digitalWrite(stepPin1, LOW);
}
// Stepper motor 2
if (digitalRead(switchPin2) == LOW) {
int potValue2 = analogRead(potentiometerPin2);
float speedPercent2 = map(potValue2, 0, 1023, 100, 10); // Calculate speed percentage for motor 2
// Check if potentiometer 2 value has changed by more than 2%
if (abs(potValue2 - previousPotValue2) > 10) {
updateLCD1(speedPercent2);
previousPotValue2 = potValue2;
}
int delayTime2 = map(potValue2, 0, 1023, 100, 1000); // Adjust these values according to your motor and preference
digitalWrite(stepPin2, HIGH);
delayMicroseconds(delayTime2);
digitalWrite(stepPin2, LOW);
// delayMicroseconds(delayTime2);
} else {
digitalWrite(stepPin2, LOW);
}
}