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 = "";
}
}
}
}
}