Fade-loop stops on webserver

Hi Guys!

Im kind of new to Arduino as a webserver. I have made the simple tutorials, as turning a LED on/off. All of it is working fine.

But i want to made a LED fade up and down continuously if i push i button in the browser. Here is my problem. When i push the button the LED turns on, and steps up very slow. It appears to do one step every time the site refreshes (5 secs)

How do i do it??

#include <WebServer.h>

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

// ethernet configuration
byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte ip[] = { 192, 168, 1, 111 }; // P1 --> { 10, 1, 1, 5 };
EthernetServer server(80);              // port 80 is default for HTTP

// initial
int LED = 3;          // led is connected to digital pin 3
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

int LDR = 5;          // LDR sensor is connected to analog in 5

float photocell = 0;  // variable for photocell (LDR) analog value
char c = 0;           // received data
char command[2] = "\0";  // command

void setup()
  Ethernet.begin(mac, ip);
  pinMode(LED, OUTPUT);


void loop()
  EthernetClient client = server.available();
  // detect if current is the first line
  boolean current_line_is_first = true;

  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");

          // auto reload webpage every 5 second
          client.println("<META HTTP-EQUIV=REFRESH CONTENT=5 URL=>");
          // webpage title
          client.println("<center><p><h1>LoveHeart <3</h1></p><center><hr>

          // read analog pin 1 for the value of photocell
          photocell = analogRead(LDR);
          client.print("<p><h2>Light reading = <font color=indigo>");
          client.println(photocell, 2);
          // read digital pin 13 for the state of PIR sensor
          // button functions
          client.println("<form  method=get name=form>");
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px>LED On</button>");
          client.println("<button name=b value=2 type=submit style=height:80px;width:150px>LED Off</button>");
          // webpage footer
          client.println("<hr><center>LoveHeart by kucza
          client.println("<p>P.S.: This page will automatically refresh every 5 seconds.</p></center>");
        if (c == '\n') {
          // we're starting a new line
          current_line_is_first = false;
          current_line_is_blank = true;
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        // get the first http request
        if (current_line_is_first && c == '=') {
          for (int i = 0; i < 1; i++) {
            c = client.read();
            command[i] = c;
          // LED control
          if (!strcmp(command, "1")) {
          else if (!strcmp(command, "2")) {
            digitalWrite(LED, LOW);
    // give the web browser time to receive the data
void fade() {
              analogWrite(LED, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  // wait for 30 milliseconds to see the dimming effect    


It appears to do one step every time the site refreshes (5 secs)

That’s because that is exactly what is does.

How do i do it??

Use something other that a web browser as the driver. The client/server (stateless) interface is not what you seem to want. Trying to impose your requirements on that interface is going to be an exercise in frustration.


I have tried different thing now. But nothing seems to work. Can someone point me in the right direction, maybe a tutorial?

Making the Arduino start to fade an LED in response to a web request is trivial. You need to separate the fading from the web server processing, though.

Get the call to fade() out of the if(client) block. Get rid of the delay() call in fade. Use millis(), as shown in the blink without delay example, instead.