code for continuous movement of motor

hi. i am doing a project "fire extinguisher robot". for obstacle avoidance, i use ultrasonic sensor. got the code for it. planned to connect motors like,, motors in one side will be a connection( +ve and -ve) , motors on other side will be another connection. this will help in getting anti-clockwise and clockwise direction (left,right) i thought. but now, i coded for motors to run front, back and all. but its not working. here is the code.

int a=13, b=12, c=10, d=9;
void setup()
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
}
void loop()
{
front();
}
void front()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
}
void back()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
}
void left()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
}
void right()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
}

is there anything wrong?? please do tel me as i am a noob..now only learning..can i use h-bridge? i have a h-bridge motor drive 1.1 (l298n). if so, can u please provide the coding.?? thanks

Here's some code I was playing with to run 2 motors using an L298.
Might need to send a minimal value to get the motors to start moving.

//Serial Control 2 dc motors
char buffer[5]; // ru000 to ru255, rd000 to rd255, lu000 to lu255, ld000 to ld255 

//buffer>parameter

//PICK PINS so that analogWrite is available for the enablePin

const int motor1Pin = 13; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 12; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 10; // H-bridge enable pin

const int motor3Pin = 11; // H-bridge leg 1 (pin 2, 1A)
const int motor4Pin = 8; // H-bridge leg 2 (pin 7, 2A)
const int enablePin2 = 9; // H-bridge enable pin
byte incomingByte;
byte speed;


void setup()
{
  Serial.begin(9600);

  //Motors
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);

  pinMode(motor3Pin, OUTPUT); 
  pinMode(motor4Pin, OUTPUT); 
  pinMode(enablePin2, OUTPUT);

  //Disable them
  analogWrite(enablePin, 0); 
  analogWrite(enablePin2, 0); 
}


void loop()
{

  if (Serial.available() > 4) {  // got 5 bytes in?
    incomingByte = Serial.read();
    if  ((incomingByte == 'r') || (incomingByte == 'l')){ // got a good start character
      buffer[0] = incomingByte;
      buffer[1] = Serial.read();
      buffer[2] = Serial.read();
      buffer[3] = Serial.read();
      buffer[4] = Serial.read();
    // done capturing a good message
    // set right motor direction
    }
    if ((buffer[0] == 'r') && (buffer[1] == 'u')) {
        digitalWrite (motor1Pin, HIGH);
        digitalWrite (motor2Pin, LOW);
      }
    if ((buffer[0] == 'r') && (buffer[1] == 'd')) {
        digitalWrite (motor1Pin, LOW);
        digitalWrite (motor2Pin, HIGH);
      }
    // end right direction
    // set left motor direction
    if ((buffer[0] == 'l') && (buffer[1] == 'u')) {
        digitalWrite (motor3Pin, HIGH);
        digitalWrite (motor4Pin, LOW);
      }
    if ((buffer[0] == 'l') && (buffer[1] == 'd')) {
        digitalWrite (motor3Pin, LOW);
        digitalWrite (motor4Pin, HIGH);
      }
    // end left direction

    // maybe a 'b' case to move both together?

    // now the speed
    // convert digits to numbers, multiply by 100/10/1 and add together
    speed =((buffer[2]-48)*100) + ((buffer[3]-48)*10) + (buffer[4]-48); // check asciitable.com for conversion
    // what are we doing?
    Serial.print("moving motor ");
    Serial.print (buffer[0]);
    Serial.print (buffer[1]);
    Serial.println(speed); // leading 0's were gettng dropped from screen print, maybe play with this
    // which motor is to move?
    if (buffer[0] == 'r'){
      analogWrite(enablePin, speed);
    }
    if (buffer[0] == 'l'){
      analogWrite(enablePin2, speed);
    }
    buffer[0] = 0; // clear starting character
  } // end serial available
} // end  loop

mechatro:
hi. i am doing a project "fire extinguisher robot". for obstacle avoidance, i use ultrasonic sensor. got the code for it. planned to connect motors like,, motors in one side will be a connection( +ve and -ve) , motors on other side will be another connection. this will help in getting anti-clockwise and clockwise direction (left,right) i thought. but now, i coded for motors to run front, back and all. but its not working.

What voltage are the motors? The Arduino can only supply 5V.
What current do they take? An Arduino pin can only supply 40mA.

i use 12v 300 rpm dc motor. i am using a rechargable 12v battery for the motors.so no problem..

@crossroads i used this program for obstacle avoidance.

const int trigPin = 2;
const int echoPin = 4;
int m1=13,m2=12,m3=8,m4=7;

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}

void loop()
{

long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<20)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}
else
{ digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

it worked perfectly..but when i use the flame sensor, the robot has to rotate to find the direction of the fire..it has to rotate slightly and stop..rotate and stop..is there anyway i can get it.?

Read this before posting a programming question

How to use this forum

Code tags, please.

It would help understand your code if you used the variable names that you have given to the pins rather than the pin numbers in digitalWrite().

The first thing that I would do would be to write individual functions to move the robot forwards, backwards, spin left, spin right and stop. I would expect the functions to be called several times in the program and calling them by name will make it more obvious what is happening. So, to "find the fire" you would call spin (left or right) for a short period of time, determined initially by using the delay() function (although there are better ways) then call stop, test for a fire and repeat until one is detected.