Arduino RC Car Coding Help

I'm a junior in high school who has been working closely with my teacher to make a RC car using an Arduino MEGA 2500 with a receiver and a Arduino Uno with a transmitter hooked up to a joystick. The transmitter is working fine but we're having problems with the car.
We have the Arduino hooked up to 2 motors through relays and the receiver, the receiver is receiving the cues from the transmitter just fine at a good pace but the car is driving as if it were a student driver where it's a very bumpy ride, constantly stopping and going at intervals. Here is the code:

#include <VirtualWire.h>
#include <VirtualWire_Config.h>

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages

byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message

// Define Relays
#define RELAY1  6                        
#define RELAY2  7                        
#define RELAY3  8                        
#define RELAY4  9

// Delay Fix
int delayPeriod = 50;

// Output Pins
int Motor1FwdPin = 6;        // Motor 1 forward
int Motor1RevPin = 7;        // Motor 1 reverse
int Motor2FwdPin = 8;        // Motor 2 forward
int Motor2RevPin = 9;        // Motor 2 reverse

// relay state status
int Relay1 = LOW;           // Relay 1 state
int Relay2 = LOW;           // Relay 2 state
int Relay3 = LOW;           // Relay 3 state
int Relay4 = LOW;           // Relay 4 state

void setup ()
{
    Serial.begin(9600);
    Serial.println("Device is ready");
    
    // Initialize the IO and ISR
    vw_setup(2000);    // Bits per sec
    vw_rx_start();     // Start the receiver     
    
    // Set pins for motor control
    pinMode (Motor1FwdPin, OUTPUT);
    pinMode (Motor1RevPin, OUTPUT);
    pinMode (Motor2FwdPin, OUTPUT);
    pinMode (Motor2RevPin, OUTPUT);
    
    digitalWrite(Motor1RevPin, LOW);
    digitalWrite(Motor2RevPin, LOW);
    digitalWrite(Motor1FwdPin, LOW);
    digitalWrite(Motor2FwdPin, LOW);
}

void loop()
{
  if (vw_get_message(message, &messageLength))
  {
    Serial.print("Received: ");    
    for (int i = 0; i < messageLength; i++)
    {
      Serial.write(message[i]);
    }
    Serial.println();
    
   // rest state
   if (message[0] == 'r')
   {
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     digitalWrite(Motor2FwdPin, LOW); 
   }
  
   // If message is Forward
   else if (message[0] == 'F')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     // set HIGH pins for movement
     digitalWrite(Motor1FwdPin, HIGH);
     digitalWrite(Motor2FwdPin, HIGH);
     delay(delayPeriod);
   }    
     
   // If message is Right
   else if (message[0] == 'R')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor2FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor1FwdPin, HIGH);
   }
      
   // If message is Left
   else if (message[0] == 'L')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor2FwdPin, HIGH);
   }
      
   // If message is Backward
   else if (message[0] == 'B')
   {
     // set LOW pins first
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2FwdPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor1RevPin, HIGH);
     digitalWrite(Motor2RevPin, HIGH);
   } 
      
   // If message is Foward-Right
   else if (message[0] == 'W')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor2FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor1FwdPin, HIGH);
   }
      
   // If message is Forward-Left
   else if (message[0] == 'X')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor2FwdPin, HIGH);
   }
      
   // If message is Back-Right
   else if (message[0] == 'Y')
   {
     // set LOW pins first
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor1RevPin, HIGH);
   }
      
   // If message is Back-Left
   else if (message[0] == 'Z')
   {
     // set LOW pins first
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2FwdPin, LOW);
     delay(delayPeriod);
     // set HIGH pins for movement
     digitalWrite(Motor2RevPin, HIGH);
   }
      
   // unknown state
   else
   {
     digitalWrite(Motor1RevPin, LOW);
     digitalWrite(Motor1FwdPin, LOW);
     digitalWrite(Motor2RevPin, LOW);
     digitalWrite(Motor2FwdPin, LOW); 
   }
 }
     
    //Do Nothing, Unknown Everything.
    else
    {
      digitalWrite(Motor1RevPin, LOW);
      digitalWrite(Motor1FwdPin, LOW);
      digitalWrite(Motor2RevPin, LOW);
      digitalWrite(Motor2FwdPin, LOW); 
    }
}

I have a feeling it is the delays in the code is causing this but when I take the delays out or place them in different spots withing the looping code for each cue the car will not move. One other thing I tried is putting the delay to a small amount (like 5 or 10) and it'll either not move or move very choppily.
I would really appreciate advice, what could I try to make it work?
(I also tried the function delayMicroseconds - that would not work either.)

Hi, can you post a copy of your circuit for the rc rec and arduino, it is important that power requirements for the arduino are met and likewise for the equipment it is driving.
A process called bypassing my have to be done to prevent interference from your motors and servo.
Have you tried controlling the car with the wheels off the bench, so there is very little electrical load on the drivetrain.

Tom.... :slight_smile:

First i would suggest you to remove all the delays from your code !
Then create a function for each motor movement(forward, backward...) and place it outside the loop function, when you need for example the forward function you call it inside the loop.