Ethernet control checkbox wont unchecked

Hi All,

I want to code my Uno and Ethernet Shield to light up an LED.
The input to light up my LED is a checkbox. The initial setup of my arduino is

boolean LED_status = 0;
and
pinMode(2, OUTPUT);

Currently, turning ON the LED from its initial setup is not a big of a problem, the checkbox is unchecked to checked.

But when I’m about to turn OFF the LED, the problem is once I press the checkbox again, it doesn’t unchecked the checkbox and the LED continues to light up.

Here is my code for the checkbox:

 if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
    }

Code for URL:

    if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }

Expected URL result to:
LED OFF: http//:IP Adress
LED ON: http//:IP Adress/?LED2=2
LED OFF: http//:IP Adress

Current progress:
LED OFF: http//:IP Adress
LED ON: http//:IP Adress/?LED2=2
LED OFF (but the LED kept turning ON and the box is still checked): http//:IP Adress/?

Which part of my code is wrong? :confused:

Hi All,

I want to code my Uno and Ethernet Shield to light up an LED.
The input to light up my LED is a checkbox. The initial setup of my arduino is

boolean LED_status = 0;
and
pinMode(2, OUTPUT);

Currently, turning ON the LED from its initial setup is not a big of a problem, the checkbox is unchecked to checked.

But when I’m about to turn OFF the LED, the problem is once I press the checkbox again, it doesn’t unchecked the checkbox and the LED continues to light up.

Here is my code for the checkbox with html:

    if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
    }

Code for URL and LED status:

    if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }

Expected URL result to:
LED OFF: http//:IP Adress
LED ON: http//:IP Adress/?LED2=2
LED OFF: http//:IP Adress

Current progress:
LED OFF: http//:IP Adress
LED ON: http//:IP Adress/?LED2=2
LED OFF (but the LED kept turning ON and the box is still checked): http//:IP Adress/?

Which part of my code is wrong? :confused:

DON'T CROSS POST!!!!!!!!!!!!!!!!!!!!

I HAVE REPORTED THIS THREAD TO THE MODERATORS

if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }

I'm doing a bit of guessing here since you didn't post all the code. If this isn't the answer you're looking for then please post a complete example that compiles and exhibits the problem.

So the bit that toggles the LED_status variable is inside and if statement checking for "LED2=2". It looks like once the box is checked, when you click it then it sends the url without "LED2=2" so you don't go into that if so you don't toggle the LED_status variable, so when you get to the first bit of code there, LED_status is still true and you leave the light on and send the html back with the box checked.

Maybe the else in the bit of code I quoted should go with the first if and not the second one.

Threads merged.