0% found this document useful (0 votes)
45 views2 pages

Servo Motor

This code controls a servo motor using an Arduino Ethernet shield. It initializes the Ethernet connection and sets the IP address. A web server listens for requests to change the servo angle. When a request is received, it extracts the angle parameter and moves the servo to that position. It returns a web page with a slider to control the servo angle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Servo Motor

This code controls a servo motor using an Arduino Ethernet shield. It initializes the Ethernet connection and sets the IP address. A web server listens for requests to change the servo angle. When a request is received, it extracts the angle parameter and moves the servo to that position. It returns a web page with a slider to control the servo angle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Ethernet.

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};


byte ip[] = {192, 168, 100, 50};
byte gateway[] = {192, 168, 100, 1};
byte subnet[] = {255, 255, 255, 0};

EthernetServer server(80);

Servo myservo; // Crear un objeto Servo

int currentAngle = 90; // Ángulo inicial

void setup() {
myservo.attach(9); // El pin 9 se utiliza para controlar el servomotor

Serial.begin(9600);

pinMode(9, OUTPUT); // Asegurarse de que el pin del servomotor esté configurado


como salida

// Iniciar Ethernet con la dirección IP y la configuración de red


Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}

void loop() {
EthernetClient client = server.available();
if (client) {
processRequest(client);
}

// Actualizar el ángulo del servomotor


myservo.write(currentAngle);
delay(15); // Pequeña pausa para permitir el movimiento del servomotor
}

void processRequest(EthernetClient client) {


String request = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n') {
break;
}
}
}

if (request.indexOf("GET /") != -1) {


int angleIndex = request.indexOf("angle=");

if (angleIndex != -1) {
int newAngle = request.substring(angleIndex + 6, angleIndex + 9).toInt();
if (newAngle >= 0 && newAngle <= 180) {
currentAngle = newAngle;
}
}

// Construir la página web con controles para el servomotor


String page = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
page += "<html><head><style>";
page += "body { font-family: 'Arial', sans-serif; background-color: #f2f2f2;
color: #333; }";
page += "h1 { color: #333; text-align: center; }";
page += "form { text-align: center; }";
page += "input[type=range] { width: 80%; margin: 10px 0; background-color:
#ddd; ";
page += "border: 1px solid #ddd; border-radius: 5px; }";
page += "#customButton { width: 40%; padding: 10px; background-color: #4CAF50;
";
page += "color: white; border: none; border-radius: 4px; cursor: pointer; ";
page += "transition: background-color 0.3s, opacity 0.3s; font-weight:
bold; }";
page += "#customButton:hover { background-color: #45a049; opacity: 0.8; }";
page += "</style></head><body>";
page += "<h1>Control del Servomotor</h1>";
page += "<form action='/' method='get'>";
page += "Ángulo: <input type='range' name='angle' min='0' max='180' ";
page += "value='" + String(currentAngle) + "'><span id='angleValue'>" +
String(currentAngle) + "</span><br>";
page += "<button id='customButton' type='submit'>Mover Servomotor</button>";
page += "</form>";
page += "<script>";
page += "function updateValue(slider)
{ document.getElementById('angleValue').innerText = slider.value; }";
page += "</script>";
page += "</body></html>";

client.print(page);
}

client.stop();
}

You might also like