Loop inside loop that can be broken

Hi all,

I own an Uno r3 arduino board, an ethernet shield and what I am trying to do is to control the brightness of a 12V light with an NPN transistor from a webpage. I have created 5 buttons: 25% - 50% - 100% - Auto - Off. All buttons work successfully except the “Auto” one. I have connected a light sensor and I am trying to control the 12V light by reading the light sensor’s value.

#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module. 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 111 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

String inString = String(35);

String exitLightLow, exitLightMedium, exitLightHigh, exitLightAuto, exitLightOff;
int exitLightCur = 0;

int led[] = {00, 9  }; //Led pins num 0 in arry is not used
int numofleds = 1; //numofleds
String value[] = {"on","on","on","on","on","on"}; //startup all led are off

EthernetServer server(1234);
String data;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip,gateway,subnet); 
  server.begin();
 
  pinMode(9,OUTPUT);
  pinMode(0,INPUT);
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY");
}

void loop()
{
  EthernetClient client = server.available();
  
  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 (inString.length() < 35) {
            inString.concat(c);
         } 
        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");
          client.println();
          client.println("<html><body><form method=get>");
          client.println("<p><b>Control room</b></p>");
         
          
          exitLightLow    = String("Exit_light_25");
          exitLightMedium = String("Exit_light_65");
          exitLightHigh   = String("Exit_light_100");
          exitLightAuto   = String("Exit_light_Auto");
          exitLightOff    = String("Exit_light_Off");
           
          if(inString.indexOf(exitLightLow)>0){
            Serial.println(exitLightLow+" is on");
            if (exitLightCur>25){
                for (int k=exitLightCur; k>=25; k--){ 
                analogWrite(9,k); 
                delay(10);
                }
            }
            else if (exitLightCur<25){
                for (int k=exitLightCur; k<=25; k++){ 
                analogWrite(9,k); 
                delay(10);
                }
            }
            exitLightCur = 25;
            value[1] = "off"; 
          }
          else if(inString.indexOf(exitLightMedium)>0){
            Serial.println(exitLightMedium+"is on");
            if (exitLightCur>100){
                for (int k=exitLightCur; k>=100; k--){ 
                analogWrite(9,k); 
                delay(10);
                }
            }
            else if (exitLightCur<100){
                for (int k=exitLightCur; k<=100; k++){ 
                analogWrite(9,k); 
                delay(10);
                }
            }
            exitLightCur = 100;
            value[2] = "off"; 
          }
          else if(inString.indexOf(exitLightHigh)>0){          
            Serial.println(exitLightHigh+" is on");
            if (exitLightCur<254){
                for (int k=exitLightCur; k<=254; k++){ 
                analogWrite(9,k); 
                delay(10);
                }
            }
            exitLightCur = 254;
            value[3] = "off";
          } 
          else if(inString.indexOf(exitLightAuto)>0){          
            Serial.println(exitLightAuto+" is on");

                int k = 1023 - analogRead(0)/4; 
                analogWrite(9,k); 
                Serial.println(analogRead(0));
                delay(5);

            exitLightCur = 0;
            //value[4] = "off";
          } 
          else if(inString.indexOf(exitLightOff)>0){          
            Serial.println(exitLightOff+" is on");
              for (int k=exitLightCur; k>=0; k--){ 
              analogWrite(9,k); 
              delay(10);
              }
            exitLightCur = 0;
            value[4] = "off";
          } 
         
         
         client.println("
 Exit LED Light control:  <input type=submit name="+exitLightLow+" value=25%>");
         client.println(" <input type=submit name="+exitLightMedium+" value=60%>");
         client.println(" <input type=submit name="+exitLightHigh+" value=100%>");
         client.println(" <input type=submit name="+exitLightAuto+" value=Auto>");
         client.println(" <input type=submit name="+exitLightOff+" value=Off>");

         client.println("</from></html></body>");
          break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    inString = "";
    client.stop();
  }
}

The problem is that all “analogWrite(9,k);” values are being set as static ones, including “Auto”. When I press “Auto” I want the system to enter a loop that can be broken when another button is pressed.

Can you tell me your opinion ? Is there something you could suggest ?

Thanks
Chris

Why not just leverage the loop you already have and simply use a variable to keep track of whether it is in auto mode or not?

int var=0; switche (var){

case 1:

do your action

if(auto){ var=2; } break;

case 2:

do your action if(other...){var=1;} break; }

etc..

Skizo !

Probably best not to use "auto" as a variable name.

Arrch: Why not just leverage the loop you already have and simply use a variable to keep track of whether it is in auto mode or not?

nice idea.. must change the whole process..

skizoh: int var=0; switche (var){

case 1:

do your action

if(auto){ var=2; } break;

case 2:

do your action if(other...){var=1;} break; }

etc..

Skizo !

could switch...case break the loop when another button is pressed ? Usually when you press "Auto" (I will change that variable as suggested) it will loop and browser will seem loading infinitely..

hyp3rkyd: could switch...case break the loop when another button is pressed ?

That's so vague I can't answer it. Anywhere inside "loop" you can "return" which will exit the loop, and restart it from the start (because loop is called repeatedly).

Please don't just post vague pseudo-code and ask questions about it. Post real code so we know what you are talking about.

i meant who switch..case works for arduino as a command, not supposed to post some specific code. The whole scetch has been post earlier. Thanks for answering though.

hyp3rkyd: i meant who switch..case works for arduino as a command, not supposed to post some specific code.

I don't understand what that means.

PeterH:

hyp3rkyd: i meant who switch..case works for arduino as a command, not supposed to post some specific code.

I don't understand what that means.

i am sorry (auto correct..)

I asked about the "switch" statement and whether it will work for arduino or not... If using it will break the "chain" of the loop.

Arduino uses standard C++. So “switch” works in the usual way, if that answers your question.