Need some simple C programing help

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
I have used this tutorial http://itp.nyu.edu/physcomp/Labs/DCMotorControl
But it's lacking two things
1) Needs a switch so i can have it turn the ASAP and not have to wait for the Arduino to boot
2) I need to figure out how after the switch^ is turned on the motors only run for X amount of time

int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
}

/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}



This is all for a small physics project, the car needs to travel 5m

Thanks for all your help

Alfa
:cookie:
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Not to be sarcastic, but the code window has been broken as long as I've known. :roll: (was there a time when it actually worked? )

Can you re-post your code, so we can read it more easily?
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
Not to be sarcastic, but the code window has been broken as long as I've known. :roll: (was there a time when it actually worked? )

Can you re-post your code, so we can read it more easily?

wow i feel stupid, i thought i looked over the thread once... i guess not ;)
I think it did work in the past
 

sao123

Lifer
May 27, 2002
12,656
207
106
Originally posted by: alfa147x
Originally posted by: chronodekar
Not to be sarcastic, but the code window has been broken as long as I've known. :roll: (was there a time when it actually worked? )

Can you re-post your code, so we can read it more easily?

wow i feel stupid, i thought i looked over the thread once... i guess not ;)
I think it did work in the past

it worked for exactly 2 hours and 3 minutes.
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Ok so i tried to get it so when the switch is toggled it digitalWrite(enablePin, HIGH);
The when its toggled again its set to digitalWrite(enablePin, LOW); but this doesnt do anything. What am thinking wrong here... :(

int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set enablePin high so that motor can turn on:
//digitalWrite(enablePin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(enablePin, HIGH);
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(enablePin, LOW);
}
}

/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
An interesting project you have here. Reminds me of my college electronics labs.I've done similar things, but with a PIC micro controller and using high-voltage transistors to create the H-bridge rather than an IC. (I couldn't find one in the stores I went to) So, I'm familiar with assembly PIC programming, but not C. (I know C, but I've never tried it on a micro controller)

Now, "Arduino" is new to me. You mention not waiting for it to boot up, so I assume that it takes more than 5 seconds to start ? Hmmm... if that is the case, and assuming you are coding into the micro controller, wouldn't that defeat the purpose? i.e. if your code is on the micro and it takes time to start, but you need to perform an action immediately after pushing a button, then you need another way to send instructions to the motor, right?

Let me ignore the 'immediate' startup requirement for a moment. According to the code, if the switch is turned ON, the motor continously spins one direction and if the switch is turned OFF, the motor spins the other direction. This is handled by this code,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
.......}
}

Now try changing that to this,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
.......}
}

That should make the motors stop spinning after 3 LED blinks.

:frown: ... Now I feel like an idiot for doing what appears to be a homework assignment... I suppose you caught me with too much spare time on my hands.
-sigh- :roll:

Tell us if it worked out, ok? ;)
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: alfa147x
Ok so i tried to get it so when the switch is toggled it digitalWrite(enablePin, HIGH);
The when its toggled again its set to digitalWrite(enablePin, LOW); but this doesnt do anything. What am thinking wrong here... :(

int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set enablePin high so that motor can turn on:
//digitalWrite(enablePin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(enablePin, HIGH);
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(enablePin, LOW);
}
}

/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}

I take back what I said about feeling like an idiot, you deserve more than that for the effort you are trying.

You need to understand what the enable pin does. Essentially, it turns your H-bridge ON or OFF.

In the above code, what I'd expect is nothing to happen.

Why ? Just read through your code, you have commented this out,

//digitalWrite(enablePin, HIGH);

Now what will that accomplish? The enablePin will be left at its default state (usually LOW). That means your H-bridge will never turn on. If you want anything to happen you need to make the pin go HIGH, that is ONLY done inside the "if" statement here,

....if (digitalRead(switchPin) == HIGH) {
........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
........digitalWrite(enablePin, HIGH);
....}

This means that after making your input switch go HIGH, it will set the legs of your H-bridge and THEN, turn it on. So, if you start up your system with the switch HIGH, your motor should start spinning in one direction. (assuming your h/w connections are correct)

Why don't you post your thoughts here? i.e. how you expect your changes to affect the system? We can help correct your thinking style, and it will turn you a better coder. :)
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: sao123
it worked for exactly 2 hours and 3 minutes.
Going off-topic, but I'd like to know HOW you arrived at THAT conclusion? :confused: And that too, so precise. :p
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
An interesting project you have here. Reminds me of my college electronics labs.I've done similar things, but with a PIC micro controller and using high-voltage transistors to create the H-bridge rather than an IC. (I couldn't find one in the stores I went to) So, I'm familiar with assembly PIC programming, but not C. (I know C, but I've never tried it on a micro controller)

Now, "Arduino" is new to me. You mention not waiting for it to boot up, so I assume that it takes more than 5 seconds to start ? Hmmm... if that is the case, and assuming you are coding into the micro controller, wouldn't that defeat the purpose? i.e. if your code is on the micro and it takes time to start, but you need to perform an action immediately after pushing a button, then you need another way to send instructions to the motor, right?

Let me ignore the 'immediate' startup requirement for a moment. According to the code, if the switch is turned ON, the motor continously spins one direction and if the switch is turned OFF, the motor spins the other direction. This is handled by this code,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
.......}
}

Now try changing that to this,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
.......}
}

That should make the motors stop spinning after 3 LED blinks.

:frown: ... Now I feel like an idiot for doing what appears to be a homework assignment... I suppose you caught me with too much spare time on my hands.
-sigh- :roll:

Tell us if it worked out, ok? ;)

Ill try this out tomorrow, thanks for the help

Oh its not a homework assignment, its a physics project where the circuit is suppose to have a motor, a battery, and a switch; this car must travel at least 5m but i kinda want learn more so i figured i would make it a bit more complicated. I want it to run exactly 5m and stop, and go as fast as possible. (Just so mine is sooo much better then everyone else's )

Thanks again, so yes your kinda doing my homework but not really

 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: alfa147x
Thanks again, so yes your kinda doing my homework but not really

Makes me feel a lot better now that you confessed. ;) he,he.

Your welcome. Just tell us how it went, ok? :)
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
Originally posted by: alfa147x
Ok so i tried to get it so when the switch is toggled it digitalWrite(enablePin, HIGH);
The when its toggled again its set to digitalWrite(enablePin, LOW); but this doesnt do anything. What am thinking wrong here... :(

int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set enablePin high so that motor can turn on:
//digitalWrite(enablePin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(enablePin, HIGH);
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(enablePin, LOW);
}
}

/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}

I take back what I said about feeling like an idiot, you deserve more than that for the effort you are trying.

You need to understand what the enable pin does. Essentially, it turns your H-bridge ON or OFF.

In the above code, what I'd expect is nothing to happen.

Why ? Just read through your code, you have commented this out,

//digitalWrite(enablePin, HIGH);

Now what will that accomplish? The enablePin will be left at its default state (usually LOW). That means your H-bridge will never turn on. If you want anything to happen you need to make the pin go HIGH, that is ONLY done inside the "if" statement here,

....if (digitalRead(switchPin) == HIGH) {
........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
........digitalWrite(enablePin, HIGH);
....}

This means that after making your input switch go HIGH, it will set the legs of your H-bridge and THEN, turn it on. So, if you start up your system with the switch HIGH, your motor should start spinning in one direction. (assuming your h/w connections are correct)

Why don't you post your thoughts here? i.e. how you expect your changes to affect the system? We can help correct your thinking style, and it will turn you a better coder. :)

The reason i commented that out was because then i can refer to my changes
I thought if i started the H bridge in the IF statement it would turn the motor on, and if the switch is turned off the H bridge is off.

After I figure this out, im going to next figure out how to make the motor run for X amount of time.

Im going to try out your code tomorrow as I am done for the night and about to hit the bed ;)

Thanks once again.

 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: sao123
Originally posted by: alfa147x
Originally posted by: chronodekar
Not to be sarcastic, but the code window has been broken as long as I've known. :roll: (was there a time when it actually worked? )

Can you re-post your code, so we can read it more easily?

wow i feel stupid, i thought i looked over the thread once... i guess not ;)
I think it did work in the past

it worked for exactly 2 hours and 3 minutes.

As far as I know it never worked :). Buuuut... there is an upcoming (someday, soon, I think) switch to different forum software that will hopefully improve this.
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: Markbnj
.. there is an upcoming (someday, soon, I think) switch to different forum software that will hopefully improve this.
Who around here doesn't know that? ;) It's the delay that's killing me!!!! :brokenheart:
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
also where can i find out what happens with different combinations of motor1Pin and motor1Pin on High and Low ?
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
:roll: Ummm.... there is a table on the link you gave in your OP post that lists that info ?

C'mon, dont' go lazy on us now !!!!!!!

You were doing so well too!!
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
:roll: Ummm.... there is a table on the link you gave in your OP post that lists that info ?

C'mon, dont' go lazy on us now !!!!!!!

You were doing so well too!!

Haha yeah but thats if I had 2 motors hooked into it, right?
Like the table only shows info for a 2 motors situation not a 1 motor set up
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: alfa147x
Haha yeah but thats if I had 2 motors hooked into it, right?
Like the table only shows info for a 2 motors situation not a 1 motor set up

I didn't realized that. :eek: Is it possible for you to show the schematic you are using? We just need to know to what pins of the H-bridge the motor is connected to and what pins the micro-controller is connected to the H-bridge.
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
Originally posted by: alfa147x
Haha yeah but thats if I had 2 motors hooked into it, right?
Like the table only shows info for a 2 motors situation not a 1 motor set up

I didn't realized that. :eek: Is it possible for you to show the schematic you are using? We just need to know to what pins of the H-bridge the motor is connected to and what pins the micro-controller is connected to the H-bridge.

Im about to try out the new code but here is how it is set up, exactly:
http://itp.nyu.edu/physcomp/im...bs/arduino_hbridge.jpg
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Originally posted by: chronodekar
An interesting project you have here. Reminds me of my college electronics labs.I've done similar things, but with a PIC micro controller and using high-voltage transistors to create the H-bridge rather than an IC. (I couldn't find one in the stores I went to) So, I'm familiar with assembly PIC programming, but not C. (I know C, but I've never tried it on a micro controller)

Now, "Arduino" is new to me. You mention not waiting for it to boot up, so I assume that it takes more than 5 seconds to start ? Hmmm... if that is the case, and assuming you are coding into the micro controller, wouldn't that defeat the purpose? i.e. if your code is on the micro and it takes time to start, but you need to perform an action immediately after pushing a button, then you need another way to send instructions to the motor, right?

Let me ignore the 'immediate' startup requirement for a moment. According to the code, if the switch is turned ON, the motor continously spins one direction and if the switch is turned OFF, the motor spins the other direction. This is handled by this code,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
.......}
}

Now try changing that to this,

void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
.......}
}

That should make the motors stop spinning after 3 LED blinks.

:frown: ... Now I feel like an idiot for doing what appears to be a homework assignment... I suppose you caught me with too much spare time on my hands.
-sigh- :roll:

Tell us if it worked out, ok? ;)

Ok so after replacing the IF ELSE with yours it acted normal, so I changed it to:
void loop()
{ // if the switch is high, motor will turn on one direction:
......if (digitalRead(switchPin) == HIGH)
......{
...........digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge low
...........digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
......} // if the switch is low, motor will turn in the other direction:
......else {
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
.......}
}

Now it works just right, the switch turns the motor on and off :)

Next two things:
1) Now i need help writing a working timer loop, so that after the motor is turned on it only runs for X amount of time
2) After that i want to replace this switch with a push button switch. This way its easier to activate the timer loop, thus having a faster reaction time.

 

chronodekar

Senior member
Nov 2, 2008
721
1
0
I can understand how the motor turns on/off using your logic. By setting both motor pins to HIGH, there is no potential difference across the motor as a result no current flows through it, and it doesn't spin.

But, are you telling me that my code didn't work as intended?

the important part what I added is this, (in both if and else)
...........blink(ledPin, 3, 100);
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);

Are you telling me that it has no effect whatsoever on the motor operation ? Hmmm... that doesn't sound right. You see the blink() function I use? Well, I meant that to act as the delay. Try setting the value to something like,
...........blink(ledPin, 3, 1000);

I'm hoping that will make the LED blink 3 times. And the 1000 represents 1 second. So, the blinking should last for 3 seconds. Then this part,
...........digitalWrite(motor1Pin, LOW);
...........digitalWrite(motor2Pin, LOW);
When the code flow reaches here, it *SHOULD* turn the motor off. Same logic you use. The state may as well be HIGH or LOW for both, but in either event, the motor should turn off.

Hmmm... we're misssing something here... Let's try this, replace the entire if-else with this,
............digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
............digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
...........blink(ledPin, 3, 1000);

...........digitalWrite(motor1Pin, HIGH);
...........digitalWrite(motor2Pin, HIGH);

Copying stuff from your working code again, what we should see is that the motor will be set to spin in one direction because of the first 2 lines. Then due to what I've bolded, we'll see a time delay. Then the last 2 lines (your idea) should turn the motor off.

The switch will do nothing with this code. But, the idea is to see if we can get the motor to spin for a certain amount of time. And after that, to see if it switches off.

What I want to know, is IF that delay is working. Give it a shot! :)