Optimisation éviter les répétitions

Bonjour
je suis totalement débutant ,j’ai pris un code sur le net et je l’ai modifier pour qu il colle a ce que je voulais mais il est “dégueu” ( y a des répétition partout et ça pose un problème mémoire ) (mais il vas)
je me demandais si faire un tableau ou quelque chose dans le genre ne pouvait pas régler le problème

en gros c’est un uno + shield ethernet qui commandes des relais via une page web
ça marche , mais seulement si je “commente” “2 relais ou plus”

* Rui Santos 
 * Complete Project Details http://randomnerdtutorials.com
*/

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x33 };
IPAddress ip(192,168,0, 33);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

// Relay state and pin
String relay1State = "Off";
String relay2State = "Off";
String relay3State = "Off";
String relay4State = "Off";
String relay5State = "Off";
String relay6State = "Off";
String relay7State = "Off";
String relay8State = "Off";
const int relay1 = 2;
const int relay2 = 3;
const int relay3 = 4;
const int relay4 = 5;
const int relay5 = 6;
const int relay6 = 7;
const int relay7 = 8;
const int relay8 = 9;

// Client variables 
char linebuf[80];
int charcount=0;

void setup() { 
  // Relay module prepared 
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);
  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);
  pinMode(relay4, OUTPUT);
  digitalWrite(relay4, HIGH);
  pinMode(relay5, OUTPUT);
  digitalWrite(relay5, HIGH);
  pinMode(relay6, OUTPUT);
  digitalWrite(relay6, HIGH);
  pinMode(relay7, OUTPUT);
  digitalWrite(relay7, HIGH);
  pinMode(relay8, OUTPUT);
  digitalWrite(relay8, HIGH);
  
  // Open serial communication at a baud rate of 9600
  Serial.begin(9600);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

// Display dashboard page with on/off button for relay
// It also print Temperature in C and F
void dashboardPage(EthernetClient &client) {
  client.println("<!DOCTYPE HTML><html><head>");
  client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");                                                             
  client.println("<h3>Multiprises Salon - <a href=\"/\">Refresh</a></h3>");
  // Generates buttons to control the relay
  client.println("<h4>Relay 1 - State: " + relay1State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay1State == "Off"){
    client.println("<a href=\"/relay1on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay1State == "On"){
    client.println("<a href=\"/relay1off\"><button>OFF</button></a>"); 
    }
    //2

    client.println("<h4>Relay 2 - State: " + relay2State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay2State == "Off"){
    client.println("<a href=\"/relay2on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay2State == "On"){
    client.println("<a href=\"/relay2off\"><button>OFF</button></a>"); 
    }
    //3

    client.println("<h4>Relay 3 - State: " + relay3State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay3State == "Off"){
    client.println("<a href=\"/relay3on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay3State == "On"){
    client.println("<a href=\"/relay3off\"><button>OFF</button></a>"); 
    }
    //4

    client.println("<h4>Relay 4 - State: " + relay4State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay4State == "Off"){
    client.println("<a href=\"/relay4on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay4State == "On"){
    client.println("<a href=\"/relay4off\"><button>OFF</button></a>");   
  }
  
    //5
    client.println("<h4>Relay 5 - State: " + relay5State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay5State == "Off"){
    client.println("<a href=\"/relay5on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay5State == "On"){
    client.println("<a href=\"/relay5off\"><button>OFF</button></a>"); 
    }
    //6
    client.println("<h4>Relay 6 - State: " + relay6State + "</h4>");
  // If relay is off, it shows the button to turn the output on          
  if(relay6State == "Off"){
    client.println("<a href=\"/relay6on\"><button>ON</button></a>");
  }
  // If relay is on, it shows the button to turn the output off         
  else if(relay6State == "On"){
    client.println("<a href=\"/relay6off\"><button>OFF</button></a>"); 
   }
  
  client.println("</body></html>"); 
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
       char c = client.read();
       //read char by char HTTP request
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          dashboardPage(client);
          break;
          }
          //1
        if (c == '\n') {
          if (strstr(linebuf,"GET /relay1off") > 0){
            digitalWrite(relay1, HIGH);
            relay1State = "Off";
          }
          else if (strstr(linebuf,"GET /relay1on") > 0){
            digitalWrite(relay1, LOW);
            relay1State = "On";
          }
          //2
          if (strstr(linebuf,"GET /relay2off") > 0){
            digitalWrite(relay2, HIGH);
            relay2State = "Off";
          }
          else if (strstr(linebuf,"GET /relay2on") > 0){
            digitalWrite(relay2, LOW);
            relay2State = "On";
          }
//3
          if (strstr(linebuf,"GET /relay3off") > 0){
            digitalWrite(relay3, HIGH);
            relay3State = "Off";
          }
          else if (strstr(linebuf,"GET /relay3on") > 0){
            digitalWrite(relay3, LOW);
            relay3State = "On";
          }
//4
          if (strstr(linebuf,"GET /relay4off") > 0){
            digitalWrite(relay4, HIGH);
            relay4State = "Off";
          }
          else if (strstr(linebuf,"GET /relay4on") > 0){
            digitalWrite(relay4, LOW);
            relay4State = "On";
          }
//5
          if (strstr(linebuf,"GET /relay5off") > 0){
            digitalWrite(relay5, HIGH);
            relay5State = "Off";
          }
          else if (strstr(linebuf,"GET /relay5on") > 0){
            digitalWrite(relay5, LOW);
            relay5State = "On";
          }
          //6
          if (strstr(linebuf,"GET /relay6off") > 0){
            digitalWrite(relay6, HIGH);
            relay6State = "Off";
          }
          else if (strstr(linebuf,"GET /relay6on") > 0){
            digitalWrite(relay6, LOW);
            relay6State = "On";
          }
      






          
          // you're starting a new line
          currentLineIsBlank = true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;          
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

hello
regarde ICI

merci c'est mieux comme ca

Bonjour, supperdd

supperdd:
Bonjour

je me demandais si faire un tableau ou quelque chose dans le genre ne pouvait pas régler le problème

Tout à fait :
_un tableau pour les relais
_une fonction ( avec pour paramètre l’index du relai) pour le traitement des relais

et des boucles for :
“”“for (relai = 0 ; relai < nbrelaimax ; relai++ ) {… }”""

Cordialement,
bidouilleelec

super merci pour ta réponse rapide je vais essayer de mètre ça en place des que possible