DC Motors unresponsive with wifi

Hi guys,

i'm building a wireless robot. I want it to react to the arrow keys. I've got the 2WD platform, with a mega 2560 on top, then a motor shield 2a and a wifi shield (wiz610wi).

The problem i'm having is when i set my speed above 100 and press the arrow up key down, the motors starts running, but when i release the key, it doesn't do anything. This doesn't happen when i release the up arrow within a second after pressing it down, or when the speed of the motors is less than 100, then the motor does stop almost every time i sent the stop-command. The lower the speed, the more responsive, a speed of 50 gave me the best result.

I'm using a c# app written in Visual Studio to control the robot via a socket to the wifi shield. It's a simple form, that just reacts to key up and key down events. When i test my code in the serial monitor (so no wifi), it runs perfectly fine. I've ported the code to a flash app, but it shows exactly the same behaviour.

What am i doing wrong? Is this a power issue? I've got the 2 motors hooked up to an external power supply (the battery pack that comes with the platform). The Arduino and the wifi shield are getting their power from a 6v - 1000mA power adapter.

c# code:

namespace socket
{
    public partial class Form1 : Form
    {

        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream;
        byte[] outStream;
        Boolean bLight = false;

        public Form1()
        {
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
            this.KeyUp += new KeyEventHandler(Form1_KeyUp);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Debug.Print("Client Started");
            clientSocket.Connect("192.168.0.193", 5000);
            Debug.Print("Client Socket Program - Server Connected ...");

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
           

            if (!bLight)
            {
                Debug.Print("form key down: key " + e.KeyValue);
                e.Handled = true;
                serverStream = clientSocket.GetStream();
                outStream = System.Text.Encoding.ASCII.GetBytes("go");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
                bLight = true;

            }

        }
        
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            Debug.Print("form key up: key " + e.KeyValue);
            e.Handled = true;
            serverStream = clientSocket.GetStream();
            outStream = System.Text.Encoding.ASCII.GetBytes("stop");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            bLight = false;
        }
    }
}

Arduino code:

  int E1 = 6;
  int M1 = 7;
  int E2 = 5;
  int M2 = 4; 
 
  String readString; 

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

  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);

  Serial.println("Welcome to the sockets");
}

void loop(){ 
  


  while (Serial.available()) {
    delay(1);
    
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

   if (readString.length() >0) {
    Serial.println(readString + " " + readString.length());  //so you can see the captured string 
   
   if (readString == "go"){
      
      Serial.println("going");
  
          digitalWrite(M1, HIGH);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 50);  
          analogWrite(E2, 50);
      
    }
    
    if (readString == "stop"){
      Serial.println("stopping");
      
      analogWrite(E1, 0);
      analogWrite(E2, 0);
      
    }
   
    readString="";
  } 
    

}

Thanks

When posting code, please put it inside code tags to make it easier to read - use the # button when composing the message.

Looks to me that you haven't got anything in place to limit the number of characters that you receive and append to readString. So it will expand to use all the available RAM, which is only 2K on the atmega328, then your program will crash. Personally, I don't think that classes that use dynamic memory allocation (such as String) have any place on an embedded platform with limited memory. I suggest instead that you use a fixed-size char array to hold the received characers, then count the characters you put into it and discard any excess characters.

I'll follow your advice and go back to an earlier version of my code where i used a simple character as command.

Like this:

char message = Serial.read();

But it has the same behaviour though :~.

My diagnosis: Your motor is outputing so much radio-frequency interference that the Wifi shield is unable to pick up any signals. You need to take precautions to reduce this interference and keep the motor and its leads away from the wifi card.

Get it working with a direct serial connection, then move to implement the wireless part.