Two potentiometers and two dc motors

Hi all,

I'm trying to control two dc motors, like these http://img.diytrade.com/cdimg/472172/3499190/0/1224408480/Small_DC_Motor_for_electrical_tool_Toy_Hobby_Massage_machine.jpg

with two potentiometers (one slide and one turn).

I've hooked up the two motors like this: http://ardx.org/src/circ/CIRC03-sheet-OOML.pdf

I can control both the motors with the potentiometers, separately. So when I comment out one of the functions in the void loop, everything works fine.

But, when both functions are "on", a change in one of the potentiometers, makes BOTH motors turn. And they just keep turning for a random amount of time.

My goal is to achieve that I can just control the motors, at the same time, but with the two different potentiometers.

The code:

//  2 motors controlled by 2 pots

int motor1Pin = 10;     
int pot1Pin = A1;       
int pot1Value = 0;      

int motor2Pin = 9;
int pot2Pin = A0; 
int pot2Value = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
}

void loop()                
{
  sliderpotmotor();
  potmotor();
}

void potmotor(){

  pot1Value = analogRead(pot1Pin);  

  pot1Value = map(pot1Value, 0, 1024, 0, 255);

  digitalWrite(motor1Pin, pot1Value);

  // debug
  //Serial.println(pot1Value);    

}


void sliderpotmotor() {
  
  pot2Value = analogRead(pot2Pin);

  pot2Value = map(pot2Value, 0, 1024, 0, 255);

  digitalWrite(motor2Pin, pot2Value);

  // debug
  //Serial.println(pot2Value); 
}

Thanks in advance, Regards,

Lars

Shouldn’t those digitalWrite lines be analogWrite?

I’m assuming you want the pots to control the speed, although you never said that anywhere… unless I’m missing something, the pots aren’t shown on that pic- how are they hooked up?

Might be an idea to post a pic of the whole shebang…

Simpler: (Uncompiled, untested)

//  2 motors controlled by 2 pots
const int motor1Pin = 10;     
const int pot1Pin = A1;       

const int motor2Pin = 9;
const int pot2Pin = A0; 

void setup()
{
  Serial.begin(9600);
}

void loop()                
{
  potmotor(pot1Pin, motor1Pin);
  potmotor(pot2Pin, motor2Pin);
}

void potmotor(int inputPin, int motorPin) 
{
  int potValue = analogRead(inputPin);

  potValue = map(potValue, 0, 1024, 0, 255);

  analogWrite(motorPin, potValue);
}

thank you JimboZA;
changing digitalWrite into analogWrite did the trick, how could I be so stupid to overlook that…

and thank you AWOL;
I did not know you could define loops like that, indeed a simpler method that will come in handy (it does work).

thanks again,

Regards,
Lars

I did not know you could define loops like that

It's a function, not a loop. There are no explicit loops in the code I wrote, only the implicit one calling the function "loop()".

mrlrs: changing digitalWrite into analogWrite did the trick, how could I be so stupid to overlook that..

So how come it was working when you had either one of calls working on its own, though?