user7861: Good day, I am currently attempting to write code that will allow a user to connect to an Arduino Mega via IP address and control a robotic arm on the X and Y axis as well as open and close the claw. I am facing 2 issues and hope someone can help me 

when it connects for the first few seconds the servos twitch and I'm not sure why
In my current code i am reading in the URL of the button pushed and moving the arm 5 degrees each time, but i would like to be able to hold the button down and have the arm move instead but I'm not sure how to go about that

/*
 Jamil Gaufr and Eva Santos
 Bridges Project @ SUNY Purchase
 Robotic Arm Control & Interface
 */

#include <SPI.h>
#include <Ethernet2.h>
#include <Servo.h>

//Represents servo to open/close claw
Servo microservo;
//Represents servo to move forward/away
Servo Yax;
//Represents servo to swivel left/right
Servo Xax;

int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
EthernetServer server(80);                             //server port
String readString;
String x = "x =";
String y = "y = ";
String cl = "claw  = "; 


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Yax.attach(4);
  Xax.attach(5);
  microservo.attach(6);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //Initializes every axis to start at the 45 degree position
  Yax.write(100);
  Xax.write(60);
  microservo.write(90);
  //1 second delay
  delay(1000);
}


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

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          //begins html code
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Bridges Program </TITLE>");
          //CSS style code for buttons
          client.println("<STYLE>");
          client.println(".button { ");
          client.println("background-color: #4CAF50;");
          client.println("border: none;");
          client.println("color: white;");
          client.println("padding: 15px 32px;");
          client.println("text-align: center;");
          client.println("display: inline-block;");
          client.println("font-size: 16px;");
          client.println("margin: 4px 2px;");
          client.println("cursor: pointer;");
          client.println("}");
          client.println("</STYLE>");
          //end of CSS code
          client.println("</HEAD>");
          client.println("<BODY>");
          //Page header
          client.println("<H1>Welcome to Robo-Farm!</H1>");
          client.println("<hr />");
          client.println("<br />");
          //Sub-heading
          client.println("<H2>Robotic Arm</H2>");
          client.println("<br />");
          //Creates buttons and corresponding URL extension
          client.println("<a href=\"/?button1on\"\" class = \"button\">Close Claw</a>");
          client.println("<a href=\"/?button1off\"\" class = \"button\">Open Claw</a><br />");
          client.println("<br />");
          client.println("<br />");
          //Creates buttons and corresponding URL extension
          client.println("<a href=\"/?button2on\"\" class = \"button\">Move forward</a>");
          client.println("<a href=\"/?button2off\"\" class = \"button\">Move Away</a><br />");
          client.println("<br />");
          client.println("<br />");
          //Creates buttons and corresponding URL extension to IP address
          client.println("<a href=\"/?button3on\"\" class = \"button\">Swivle left</a>");
          client.println("<a href=\"/?button3off\"\" class = \"button\" >Swivle right</a><br />");
          client.println("<br />");
          client.println("<br />");
          //Footer text
          client.println("<p>Created by Gafur & Santos.</p>");
          client.println("<br />");
          client.println("</BODY>");
          client.println("</HTML>");
          //end of html file
          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          //parses the URL for the corresponding extension
          if (readString.indexOf("?button1on") > 0 && microservo.read() <= 175) { // button1 on is close claw
            microservo.write(microservo.read() + 5);  // tell servo to go to position in variable 'pos'
            delay(15);                        // waits 15ms for the servo to reach the position
            Serial.println(cl + microservo.read());
          }
          if (readString.indexOf("?button1off") > 0&& microservo.read() >= 95) { // close claw
            microservo.write(microservo.read() - 5);  // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(cl +microservo.read());
          }
          if (readString.indexOf("?button2on") > 0 && Yax.read() < 150 ) { // button2 on is move forward on y ax (towards the servo on the claw)
            Yax.write(Yax.read() - 5);                // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(y + Yax.read());
          }
          if (readString.indexOf("?button2off") > 0&& Yax.read() > 70 ) {
            Yax.write(Yax.read() + 5);                // tell servo to go to position in variable 'pos'
            delay(15);                // waits 15ms for the servo to reach the position
            Serial.println(y + Yax.read());
          }
          if (readString.indexOf("?button3on") > 0 ) { // button3 is swivle in direction of sticker
            Xax.write(Xax.read() - 3);                // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(x + Xax.read());
          }
          if (readString.indexOf("?button3off") > 0) {
            Xax.write(Xax.read() + 3);                // tell servo to go to position in variable 'pos' S
            delay(15);                // waits 15ms for the servo to reach the position
            Serial.println(x + Xax.read());
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}


user313: Question 1 will need more information about your code and your wiring of the arm to offer any reasonable answer.
Question 2: There really isn't much to go on but I'll take a guess that you're sending a single message to the arm server on each button press - ? I'd try instead, sending another message every 't' starting at (.75 * t) from message-1, where 't' is the time taken for the arm to move 5 degrees. That's a totally wild guess based on unfounded assumptions of what's happening now, and what fraction of 't' I guess would make sense, but that would be my first, very experimental approach. And even so, given the non-determinism of internet packet arrival times, I wouldn't bet on getting very smooth motion, nor instant stopping on button release.
My next experiment would be send start moving/keep moving commands to the arm on button-press, stop moving on button-release, with the arm controller being smart enough to 1) stop anyway after some short period of no messages or it reaches its limit, and 2) not let commands accumulate (or the arm will keep driving until it limits).
Update:
Yes, if you're using a terminal emulator that has the ability to send each keystroke rather than line-by-line, and doesn't suppress key-repeat. As above, don't accumulate incoming keystrokes but when you're looking for another, drain the buffer first then watch for an incoming one. If you don't find it within <TIMEOUT>, stop moving. <TIMEOUT> should be slightly longer than the key-repeat interval.
(I like my second way better - winging it at the keyboard, heh! - but if it were me, I'd probably try both anyway just for what I could learn from either one).

7861: <p>Good day, I am currently attempting to write code that will allow a user to connect to an Arduino Mega via IP address and control a robotic arm on the X and Y axis as well as open and close the claw. I am facing 2 issues and hope someone can help me </p>

<ol>
<li>when it connects for the first few seconds the servos twitch and I'm not sure why</li>
<li>In my current code i am reading in the URL of the button pushed and moving the arm 5 degrees each time, but i would like to be able to hold the button down and have the arm move instead but I'm not sure how to go about that</li>
</ol>



<pre class="lang-cpp prettyprint-override"><code>/*
 Jamil Gaufr and Eva Santos
 Bridges Project @ SUNY Purchase
 Robotic Arm Control &amp; Interface
 */

#include &lt;SPI.h&gt;
#include &lt;Ethernet2.h&gt;
#include &lt;Servo.h&gt;

//Represents servo to open/close claw
Servo microservo;
//Represents servo to move forward/away
Servo Yax;
//Represents servo to swivel left/right
Servo Xax;

int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
EthernetServer server(80);                             //server port
String readString;
String x = "x =";
String y = "y = ";
String cl = "claw  = "; 


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Yax.attach(4);
  Xax.attach(5);
  microservo.attach(6);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //Initializes every axis to start at the 45 degree position
  Yax.write(100);
  Xax.write(60);
  microservo.write(90);
  //1 second delay
  delay(1000);
}


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() &lt; 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

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          //begins html code
          client.println("&lt;HTML&gt;");
          client.println("&lt;HEAD&gt;");
          client.println("&lt;TITLE&gt;Bridges Program &lt;/TITLE&gt;");
          //CSS style code for buttons
          client.println("&lt;STYLE&gt;");
          client.println(".button { ");
          client.println("background-color: #4CAF50;");
          client.println("border: none;");
          client.println("color: white;");
          client.println("padding: 15px 32px;");
          client.println("text-align: center;");
          client.println("display: inline-block;");
          client.println("font-size: 16px;");
          client.println("margin: 4px 2px;");
          client.println("cursor: pointer;");
          client.println("}");
          client.println("&lt;/STYLE&gt;");
          //end of CSS code
          client.println("&lt;/HEAD&gt;");
          client.println("&lt;BODY&gt;");
          //Page header
          client.println("&lt;H1&gt;Welcome to Robo-Farm!&lt;/H1&gt;");
          client.println("&lt;hr /&gt;");
          client.println("&lt;br /&gt;");
          //Sub-heading
          client.println("&lt;H2&gt;Robotic Arm&lt;/H2&gt;");
          client.println("&lt;br /&gt;");
          //Creates buttons and corresponding URL extension
          client.println("&lt;a href=\"/?button1on\"\" class = \"button\"&gt;Close Claw&lt;/a&gt;");
          client.println("&lt;a href=\"/?button1off\"\" class = \"button\"&gt;Open Claw&lt;/a&gt;&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          //Creates buttons and corresponding URL extension
          client.println("&lt;a href=\"/?button2on\"\" class = \"button\"&gt;Move forward&lt;/a&gt;");
          client.println("&lt;a href=\"/?button2off\"\" class = \"button\"&gt;Move Away&lt;/a&gt;&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          //Creates buttons and corresponding URL extension to IP address
          client.println("&lt;a href=\"/?button3on\"\" class = \"button\"&gt;Swivle left&lt;/a&gt;");
          client.println("&lt;a href=\"/?button3off\"\" class = \"button\" &gt;Swivle right&lt;/a&gt;&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          client.println("&lt;br /&gt;");
          //Footer text
          client.println("&lt;p&gt;Created by Gafur &amp; Santos.&lt;/p&gt;");
          client.println("&lt;br /&gt;");
          client.println("&lt;/BODY&gt;");
          client.println("&lt;/HTML&gt;");
          //end of html file
          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          //parses the URL for the corresponding extension
          if (readString.indexOf("?button1on") &gt; 0 &amp;&amp; microservo.read() &lt;= 175) { // button1 on is close claw
            microservo.write(microservo.read() + 5);  // tell servo to go to position in variable 'pos'
            delay(15);                        // waits 15ms for the servo to reach the position
            Serial.println(cl + microservo.read());
          }
          if (readString.indexOf("?button1off") &gt; 0&amp;&amp; microservo.read() &gt;= 95) { // close claw
            microservo.write(microservo.read() - 5);  // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(cl +microservo.read());
          }
          if (readString.indexOf("?button2on") &gt; 0 &amp;&amp; Yax.read() &lt; 150 ) { // button2 on is move forward on y ax (towards the servo on the claw)
            Yax.write(Yax.read() - 5);                // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(y + Yax.read());
          }
          if (readString.indexOf("?button2off") &gt; 0&amp;&amp; Yax.read() &gt; 70 ) {
            Yax.write(Yax.read() + 5);                // tell servo to go to position in variable 'pos'
            delay(15);                // waits 15ms for the servo to reach the position
            Serial.println(y + Yax.read());
          }
          if (readString.indexOf("?button3on") &gt; 0 ) { // button3 is swivle in direction of sticker
            Xax.write(Xax.read() - 3);                // tell servo to go to position in variable 'pos'
            delay(15);                                // waits 15ms for the servo to reach the position
            Serial.println(x + Xax.read());
          }
          if (readString.indexOf("?button3off") &gt; 0) {
            Xax.write(Xax.read() + 3);                // tell servo to go to position in variable 'pos' S
            delay(15);                // waits 15ms for the servo to reach the position
            Serial.println(x + Xax.read());
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}
</code></pre>
 313: <p>Question 1 will need more information about your code and your wiring of the arm to offer any reasonable answer.</p>

<p>Question 2: There really isn't much to go on but I'll take a guess that you're sending a single message to the arm server on each button press - ? I'd try instead, sending another message every 't' starting at (.75 * t) from message-1, where 't' is the time taken for the arm to move 5 degrees. That's a totally wild guess based on unfounded assumptions of what's happening now, and what fraction of 't' I guess would make sense, but that would be my first, very experimental approach. And even so, given the non-determinism of internet packet arrival times, I wouldn't bet on getting very smooth motion, nor instant stopping on button release.</p>

<p>My next experiment would be send start moving/keep moving commands to the arm on button-press, stop moving on button-release, with the arm controller being smart enough to 1) stop anyway after some short period of no messages or it reaches its limit, and 2) not let commands accumulate (or the arm will keep driving until it limits).</p>

<p>Update:<br>
Yes, if you're using a terminal emulator that has the ability to send each keystroke rather than line-by-line, and doesn't suppress key-repeat. As above, don't accumulate incoming keystrokes but when you're looking for another, drain the buffer first then watch for an incoming one. If you don't find it within &lt;TIMEOUT&gt;, stop moving. &lt;TIMEOUT&gt; should be slightly longer than the key-repeat interval.</p>

<p>(I like my second way better - winging it at the keyboard, heh! - but if it were me, I'd probably try both anyway just for what I could learn from either one).</p>
