Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1.  
  2. //--------------- Servo Motor -------------------------------------
  3.  
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6.  
  7. #include <Servo.h>
  8. Servo microservo;
  9. int pos = 0;
  10.  
  11. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  12. byte ip[] = { 192, 168, 1, 188 };
  13. byte gateway[] = { 192, 168, 1, 1 };
  14. byte subnet[] = { 255, 255, 255, 0 };
  15. EthernetServer server(8089);
  16. String readString;
  17.  
  18. void setup() {
  19. // Open serial communications and wait for port to open:
  20. Serial.begin(9600);
  21.  
  22. microservo.attach(7);
  23.  
  24.  
  25. Ethernet.begin(mac, ip, gateway, subnet);
  26. server.begin();
  27. Serial.print("server is at ");
  28. Serial.println(Ethernet.localIP());
  29. }
  30.  
  31.  
  32. void loop() {
  33. // Create a client connection
  34. EthernetClient client = server.available();
  35. if (client) {
  36. while (client.connected()) {
  37. if (client.available()) {
  38. char c = client.read();
  39.  
  40. //read char by char HTTP request
  41. if (readString.length() < 100) {
  42. //store characters to string
  43. readString += c;
  44. //Serial.print(c);
  45. }
  46.  
  47.  
  48. if (c == '\n') {
  49. Serial.println(readString);
  50.  
  51. client.println("HTTP/1.1 200 OK");
  52. client.println("Content-Type: text/html");
  53. client.println();
  54. client.println("<HTML>");
  55. client.println("<HEAD>");
  56. client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">");
  57. client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  58. client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
  59. //<!-- Optional theme -->
  60. client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css\">");
  61. //<!-- Latest compiled and minified JavaScript -->
  62. client.println("<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js\"></script>");
  63.  
  64.  
  65. client.println("</HEAD>");
  66. client.println("<BODY>");
  67. client.println("<center>");
  68. client.println("<H1>Arduino Servo Motor</H1>");
  69. client.println("<hr />");
  70. client.println("<br />");
  71.  
  72. client.println("<br />");
  73. client.println("<br />");
  74. client.println("<a href=\"/?button2on\" class=\"btn btn-primary\">SERVO</a>");
  75. client.println("<a href=\"/?button2off\" class=\"btn btn-primary\">SERVO</a>");
  76. client.println("<br />");
  77. client.println("<br />");
  78. client.println("<br />");
  79. client.println("<br />");
  80.  
  81. client.println("<br />");
  82.  
  83. client.println("<br />");
  84. client.println("<br />");
  85. client.println("<br />");
  86. client.println("</center>");
  87. client.println("</BODY>");
  88. client.println("</HTML>");
  89.  
  90. delay(1);
  91.  
  92. client.stop();
  93. //controls the Arduino if you press the buttons
  94.  
  95.  
  96. if (readString.indexOf("?button2on") > 0) {
  97. for (pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
  98. { // in steps of 1 degree
  99. microservo.write(pos); // tell servo to go to position in variable 'pos'
  100. delay(15); // waits 15ms for the servo to reach the position
  101. }
  102. }
  103. if (readString.indexOf("?button2off") > 0) {
  104. for (pos = 180; pos >= 1; pos -= 3) // goes from 180 degrees to 0 degrees
  105. {
  106. microservo.write(pos); // tell servo to go to position in variable 'pos'
  107. delay(15); // waits 15ms for the servo to reach the position
  108. }
  109. }
  110. //clearing string for next read
  111. readString = "";
  112.  
  113. }
  114. }
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement