Arduino uno + L298N 4Wheel car problem

Hi! I’m new in arduino and things involved so please be patient
I have to do a project that consists in building a machine capable to follow a line, to detect obstacles and avoid them. I buyed this stuff some time ago
http://www.ebay.com/itm/Remote-Control-Trace-Obstacle-Avoidance-Intelligent-Car-for-Arduino-/130948382242?pt=LH_DefaultDomain_0&hash=item1e7d21be22

After many hours of reading topics and tutorials, I wrote the following code:

#include <Servo.h>
// define motor pins
int MD1 = 6; // right motor 1
int MD2 = 2; // right motor 2
int MS1 = 3; // left motor 1
int MS2 = 4; // left motor 2
int ENA = 10; // enable MD1 & MD2
int ENB = 11; // enable MS1 & MS2


// define sensor pins
const int SenzorStanga=7; // SENZOR left
const int SenzorDreapta=8; // SENZOR right
const int SenzorFata=12; // SENZOR front
int SSt; //  SENZOR state left
int SDr; // SENZOR state right
int SF; // SENZOR state front

//define ultrasonic sensor and servo
int trig = A1; 
int echo = A0; 
int DF = 0; // front distance
int DDr = 0; // front distance
int DSt = 0; // front distance
Servo servodetect;  


void setup()
{
  Serial.begin(9600);
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
  pinMode (MD1, OUTPUT);
  pinMode (MD2, OUTPUT);
  pinMode (MS1, OUTPUT);
  pinMode (MS2, OUTPUT);
  
  pinMode (SS, INPUT);
  pinMode (SDr, INPUT);
  pinMode (SF, INPUT);
  
  pinMode (trig, INPUT);
  pinMode (echo, OUTPUT);
  servodetect.attach(9);
  
}

void loop()
{
  mfata(1);
  mspate(1);
  mdreapta(1);
  mstanga(1);
}

and the move code. mdreapta=move right, mstanga=move left

// define moves
void mfata (int a)
{
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (MD1, LOW);
  digitalWrite (MD2, HIGH);
  digitalWrite (MS1, HIGH); 
  digitalWrite (MS2, LOW); 
  delay (a * 1000);
}

void mspate(int b)
{
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (MD1, HIGH);
  digitalWrite (MD2, LOW);
  digitalWrite (MS1, LOW); 
  digitalWrite (MS2, HIGH); 
  delay (b * 1000);
} 

void mdreapta(int c)
{
  analogWrite (ENA, 250);
  digitalWrite (MD1, LOW);
  digitalWrite (MD2, HIGH);
  analogWrite (ENB, 250);
  digitalWrite (MS1, LOW); 
  digitalWrite (MS2, HIGH); 
  delay (c * 1000);
}

void mstanga(int d)
{
  analogWrite (ENA, 250);
  digitalWrite (MD1, HIGH);
  digitalWrite (MD2, LOW);
  analogWrite (ENB, 250);
  digitalWrite (MS1, HIGH); 
  digitalWrite (MS2, LOW); 
  delay (d * 1000);
}

also tryed this for left and right

void mdreapta(int c)
{
  analogWrite (ENA, 255);
  analogWrite (ENB, 255);
  digitalWrite (MD1, LOW);
  digitalWrite (MD2, HIGH);
  digitalWrite (MS1, LOW); 
  digitalWrite (MS2, LOW);
  delay (c*1000);
  digitalWrite (MD1, LOW);
  digitalWrite (MD2, LOW);
  digitalWrite (MS1, LOW); 
  digitalWrite (MS2, HIGH);
  //delay (c * 2000);
  delay (c*1000);
}

void mstanga(int d)
{
  analogWrite (ENA, 255);
  analogWrite (ENB, 255);
  digitalWrite (MD1, HIGH);
  digitalWrite (MD2, LOW);
  digitalWrite (MS1, LOW); 
  digitalWrite (MS2, LOW);
  delay (d*1000);
  digitalWrite (MD1, LOW);
  digitalWrite (MD2, LOW);
  digitalWrite (MS1, HIGH); 
  digitalWrite (MS2, LOW);
  //delay (d * 2000);
  delay (d*1000);
}

but still don’t work

As you can see in the code, i didn’t used any sensor or servo till now, i just defined them cuz i inted to use them later.

My problem occurs when the car have to execute the cornering lines. I do not know if the 4 engines do not get enough power or is the code wrong, but when the left or right command runs, the car barely moves.
For power i use 6 x AA 1.5V duracel. I also tried to put arduino board to the 9V battery and the motor shield to the 6xAA, with the same result.
here’s avideo with the car in “action”:

What i’m doing wrong and how to resolve this?

PS: please excuse my grammar mistakes

You need to get each motor driving in the correct direction at the correct speed, but I have no way of knowing whether the code you posted does that.

I suggest you write a sketch that does nothing but drive one motor and make sure you know how to control the direction and speed. Then do the same for each of the other motors on their own. Then produce a combined sketch that drives all four motors together. It should work just the same as when the motors were driven singly, and if it doesn't then it suggests you might have a power supply or wiring problem.

You need to get each motor driving in the correct direction at the correct speed, but I have no way of knowing whether the code you posted does that.

i think i did that already because if i hold up the car, so the wheels don't make contact with a surface, they rotate properly
eg: for the turn left move i write in the sequence code for the left side [motor b] to move backwards and the right side [motor a] to move forward, and the wheels rotate properly

also i noticed one thing: if i put the car on shiny surface, works better and the curc actually turns, but in a different angle almoust every time

the turn left/right code might not be the best one, but i think the car should rotate

AdyR:
i think i did that already because if i hold up the car, so the wheels don't make contact with a surface, they rotate properly

That makes me suspect the problem is the motors just aren't generating enough torque to do what you want them to do. Perhaps they're too small, or not geared down sufficiently, or not getting as much power as they need.

i think is about the motors power.
i managed to make it work on a shiny surface, not the one in the video. seems to work ok.

now i have another question: it's ok if i use the 6xAA power for the L298N shield and the 1x9V for the arduino uno board?

The L298 shield can be powered from 6AA cells and the Arduino can be powered from a 9V battery via the power jack. Depending on the shield, it may also be capable of providing the logic level power supply for the Arduino itself - the motor shields I've used have some jumpers which can be used to select the power sources.

i'm stuck with another problem now.
if i define the servomotor in my code, motor A [right side of the car] don't work with analogWrite comand. if i use digitalWrite comand, works fine but i can't use the PWN function, and i really need that.
if i comment the "Servo servodetect" line, motor A works fine with analogWrite comand, and PWN function.

what i did wrong?

AdyR:
what i did wrong?

Read the documentation for the Servo library. It disables PWM output on certain pins, and unfortunately some motor shields use the affected pins for speed control.

ok, that solved the mistery.
i will search for a better servo library and see what i can do about that.

thanks a lot!