Loops and Ethernet

Hello,
I’m working on an ethernet based rgb light. It’s quite simple right now. Basically, there are 3 buttons for turning each color of an RGB led on, and 3 for turning it off. The on buttons are supposed to fade the leds in and out. The problem is that the led’s won’t keep fading in and out unless they’re in a loop. When this loop is in place, the program won’t turn off the led because it’s still in the loop. How do i get around this?

Code is here:

 /*
 Created by Rui Santos
 Edited by Mihir Trivedi
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
int red = 6;
int blu = 5;
int grn = 3;
Servo microservo; 
int pos = 0; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // 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);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(red, OUTPUT);
  pinMode(grn, OUTPUT);
  pinMode(blu, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  digitalWrite(red, LOW);
  digitalWrite(blu, LOW);
  digitalWrite(grn, LOW);

  //delay(10000);
}


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("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn on red fade</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off red fade</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Turn on blue fade</a>");
           client.println("<a href=\"/?button2off\"\">Turn off blue fade</a>
");
           client.println("

");
           client.println("<a href=\"/?button3on\"\">Turn on green fade</a>");
           client.println("<a href=\"/?button3off\"\">Turn off green fade</a>
"); 
           client.println("


");
           client.println("<p>Created by Rui Santos. Visit &nbsp;<a href=http://randomnerdtutorials.com> RandomNerdTutorials </a>  &nbsp;for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               //digitalWrite(led, HIGH);
               fadeRed();
               Serial.println("ON Red");
           }
           
           if (readString.indexOf("?button1off") >0)
           {
               digitalWrite(red, LOW);
               Serial.println("OFF");
            }
            
           if (readString.indexOf("?button2on") >0)
           {
              fadeBlu();
               Serial.println("ON Blu");
            } 
           
           if (readString.indexOf("?button2off") >0){
                digitalWrite(blu,LOW);
               
           }
           
           if (readString.indexOf("?button3on") >0)
           {
              fadeGrn();
               Serial.println("ON Grn");
            } 
           
           if (readString.indexOf("?button3off") >0){
                digitalWrite(grn,LOW);
               
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

void fadeRed () 
{
  for(int fadeValue = 110 ; fadeValue >= 0; fadeValue -=2)
  { 
    analogWrite(red, fadeValue);         
    delay(70);                            
  }
  for (int fadeValue = 0;fadeValue <=110; fadeValue +=2)
  {
    analogWrite(red, fadeValue);
    delay(70);
  }
}

void fadeGrn () 
{
  
  for(int fadeValue = 110 ; fadeValue >= 0; fadeValue -=2)
  { 
    analogWrite(grn, fadeValue);         
    delay(70);                            
  }
  for (int fadeValue = 0;fadeValue <=110; fadeValue +=2)
  {
    analogWrite(grn, fadeValue);
    delay(70);
  }
}


void fadeBlu () 
{
  
  for(int fadeValue = 110 ; fadeValue >= 0; fadeValue -=2)
  { 
    analogWrite(blu, fadeValue);         
    delay(70);                            
  }
  for (int fadeValue = 0;fadeValue <=110; fadeValue +=2)
  {
    analogWrite(blu, fadeValue);
    delay(70);
  }

  
}

You could implement the millis() function to allow the script to continue running but seem to have a "delay" on the LEDs. For example:

int ledPin = 13; // or whatever pin your LED is on
int interval = 30; // the interval for changing the value of the LED
unsigned long prevTiming = 0;  // I used an unsigned long because this value will get very large
int val = 0;  // the first value for the LED
boolean state = HIGH;  // just to know if we're going up or down.

void loop() {
  if (millis() > prevTiming) {     // if we've waited the interval amount
    analogWrite(ledPin, val);   // write the value to the LED
    if(state) {     // if we're counting up
      val++;         // increment the LED value. You could set it to anything, I have it as 1.
    }
    else if(!state) {  // else if we're counting down
      val--;
    }
    prevTiming = millis() + interval;
  }
}

That should theoretically fade one led on and off. Just put that while you're reading from Ethernet as well and it would execute. Hope that helps

This concept is useful, but it unfortunately isn’t working in this case. It works if everything is in a loop, but here, there is no loop in the function fadeBlu(). It needs some way to check if there is a new input waiting from the webpage…any idea on how to do that while the loop is still running? i.e. the LED is still fading on and off?

What might work for you is to use loop() as your loop and inside of it

// Pseudocode – not actual code, just showing logical structure here
void loop()
{
static int colorLevel[ 3 ] = { 0, 0, 0 }; // R, G, B

for ( byte i = 0; i < 3; i++ )
{
if ( button i pressed )
{
// depending on the colorLevel, do something
}
}
}

You might need to add a timer to only check the buttons once every 10 to 100 millis just to slow it down a bit.