Controlling 2 DC motors with two potentiometers

Ok so Im working on an Etch a sketch type project working from a printer with two DC motors. Im having a little trouble with the code. I cant seem to get the potpin2 to work with its motor. Can anybody helps? Sorry Im brand new, and this is one of my first arduino attempts.

thanks

/*
* Arduino code for SN754410 H-bridge
 * motor driver control. A potentiometer's
 * 270 degree rotational angle is used to
 * control a DC motor in the following way:
 * 0-135 degrees - motor runs forward, speed
 * getting slower the higher the angle, motor
 * stops at 135 degrees.
 * 135-270 degrees: motor runs backward,
 * speed is getting faster the higher the
 * angle, motor speed is 0 at 135 degrees.
 *
 * copyleft Feb. 2008, Fabian Winkler
 *
 */
int potPin1 = 0; // analog input pin 0 for the potentiometer
int potPin2= 1; // analog input pin 2 for the potentiometer
int speedPin1 = 9;// H-bridge enable pin for speed control
int speedPin2 = 10; // H-bridge enable pin for speed control
int motor1Pin = 1; // H-bridge leg 1
int motor2Pin = 2; // H-bridge leg 2
int motor3Pin = 11; // #2 H-bridge leg 1
int motor4Pin = 12; // #2 H-bridge leg 2
int ledPin = 13; // status LED
int val1 = 0; // variable to store the value coming from the #1 potentiometer
int val2 = 0; // variable to store the value coming from the #2 potentiometer


void setup() {
  // set digital i/o pins as outputs:
  pinMode(speedPin1, OUTPUT);
  pinMode(speedPin2, OUTPUT);
  pinMode(motor3Pin, OUTPUT);
  pinMode(motor4Pin, OUTPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
  digitalWrite(ledPin, HIGH); // status LED is always on

  val1 = analogRead(potPin1); // read the value from the potentiometer
  val1 = val1 /4; // convert 0-1023 range to 0-255 range
  if (val1 <= 127) {
    // put motor in forward motion
    digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
    // control speed based on angle of potentiometer
    analogWrite(speedPin1, 254-(val1*2)); // output speed as PWM value
    // this value needs to go from 254 to 0 for input values
    //from 0 to 127
  }
  // put motor in backward motion
  else {
    digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
    // control speed based on angle of potentiometer
    analogWrite(speedPin1, (val1*2)-256); // output speed as PWM value
    // this value needs to go from 0 to 254 for input values
    // from 128 to 255
    }
  
   val2 = analogRead(potPin2); // read the value from the potentiometer
  val2 = val2/4; // convert 0-1023 range to 0-255 range
  if (val2 <= 127) {
    // put motor in forward motion
    digitalWrite(motor3Pin, LOW); // set leg 1 of the H-bridge low
    digitalWrite(motor4Pin, HIGH); // set leg 2 of the H-bridge high
    // control speed based on angle of potentiometer
    analogWrite(speedPin2, 254-(val2*2)); // output speed as PWM value
    // this value needs to go from 254 to 0 for input values
    //from 0 to 127
  }
  // put motor in backward motion
  else {
    digitalWrite(motor3Pin, HIGH); // set leg 1 of the H-bridge high
    digitalWrite(motor4Pin, LOW); // set leg 2 of the H-bridge low
    // control speed based on angle of potentiometer
    analogWrite(speedPin2, (val2*2)-256); // output speed as PWM value
    // this value needs to go from 0 to 254 for input values
    // from 128 to 255
  
    }
}

First off, swap the wiring, just to make sure it's not a blown transistor somewhere. Second, my guess is the problems to do with your analogWrite usage. Try taking the maths out of the function call and see if it helps.

Also worth freeing up the RX and TX pins. They make debugging a lot easier. Just pepper your code with Serial.println() commands. (The number of times I found maths doing weird things causing all sorts of mischief!)

how exactly could I use serial.print and what does it do?

Read the doco on the Serial lib.. but here's some pointers...

Serial.begin( SPEED );
...
Serial.println( 'HELLO WORLD" );

The Serial Monitor option under Tools is your friend... :slight_smile:

G.

This is the bit that is wrong:-
int motor1Pin = 1; // H-bridge leg 1

You should not be using pin 1 (or pin 0) for anything as that is the pin used in serial communications. If you use it you will get things messed up like you are finding.