Issue with LEDs toggling on Webserver

Hi!

I'm trying to get a simple webserver going for just controlling a few LEDs, but for some reason the LEDs are acting strange and I can't figure out why...

The problem is that when I press the button on the webpage to turn it on/off, it always toggles 1 time before going into the state it's supposed to be. So if it's off and I try to turn it on, it will go like this 'Off - On - Off - On' and vice versa.

I hope my code isn't too messy, as I'm still new to this :slight_smile:

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

#include <SPI.h>



int YLW = LOW;
int Cyan = LOW;
int Buzz = 6;
int freq;
int Green = LOW;

int lysSensor;
const int tempSensor = A1;

boolean Maal = false;

boolean receiving;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0xC2, 0xC1};


IPAddress ip(158,39,162,161);
EthernetServer server(80);

void setup() {

    Serial.begin(9600);

    pinMode(2,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(3,OUTPUT);

   if(Ethernet.begin(mac) == 0){
    Serial.println("Failed to configure Ethernet using DHCP");

    Ethernet.begin(mac, ip);
   }

  delay(1000);

  server.begin();
  Serial.print("server ia at: ");
  Serial.println(Ethernet.localIP());

  Serial.println("End setup()");

}

void loop() {
  Serial.println("Start av loop()");



  EthernetClient client = server.available();

  if(client){
    Serial.println("Cleint == true --> new client");

     boolean currentLineIsBlank = false;
     
     
     while (client.connected()){
      
     if (client.available()){
        char c = client.read();
        Serial.write(c);
        
        if(receiving && c == ' '){
          receiving = false;
        }

        if(c == '?'){
          receiving = true;
        }
          
         if(receiving){
          
           
           if (c == 'Y'){
            
             YLW = client.parseInt(); // Plukker
             digitalWrite(YLW, !digitalRead(YLW));
 
             //receiving = false;
             }

             else if( c == 'L'){

              Cyan = client.parseInt();
              digitalWrite(Cyan, !digitalRead(Cyan));
              receiving = false;
              
             }

             
           else if(c == 'S'){
              freq = client.parseInt();
              tone(Buzz,freq);
              if(freq == 0){
                noTone(6);
              }
            }

           else if(c == 'M'){
            Green = client.parseInt();
              digitalWrite(Green, !digitalRead(Green));
            }
            
         }

        
 
     if (c == '\n' && currentLineIsBlank){
       client.println("HTTP/1.1 200 OK");
       client.println("Content-Type: text/html");
       client.println("Connection: close");

       //client.println("Refresh: 5");
       client.println();
       
       client.println("<!DOCTYPE HTML>");
       client.println("<html><head><title>Lab 7</title>");
       client.println("<meta charset=""UTF-8"">");
       client.println("</head><body>");

        client.println("<h1>Lab 7 </h1>
");
        client.println("<p><FONT face=Verdana color= red size=2> <b>Arduino Uno</p></b>"); 

        //Form for lys med bruk av knapp på nett.
        client.println("<form action='' method='get'>");
        client.println("<input type='hidden' name='Y' value='2' />");
        client.println("<input type='submit' value='Gult LED on/off' />");
        client.println("</form>");


        client.println("<form action='' method='get'>");
        client.println("<input type='hidden' name='L' value='5' />");
        client.println("<input type='submit' value='Blå LED on/off' />");
        client.println("</form>");


          
         //Form for Sensorer
        client.println("<form action='' method='get'>");
        client.println("<input type='hidden' name='M' value='3' />");
        client.println("<input type='submit' value='Sensor on/off' />");
        client.println("</form>");
        
        client.println("");

         client.println("<form action='' method='get'>");
         client.print("<input type='range' name='S' min='0' max='4000' step='100' value='0'/>");
         client.println("<input type='submit' value='Set Frequency' />");
         client.println("</form>");

         client.println("</body></html>");
        break;
     }



      if (c == '\n'){
   
      currentLineIsBlank = true;
        }

      else if (c != '\r'){
       
       currentLineIsBlank = false;
       }

     }
     
    }

     delay(1);
     // close the connection:
     client.stop();
     Serial.println("client disconnected i if-setning");
     Serial.println("End loop() i if-setning");
     Serial.println("\n\n");
     } // end if (client)
     Serial.println("Ingen client tar kontakt. End loop()");
     Serial.println("\n");
}

Is your web browser requesting any other files, like favicon.ico? Could that cause the toggling?

Why do you have four forms? A form can have multiple items in it.

What DO the GET requests look like?