0

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

  1. when it connects for the first few seconds the servos twitch and I'm not sure why
  2. 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 = "";

        }
      }
    }
  }
}
  • 1
    No, we can't help you. You have a problem with your code, yet you aren't showing us that code. We need to see that code if we are to have any hope of knowing what is happening with that code. Imagine if you wanted the garage to fix your car but decided to leave it at home instead of taking it to the garage - they would have a hard time fixing it... – Majenko Jun 23 '16 at 14:56
  • If you are using a webpage, obviously you cannot implement it with links (since links work by "reloading" the page). You will need to use AJAX and write some functions in Javascript to send multiple requests. – frarugi87 May 23 '17 at 13:31
1

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).

  • For wiring it on a keyboard is there any way to read in data/keystrokes and have a live stream being implemented in the arduino; Example press up the the hand moves up until i let it go? – user3233991 Jun 25 '16 at 1:53
  • In case I used an unfamiliar idiom, "Winging it" means doing something without a great deal of fore-thought or research, similar to "off the cuff", if that helps. In this case I meant I was thinking and forming new ideas, even as I was typing the first idea I had thought of. – JRobert Jun 25 '16 at 22:14
  • gotcha, sorry for that – user3233991 Jun 26 '16 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.