Bonjour j'ai un projet sur un serveur web avec un commande de servomoteur sauf que ce dernier ne s'arrête pas lors de sa rotation.
Vu que je suis débutant j'ai essayé beaucoup de chose en vain, je demande donc votre aide.
Voici mon programme :
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp2.h>
#include <SPI.h>
#include <Servo.h>
int led = 4, role = 2
;
Servo microservo;
int pos = 0;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x2E, 0x98 }; //physical mac address
byte ip[] = { 10, 134, 97, 13 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(role, OUTPUT);
microservo.attach(9);
microservo.writeMicroseconds(1500); // mettre le servomoteur à l'arrêt (1500 = 1.5 ms)
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
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();
client.println("");
client.println("");
client.println("<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">");
client.println("");
client.println("<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
//
client.println("<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css\">");
//
client.println("<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js\"></script>");
client.println("Sécurisation automatique d'une maison individuelle");
client.println("");
client.println("");
client.println("");
client.println("
Commande de la maison sur Arduino
");client.println("
");
client.println("
");
client.println("
Gestion de la maison
");client.println("
");
client.println("<a href="/?button1on" class="btn btn-success">Allumer la lumière");
client.println("<a href="/?button1off" class="btn btn-danger">Éteindre la lumière");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("<a href="/?button2on" class="btn btn-primary">Ouvrir les volets");
client.println("<a href="/?button2off" class="btn btn-primary">Fermer les volets");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("
");
client.println("");
client.println("");
client.println("");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") > 0) {
digitalWrite(led, HIGH);
}
if (readString.indexOf("?button1off") > 0) {
digitalWrite(led, LOW);
}
if (readString.indexOf("?button2on") > 0) {
for (pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
microservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if (readString.indexOf("?button2off") > 0) {
for (pos = 180; pos >= 1; pos -= 3) // goes from 180 degrees to 0 degrees
{
microservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
//clearing string for next read
readString = "";
}
}
}
}
}