RSLK Reverse Motor

I've assembled the TI RSLK Robot Maze Edition for a final project for my embedded systems class. Instead of using the MSP432P401R LaunchPad that came with the kit to control the power board, I've used an Arduino MEGA 2560 instead. I'm also using a KY-023 Analog Joystick to control the two dc motors.

The challenge that I'm having now is how do I write my code to reverse the motors in the opposite direction so the RSLK can move backwards?

Here's my code

int xPin = A0; // analog input for x-axis joystick
int yPin = A1; // analog input for y-axis joystick

int yPosition = 0;
int xPosition = 0;

//Right motor pins
#define MRSpeed 11
#define MRDir 10
#define MRSleep 9

//Left motor pins
#define MLSpeed 8
#define MLDir 7
#define MLSleep 6


void setup() {
  Serial.begin(9600);
  pinMode(xPin, INPUT); // setup x input
  pinMode(yPin, INPUT); // setup y input
  
  pinMode(MRSpeed, OUTPUT);
  pinMode(MRDir, OUTPUT);
  pinMode(MRSleep, OUTPUT);
  digitalWrite(MRSleep, 1);

  pinMode(MLSpeed, OUTPUT);
  pinMode(MLDir, OUTPUT);
  pinMode(MLSleep, OUTPUT);
  digitalWrite(MLSleep, 1);
  
}

void loop() {
  
  yPosition = analogRead(yPin);
  xPosition = analogRead(xPin);

  //Steer Forward
  if (yPosition > 1020) 
  {
    analogWrite(MRSpeed, 255); //To move right wheel forward 
    analogWrite(MLSpeed, 255); //To move left wheel forward 
  }

  //To stop both wheels
   else if (yPosition = 506 || 505) 
   {
    analogWrite(MRSpeed, 0); 
    analogWrite(MLSpeed,0); 
   }

  //Turn Right
   if(xPosition < 512) 
    {
    analogWrite(MRSpeed, 0);
    analogWrite(MLSpeed, 255);
    }

  //Turn Left
    if(xPosition > 1020) 
      {
      analogWrite(MRSpeed, 255);
      analogWrite(MLSpeed, 0);
      }
    
   
}

As you can see I was able to build my code to make it move forward, right, and left.

You control that using the direction pin (MRDir and MLDir). Since your current code never writes to these pins, I am assuming that the output is LOW and it is causing your motors to rotate forward. If you write a HIGH value to these pins, the motors will rotate in the opposite direction.

I added some code under the comment Reverse. I wrote the MRDir to HIGH as you suggest and also wrote that the right will move however there is no movement.

int xPin = A0; // analog input for x-axis joystick
int yPin = A1; // analog input for y-axis joystick

int yPosition = 0;
int xPosition = 0;

//Right motor pins
#define MRSpeed 11
#define MRDir 10
#define MRSleep 9

//Left motor pins
#define MLSpeed 8
#define MLDir 7
#define MLSleep 6


void setup() {
  Serial.begin(9600);
  pinMode(xPin, INPUT); // setup x input
  pinMode(yPin, INPUT); // setup y input
  
  pinMode(MRSpeed, OUTPUT);
  pinMode(MRDir, OUTPUT);
  pinMode(MRSleep, OUTPUT);

  
  digitalWrite(MRSleep, 1);

  pinMode(MLSpeed, OUTPUT);
  pinMode(MLDir, OUTPUT);
  pinMode(MLSleep, OUTPUT);
  digitalWrite(MLSleep, 1);

  digitalWrite(MRDir,LOW);
  
}

void loop() {
  
  yPosition = analogRead(yPin);
  xPosition = analogRead(xPin);

  //Steer Forward
  if (yPosition > 1020) 
  {
    analogWrite(MRSpeed, 255); //To move right wheel forward 
    analogWrite(MLSpeed, 255); //To move left wheel forward 
  }

  //To stop both wheels
   else if (yPosition = 506 || 505) 
   {
    analogWrite(MRSpeed, 0); 
    analogWrite(MLSpeed,0); 
   }

   //Reverse
   else if (yPosition < 505)
   {
    digitalWrite(MRDir, HIGH);
    analogWrite(MRSpeed,255);
    }

  //Turn Right
   if(xPosition < 512) 
    {
    analogWrite(MRSpeed, 0);
    analogWrite(MLSpeed, 255);
    }

  //Turn Left
    if(xPosition > 1020) 
      {
      analogWrite(MRSpeed, 255);
      analogWrite(MLSpeed, 0);
      }
    
   
}