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);
}
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);
}
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?
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
}