Controll two motors with Wifly RN-131C

Hello everyone,

I’m currently working on a project that involves controlling the direction of two motors mounted on a chasis over wifi. It’s like a wireless “tank” but I’m having problems when I try to send data to the wifly shield. The Wifly shield (RN-131C) is connected to a Arduino UNO R3 that has a Motor shield R3 to control the two motors mentioned before.

It uses the “W, A, S, D” keys of the computer keyboard to change the direction of the motors, note that without the wifly connection, the motors follow orders perfectly through the USB cable.

Now, here’s the thing: I can’t find any error in the sketch but one of the motors doesn’t follow orders, but the other one works perfectly. The only order wich seems to follow is when I tell him to stop, but it won’t change direction.

Anyone knows why? If someone could help me it would be of great help!

Here’s the Wifly code:

#include "WiFly.h"
#include "Credentials.h"

void setup() {

  int a;
  for (a=5; a<=8; a++)
  pinMode (a, OUTPUT);
  Serial.begin(9600);
  
  pinMode(12, OUTPUT); 
  pinMode(9, OUTPUT); 
  pinMode(13, OUTPUT); 
  pinMode(8, OUTPUT);
  
  digitalWrite (9, HIGH);
  digitalWrite (8, HIGH);
  
  Serial.begin(9600);
  WiFly.begin();
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }
  
  Serial.println("Associated!");
   
 }


void loop() {
 
   while (SpiSerial.available() < 1) {} // Wait until a character is received
   char val = SpiSerial.read();
   int leftspeed = 150; //255 is maximum speed
   int rightspeed = 150;
   switch(val) // Perform an action depending on the command
 {
   case 'w'://Move Forward
   forward (leftspeed,rightspeed);
   break;
   case 's'://Move Backwards
   reverse (leftspeed,rightspeed);
   break;
   case 'a'://Turn Left
   left (leftspeed,rightspeed);
   break;
   case 'd'://Turn Right
   right (leftspeed,rightspeed);
   break;
   default:
   digitalWrite(9,HIGH);
   digitalWrite(8,HIGH);
   break;
   }
    }
   
   void forward(char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   
   }
   void reverse (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void left (char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void right (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }

Here’s the code that works fine with the USB cable:

 void setup()
 {
  int a;
  for (a=5; a<=8; a++)
  pinMode (a, OUTPUT);
  Serial.begin(9600);
  
  pinMode(12, OUTPUT); 
  pinMode(9, OUTPUT); 
  pinMode(13, OUTPUT); 
  pinMode(8, OUTPUT);  
 
  Serial.println("WIFI ROVER");
  Serial.println("Treball de recerca de 2n de Batxillerat");
  Serial.println("Programari dissenyat per Josep Llistosella");
  Serial.println("Instruccions:");
  Serial.println("Apreta W per anar endavant");
  Serial.println("Apreta S per anar endarrere"); 
  Serial.println("Apreta A per anar a l'esquerra");
  Serial.println("Apreta D per anar a la dreta");
  
 }
   
  void loop()
  { 
   while (Serial.available() < 1) {} // Wait until a character is received
   char val = Serial.read();
   int leftspeed = 150; //255 is maximum speed
   int rightspeed = 150;
   switch(val) // Perform an action depending on the command
 {
   case 'w'://Move Forward
   forward (leftspeed,rightspeed);
   break;
   case 's'://Move Backwards
   reverse (leftspeed,rightspeed);
   break;
   case 'a'://Turn Left
   left (leftspeed,rightspeed);
   break;
   case 'd'://Turn Right
   right (leftspeed,rightspeed);
   break;
   default:
   digitalWrite(9,HIGH);
   digitalWrite(8,HIGH);
   break;
   }
    }
   
   void forward(char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   
   }
   void reverse (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void left (char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void right (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }

I think I’ve found the core of the problem, the pin 13 is being used by the motor shield and the wifly shield, and that may cause some sort of conflict between the motor connected to that pin and the wifly. Any ideas on how to solve this problem?