Problem controlling an Air-Swimmer via browser

Hi All,

I'm a beginner to arduino and my code is basically a mash-up from various tutorials.
It works perfectly (for me) but with one small issue.

Project Information: I am hacking an Air Swimmer Remote Air Swimmers - Awesome RC Flying Shark and Clownfish! - YouTube
control so that I can control it via web browser.

The code works how I need it to BUT there's one small issue.

After sending the first command every command after that repeats the previous one as well.

Here's my current code:

//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY


#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
  //byte ip[] = { 192, 168, 0, 199 };   //Manual setup only
  //byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
  //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

  // if need to change the MAC address (Very Rare)
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  EthernetServer server = EthernetServer(80); //port 80
////////////////////////////////////////////////////////////////////////

int led6=6;
int led7=7;
int led8=8;
int led9=9;

int myArrayF[]={500,500,500,500,500,500}; //Forward Movement Array
int myArrayT[]={500,500,500}; //Turning Movement Array


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

  //Pins 10,11,12 & 13 are used by the ethernet shield

  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  Ethernet.begin(mac);
  //Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

  server.begin();
  Serial.println(Ethernet.localIP());

}

void loop(){

  // listen for incoming clients, and process qequest.
  checkForClient();

}

void checkForClient(){

  EthernetClient client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println(F("HTTP/1.1 200 OK")); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<style type=\"text/css\">body {font-family: helvetica,sans-serif; color: white; background-color: black} </style>");
          client.println("<title>Arduino Web Page</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<center><h1>FLYING ORCA CONTROLS</h1></center>");
          client.println("<center><a href=\"/?6\"\"><img src='http://i.imgur.com/rihOyUl.png'></a></center>");
          client.println("<center><a href=\"/?7\"\"><img src='http://i.imgur.com/M6j2tA7.png'></a><a href=\"/?8\"\"><img src='http://i.imgur.com/KsRxtZQ.png'></a></center>");
          client.println("<center><a href=\"/?9\"\"><img src='http://i.imgur.com/RATzpAc.png'></a></center>");
          client.println("<center><p>A web page from the Arduino server</p></center>");
          client.println("</body>");
          client.println("</html>");
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);

           switch (c) {
             
            case '6':
            //add code here to trigger a RIGHT turn
              for(int t=0; t<3; t++){          //Button Right (Turn Right)
                  digitalWrite(led6,HIGH);
                  digitalWrite(led7,LOW);
                  delay(myArrayT[t]);
                  digitalWrite(led6,LOW);
                  digitalWrite(led7,LOW);
                  delay(200);
                  digitalWrite(led6,LOW);
                  digitalWrite(led7,HIGH);
                  delay(200); 
                  }
                  digitalWrite(led6,LOW);
                  digitalWrite(led7,LOW);
              triggerPin(6, client);
              break;
              
            case '7':
            //add code here to trigger a turn
              for(int t=0; t<3; t++){          //Button Left (Turn Left)
                  digitalWrite(led7,HIGH);
                  digitalWrite(led6,LOW);
                  delay(myArrayT[t]);
                  digitalWrite(led7,LOW);
                  digitalWrite(led6,LOW);
                  delay(200);
                  digitalWrite(led7,LOW);
                  digitalWrite(led6,HIGH);
                  delay(200); 
                  }
                  digitalWrite(led7,LOW);
                  digitalWrite(led6,LOW);
              triggerPin(7, client);
              break;
              
            case '8':
            //add code here to trigger a UP motion
            digitalWrite(led8,HIGH);            //Button UP for 600ms
            delay(800);
            digitalWrite(led8,LOW);
              triggerPin(8, client);
              break;
              
            case '9':
            //add code here to trigger a DOWN motion     
            digitalWrite(led9,HIGH);            //Button DOWN for 600ms
            delay(800);
            digitalWrite(led9,LOW);
              triggerPin(9, client);
              break;
          
          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

void triggerPin(int pin, EthernetClient client){
//blink a pin - Client needed just for HTML output purposes.  
  client.print("Turning on pin ");
  client.println(pin);
  client.print("
");


}

I know most of you are cringing for how poorly it is put together, but if there is a way so that I can get it to just run the command once without it repeating the previous one? I also attached the schematic if it helps. Just pretend the LEDs are the buttons on the remote.

Thanks a shit-tonne in advance.

I can't test this, as I don't have the equipment, but try this. Reset the variable 'c' at the end of the function CheckForClient with something like 'c = "";'
Again, you'll have to fiddle with it, but that could be a potential issue, that the variable is already assigned going in. But I honestly don't know, just an idea.

            case '9':
            //add code here to trigger a DOWN motion     
            digitalWrite(led9,HIGH);            //Button DOWN for 600ms
            delay(800);
            digitalWrite(led9,LOW);
              triggerPin(9, client);
              break;
          
          }
        c = ""; // Add something like this somewhere towards the end of this function.  You'll have to tinker with placement.
        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  }

Hope this helped... Have a good one!

P.S. What did you use to make that diagram?

Better than the last advice, would be to set c = ' ' ;

It's a char, not a string.

P.S. What did you use to make that diagram?

That's a Fritz.

Some browsers apparently send a second request for an icon along with each page request. this could be part of your issue. Below is some code that tells the browser to not update the current web page and dumps anything returned into an Iframe.

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on1\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}