Confused: Need to use to Potentiometers

I am working on a sculpture project and I need some help. I have two stepper motors that I want to control back and forth separately. I am using two 10k potentiometers but I feel that the arduino is reading both analog values at the same time or they are interfering with one another which is giving me some weird problems. Do I have to delay one motor command while the arduino reads from the second potentiometer? I am pretty new to this and I feel there is something small I am missing. Has anybody had a similar project (two pots running two motors separately) and has some sample code?

Thanks

Please share your code so we can have a look at it.

If possible also share the schematics .

int motorPin5 = 4;
int motorPin6 = 5;
int motorPin7 = 6;
int motorPin8 = 7;
int motorPin4 = 8;
int motorPin3 = 9;
int motorPin2 = 10;
int motorPin1 = 11;

int motorSpeed = 0;
int motor2Speed = 0;//variable to set stepper speed
int potPin = 2; //potentiometer connected to A2
int potValue = 0;
int pot2Pin = 4;
int pot2Value = 0;//variable to read A0 input
//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
//int potValue = potValue*2;
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

pinMode(motorPin5, OUTPUT);
pinMode(motorPin6, OUTPUT);
pinMode(motorPin7, OUTPUT);
pinMode(motorPin8, OUTPUT);

Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){

potValue = analogRead(potPin); //read the value of the potentiometer
Serial.println(potValue);//View full range from 0 - 1023 in Serial Monitor
if (potValue < 511){ //if potentiometer reads 0 to 511 do this
motorSpeed = (potValue/15 + 8); //scale potValue to be useful for motor
clockwise(); //go to the ccw rotation function
}
else { //value of the potentiometer is 511 - 1023
motorSpeed = ((1023-potValue)/15 + 8); //scale potValue for motor speed
counterclockwise(); //go the the cw rotation function
}

pot2Value = analogRead(pot2Pin); //read the value of the potentiometer
Serial.println(pot2Value);//View full range from 0 - 1023 in Serial Monitor
if (pot2Value < 511){ //if potentiometer reads 0 to 511 do this
motor2Speed = (pot2Value/15 + 8); //scale potValue to be useful for motor
clockwise2(); //go to the ccw rotation function
}
else { //value of the potentiometer is 511 - 1023
motor2Speed = ((1023-pot2Value)/15 + 8); //scale potValue for motor speed
counterclockwise2(); //go the the cw rotation function


}
}

void counterclockwise (){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay (motorSpeed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
//delay "motorSpeed" between each pin setting (to determine speed)

void clockwise(){
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay (motorSpeed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);
}


void counterclockwise2 (){
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW);
delay(motor2Speed);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW);
delay(motor2Speed);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin8, LOW);
delay(motor2Speed);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, HIGH);
delay(motor2Speed);
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
//delay "motorSpeed" between each pin setting (to determine speed)

void clockwise2(){
digitalWrite(motorPin8, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, LOW);
delay(motor2Speed);
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, LOW);
delay(motor2Speed);
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin5, LOW);
delay(motor2Speed);
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, HIGH);
delay(motor2Speed);
}

also the schematic is basic:

Im running two h-bridges attached to two steppers.

two 10k potentiometers connected to ground, 5v and each to its corresponding analog input.

I am still learning, Ive been looking around trying to figure out what to do, Ive seen people utilizing 10k resistors to create isolated voltage circuits? Im not sure

three things to be changed to the code

  • 1 - use proper indenting and add some extra spaces before the comments to make it more readable. Leave out comment that is trivial and take care that comments are correct (check for cw and ccw in your comments :slight_smile:

  • 2 - in loop() add two delay(100) before doing the analog read

  • 3 - You have split the potmeter range in two, but as a potmeter can fluctuate in its readings you will have problems in the “511” area. define a empty zone so fluctuations don’t influence the system. Done the first half in code below.

void loop()
{
  delay(100);
  potValue = analogRead(potPin); 
  Serial.println(potValue);
  if (potValue < [glow]505[/glow])  // was 511
  { 
    motorSpeed = (potValue/15 + 8);  //scale potValue to be useful for motor
    clockwise(); 
  }
  if (potValue > 516)       // note that values around 511 are skipped.
  { 
    motorSpeed = ((1023-potValue)/15 + 8); 
    counterclockwise(); 
  }

  delay(100);
  pot2Value = analogRead(pot2Pin); 
  Serial.println(pot2Value);

  ... etc

}

Make sure to get the data sheets for the shields and read over them. I was having all kinds of similiar problems with my sabertooth 2x25 going off the included "quick start" guide for analog mode. When I downloaded the entire manual from the site, I found some very pertinent info that was causing my problem. Basically, in analog mode and controlled by a microcontroller, I had to make a separate circuit in between the digital signal out and the H-Bridge, or else the 2 signals interfered with each other.