code for two motors

I'm running two motors connected to a transistor. Pin 9 is the first motor and Pin 10 is the second motor. I've written some code. first of all both motors should rotate for 10 seconds and then turn off. Then the first motor(pin 9) should rotate on its own for 2 seconds and then turn off. Is the following code correct?

const int transistorPin = 9;    // connected to the base of the transistor
const int transistorPin = 10;

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   digitalWrite(transistorPin, HIGH);
   delay(10000);
   digitalWrite(transistorPin, LOW);
 }
 
const int transistorPin = 9;    // connected to the base of the transistor

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   digitalWrite(transistorPin, HIGH);
   delay(2000);
   digitalWrite(transistorPin, LOW);
 }

It may be correct for two sketches (apart from the multiply-defined "transistorpin"), but not for one.

The compiler will have told you that.
You can have one "loop" and one "setup" in a sketch.

Please use tags when posting code

I redefined my second motor and then adjusted some of the code.
I think this should work…

What do you think?

const int transistorPin = 9;    // connected to the base of the transistor
const int transistorPin1 = 10;

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
   pinMode(transistorPin1, OUTPUT);
 }

 void loop()
 {
   digitalWrite(transistorPin, HIGH); //turn on
   digitalWrite(transistorPin1, HIGH); //turn on

   delay(10000);

   digitalWrite(transistorPin, HIGH); //turn on
   digitalWrite(transistorPin1, LOW); //turn off

   delay(2000);
 }

It will probably run, but it won't do what you intended.
Why don't you try it?
You're the one with the hardware.

I can't test it because I'm getting my arduino uno replaced because it was faulty. Till then, I can only try to visualise I guess.

You say it wont do what I intended. What would it do then.Will Both motors rotate for 10 seconds, then Motor 1 will continue running for another 2 seconds, while Motor 2 will stop?

Will Both motors rotate for 10 seconds, then Motor 1 will continue running for another 2 seconds, while Motor 2 will stop?

Correct, but that's not what you described in your first post, with both motors stopping after ten seconds

Ah yes. I actually meant for both to run for 10 seconds, motor 1 continues for 2 and motor 2 stops. My bad, i should i have clearly wrote it.

Anyway, Thankyou

I rewrote your code and changed the pin names to motor1 and motor2 to make it easier to understand.
I also added comments and made changes to bring the motors to a stop for 2 seconds.

Read the code over carefully and tell us if this is what you want to do.

Also why do you think your Uno was faulty?

const int motor1 = 9;    // connected to the base of the transistor
const int motor2 = 10;

void setup() {
  // set  the transistor pin as output:
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
}

void loop()
{
  digitalWrite(motor1, HIGH); //turn on motor1
  digitalWrite(motor2, HIGH); //turn on motor2

  delay(10000);

  // motor1 is still running because we have not changed digitalWrite for motor1.
  digitalWrite(motor2, LOW); //turn off motor2

  delay(2000);

  // now turn off motor1 and both motors will be stopped
  digitalWrite(motor1, LOW); //turn off motor1

  delay(2000); // tank will be stopped for 2 seconds
}