Autonomous Car

can you explain as to why its 3 and 4. sorry for these stupid questions.

You can use any pins on the Arduino you want. Choosing a PWM as one of them gets you speed control.

3 is a pin with PWM capability.

An analogwrite to pin 3 sets the speed you want to go. 0 = stop. 255 = full speed.

Now digitalwrite to 4. 1 = one direction. 0 = the other direction.

so i completely re wired my circuit using this schematic. and this code. still not spinning the motor.
ignore the switch. i am not using that.

const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const 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);
}

ignore the switch. i am not using that.

The code is designed to check the switch and then run the motors ;D well in that case you could modify the sketch or just short out the place wher the switch should be there ( the diagram looks correct )

or Use this code ( i wrote it so that you could learn xomething out of it ) remove the entire switchey thing and

const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
 const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
 const int enablePin = 9;    // H-bridge enable pin
Void MAin()
{
 pinMode(motor1Pin, OUTPUT); 
   pinMode(motor2Pin, OUTPUT); 
   pinMode(enablePin, OUTPUT);
}
void loop ()
{
digitalWrite(motor1pin,HIGH);
digitalWrite(motor2pin,LOW);
digitalWrite(enablePin,HIGH); //you could use analog write on pwm pins to alter speed
dealy(10000);//motor spins in one diredtion for ten seconds
digitalWrite(motor1pin,LOW);
digitalWrite(motor2pin,HIGH);
digitalWrite(enablePin,HIGH); //you could use analog write on pwm pins to alter speed
dealy(10000);//motor spins in the other diredtion for ten seconds
}

i sure wish i knew that before. thanks for putting up with all my dumb mistakes. youre saying replace the switch code with that or that is what will be left. i will read through and try to make sense of that. thanks.

EDIT:

so i fixed up some little capitalization and spelling errors in your code and its compiling fine. now is that to be used alone or is that to be put in the other?

so im trying to do this basic code to get the motor to spin…

const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin

void setup()
{
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on: ADDED
digitalWrite(enablePin, HIGH);
}
void loop()
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}

if this is all correct it must be something with the connections i have. can someone verify this code should work so that i can move on from editing code to trying to fix my circuit.

also im using this schematic with the capacitor. http://itp.nyu.edu/physcomp/Labs/DCMotorControl im not using the switch though. is this schematic still good if i did everything but the switch?

is this schematic still good if i did everything but the switch?

Yes
but you need to use the code which dosent neeed a switch to activate the motor

exactly, so is the simple code i posted correct? because im still having issues and im not sure if its my code or my connections.

const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A) const int enablePin = 9; // H-bridge enable pin

void setup() { pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on: ADDED digitalWrite(enablePin, HIGH); } void loop() { digitalWrite(motor1Pin, HIGH); digitalWrite(motor2Pin, LOW); }

well thats not going to work if you dont have a delay because the pin is switched too fast to make any sense to the motor

ok i understand. so with a delay(10000); to make it spin for 10 seconds is that code correct?

YES :)

ok great. now i know my code works. now all i have to do is trouble shoot my connections... i wish i had an idea of where to begin..

i have already went two completely different ways. the first schematic and now im currently using the second. (minus the switch)

ok guys. so since with the help of newbie and a bunch of you ive finally figured out simple code that should make my motor spin the wheels. i need to figure out why it is not. so now i am on to my wiring. im using this schematic

what is crossed out in green i didnt use and i labeled and put in what i added. also im not using a 9v im using a 4 battery pack of AA batteries.

if anyone sees anything that i messed up or a better way to go about please post or message me. thanks a lot.

WEll you did wire up things corectly And you Were right you didnt need that ground Extra And IS it Working ;)

still not running. im at a lost considering my code and my wiring should both work. with other code i was trying i also made the led blink to see if the code was working at all and it was. i have no idea what to do as of right now. ive changed my wiring and coding twice with no success.

EDIT: im also going to get rid of the extra ground wire along with the two black wires to the left of it in the schematic because that side doesnt need ground because im not using it.

SECOND EDIT: im going to try a few things that will most likely not work (hard to be positive right now) 1. changed to a 9v 2. also im going to try to add simple LED code to see if thats getting sent to the board and being processed.

well where is your code ? abnd i just notice that your post is in the exhibition forum ;D

hah sorry when i first made my account i just kind of came on looked quickly and made on. if a mod wants to move it that would be great. i just now wrote up this code which the led is blinking but no motor movement.

#define m1p1 3 //motor one pin one
#define m1p2 4 //motor one pin two
const int ledPin = 13;
void setup()
{
pinMode (m1p1,OUTPUT);
pinMode (m1p2,OUTPUT);
pinMode(ledPin, OUTPUT);

blink(ledPin, 20, 100);
}
void loop()
{
digitalWrite(m1p1,HIGH);
digitalWrite(m1p2,LOW);
// drive motors forward set delay
delay(10000);
//now drive motors reverse both
digitalWrite(m1p1,LOW);
digitalWrite(m1p2,HIGH);
delay(10000);

}

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

Hmmm well but i see you use a enable pin in your schematic i guess pin 9 You need to put it high at all time to run the motors ! ( i didnt read the entire code )