Need help shrinking a code

I’am new to Arduino and c++

i get his code from the INTERNET

void setup() {     
  Serial.begin(57600);  
  pinMode(13, OUTPUT);
}
int f = 0, pos;
void loop() {
  boolean has_request = false;
  String in = "";
  if (Serial.available()) {
    in = "";
    while (true) {  // should add time out here
      while (Serial.available() == false) {}
      in += (char)(Serial.read());
      if (in.endsWith("\r\n\r\n")) {
        has_request = true;  break;
      } 
    }
  }
  if (has_request) {
    int i1 = in.indexOf("GET /blink?f="), i2;
    if (i1 != -1) {
      i2 = in.indexOf(" ", i1+13);
      f = in.substring(i1+13, i2).toInt();
    }
    Serial.println("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close");
    String sr = "<!DOCTYPE HTML>\n";
    sr += "<html>\n";
    sr += "LED frequency: ";
    sr += f;
    sr += "Hz.</html>";
    Serial.print("Content-Length: ");
    Serial.print(sr.length());
    Serial.print("\r\n\r\n");
    Serial.print(sr);
    has_request = false;
  }
  if (f>0) {
    static unsigned long t = millis();
    if (millis() > t + 1000/f) {
      digitalWrite(13, 1-digitalRead(13));
      t = millis();
    }
  }
}

this code is used for the Arduino uno wifi shield
it is working perfectly except it is too complicated for me
while loop inside a while loop

and i don’t want to do frequency i only need to send turn on or off
without any complexity

anyone could help me ?

it is working perfectly

Nonsense.

      while (Serial.available() == false) {}

Serial.available() returns the number of bytes available to be read. It does NOT return true or false.

and i don't want to do frequency i only need to send turn on or off

Perhaps you should post an example of the GET request you want to deal with, not a bunch of hand-waving.

i want to send numbers to the arduino (0 or 1) if it is 0 then turn off the led on pin 13 if 1 then turn on

can you help me with this code ?

PaulS:      while (Serial.available() == false) {}

Serial.available() returns the number of bytes available to be read. It does NOT return true or false.

Suspect I'm about to learn something here, but doesn't the equivalency of false and '0' mean that this code effectively will proceed with the while loop when serial.available() returns '0'? I haven't read the code, but I can see how this might be useful and valid in some cases ...

I haven’t read the code, but I can see how this might be useful and valid in some cases …

How does testing for false seem any more useful than testing for < 1? The function returns a number. That the number is the same as the value of false is immaterial. It is also the same as (A0 - 14) on the Uno. But, does comparing the return value to (A0 - 14) or LOW or 3.14159/4 make any sense?

PaulS:
How does testing for false seem any more useful than testing for < 1?

Depends on the context, but in general I’ll agree it is better to use < and > over ==. But I’m not sure of the specific relevance to my post (that was in reply to yours).

The function returns a number. That the number is the same as the value of false is immaterial.

It’s not immaterial to the condition in the OP’s while loop. Please address what I’ve suggested directly, are you saying I’m wrong?
The way I read that code in english is:
“if there are 0 bytes of serial data in the buffer then proceed with the code in this while loop”
That is valid logic, and may be of use in some cases.
In this case it’s saying do nothing - i.e. effectively wait/block until there is data.

I recall doing it differently when I was using serial comms…

It is also the same as (A0 - 14) on the Uno. But, does comparing the return value to (A0 - 14) or LOW or 3.14159/4 make any sense?

Not quite sure what you’re saying here. What is ‘it’ ? return value to (A0 - 14) ?? 3.14159/4 ?? ?
But I fear we’re just going to take this thread away from the OP’s initial concern…

1:1: The way I read that code in english is: "if there are 0 bytes of serial data in the buffer then proceed with the code in this while loop"

Which is exactly what it does. The big thing to remember is all comparisons of primitive types are done in the integer domain. Converting a bool to an integer isn't any less logical than converting an integer to a bool. E.g:

if( Serial.available() ){ }

Your presumptions are correct and there is not 'immaterial' to the current solution.

@fraizor, this link may shed some light (looks like simpler code) it is for the Ethernet shield, however the app structure looks helpful:

http://www.instructables.com/id/Arduino-WebServer-controlled-LED/

fraizor:
I’am new to Arduino and c++

i get his code from the INTERNET

void setup() {     

Serial.begin(57600); 
  pinMode(13, OUTPUT);
}
int f = 0, pos;
void loop() {
  boolean has_request = false;
  String in = “”;
  if (Serial.available()) {
    in = “”;
    while (true) {  // should add time out here
      while (Serial.available() == false) {}
      in += (char)(Serial.read());
      if (in.endsWith("\r\n\r\n")) {
        has_request = true;  break;
      }
    }
  }
  if (has_request) {
    int i1 = in.indexOf(“GET /blink?f=”), i2;
    if (i1 != -1) {
      i2 = in.indexOf(" ", i1+13);
      f = in.substring(i1+13, i2).toInt();
    }
    Serial.println(“HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close”);
    String sr = “\n”;
    sr += “\n”;
    sr += "LED frequency: ";
    sr += f;
    sr += “Hz.”;
    Serial.print(“Content-Length: “);
    Serial.print(sr.length());
    Serial.print(”\r\n\r\n”);
    Serial.print(sr);
    has_request = false;
  }
  if (f>0) {
    static unsigned long t = millis();
    if (millis() > t + 1000/f) {
      digitalWrite(13, 1-digitalRead(13));
      t = millis();
    }
  }
}




this code is used for the Arduino uno wifi shield
it is working perfectly except it is too complicated for me 
while loop inside a while loop 

and i don't want to do frequency i only need to send turn on or off
without any complexity 

anyone could help me ?

This code monitors the serial input for a “request” string and with the exception in lacking safety timeout ( could wait forever for incoming data ) , as noted, works OK.

Can you be more descriptive on this

“need to send turn on or off”

Turn on or off - what?
Send what to where ?

Vaclav: This code monitors the serial input for a "request" string and with the exception in lacking safety timeout ( could wait forever for incoming data ) , as noted, works OK.

Can you be more descriptive on this

"need to send turn on or off"

Turn on or off - what? Send what to where ?

i just need to turn on or off the led i dont need blinking or frequency if i sent 1 the led must turn on if send 0 the led must turn off

how can i dot that ?

That code needs to be scrapped!.

  1. Never use while loops in that way it stops you from doing any thing else. Read this http://forum.arduino.cc/index.php?topic=223286.0

  2. Do not use the String class on an Arduino!

Mark

This kind of thing?:

void loop()
{   
    if( Serial.available() > 0 )
    {
        char read_char = Serial.read();
        switch( read_char )
        {
            case '1':
            {
                DigitalWrite( 13, HIGH );
            }
            break;
            case '0':
            {
                DigitalWrite( 13, LOW );
            }
            break;
        }
    }
}

Yep

Mark

I was trying to get across to people who do not read well and passed an unwarranted judgement on code they obviously do not understand - including worn out negative comment on "string" class.

For loop function to work as is the serial input EXPECTS specific string from a source.

That string could be "1" or "0" to take an advantage of the existing code. Plain if(Serial.available()) will work,but I hope the OP realize that he has to have some means to get the input to the serial port, never mid the rest of the code.

So - to the OP - how do you send (ASCII character ) 1 or 0 to the serial port?

" if i sent 1 the led must turn on if send 0 the led must turn off ??

including worn out negative comment on "string" class.

Crap - It's "String" not "string"(sic) but even so the advice to avoid it can never be worn out! Nor can the advice to avoid goto's delay's etc ever be worn out. Nor is it negative !, IT IS HOWEVER GOOD POSITIVE ADVICE that needs to be repeated.

Mark