Question About Loops.....

ok so im trying to write some code which basically pings a server and waits for a responce.

ive got something from a code libary which works fine.

however, i need my code to wait for x amount of failed pings or x amount of time then run another function.

im using the IMCPPing.h libary

here is what i have so far

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(12,11,5,4,3,2);      // tell lcd module where pins being
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set mac address for ethernet shield
    byte ip[] = {192,168,2,177}; // set desired ip address for shield
    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    int const  plugRelay =8;
    SOCKET pingSocket = 0;
     
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
     Ethernet.begin(mac, ip);  // start Ethernet
      lcd.begin(16,2);  // start lcd
      Serial.begin(9600);  // 
      lcd.setCursor(0,0);  // set lcd to print to line 1
      lcd.print("   Power-Ping   ");  // message to print
//      delay(5000);  // delay for message
      lcd.clear();  // clear lcd
      lcd.print("Power    |    ON"); // print
      lcd.setCursor(0,0);  // let lcd to print to line 2
      lcd.print("Ping "); // print
    }
     
    void loop()
    {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
        sprintf(buffer,
                "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
                echoReply.data.seq,
                echoReply.addr[0],
                echoReply.addr[1],
                echoReply.addr[2],
                echoReply.addr[3],
                REQ_DATASIZE,
                millis() - echoReply.data.time,
                echoReply.ttl);
      }
      else   // if no success the report fail message
      {
        sprintf(buffer, "Echo request failed; %d", echoReply.status);   // failed message
      }
      Serial.println(buffer);
      delay(500);  // 0.5 sec delay
    }

now this is what i think i need to is change the loop to the following but im not 100% if this is going to work

what should happen is the code should check for 3 errors, then
change the state of a input_pullup

      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
        sprintf(buffer,
                "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
                echoReply.data.seq,
                echoReply.addr[0],
                echoReply.addr[1],
                echoReply.addr[2],
                echoReply.addr[3],
                REQ_DATASIZE,
                millis() - echoReply.data.time,
                echoReply.ttl);
      }
      else   // if no success the report fail message
      {
   for(int j = 1; j  < 3; j++) // if ping fails 3times the change state of relay
     {
        sprintf(buffer, "Echo request failed; %d", echoReply.status);   // failed message
      }
      Serial.println(buffer);
      delay(500);  // 0.5 sec delay
      pinMode(plugRelay, INPUT_PULLUP);
    }

thanks and hopefull this makes sence

now this is what i think i need to is change the loop to the following but im not 100% if this is going to work

What loop? The code does not loop, except that loop() gets called over and over.

And, no, pinging once and printing the message three times is not going to do what you think it will.

You need to create a for loop that tries to ping n times. Set a flag to false before the loop, and break out of the for loop when the ping is successful, after setting a flag to true.

After the loop, if the flag is true, the ping was successful once, in up to n tries. Otherwise, it failed all n times.

You can even track the number of pings needed to get through.

Changing the mode of a pin on every pass through loop() is silly.

ok this sounds quite complex, i dont know how setting flags work.

could you point me to any tutorials ?

Regards & Thanks once again for the fast responce.

Use a boolean variable as a flag. Give it a meaningful name, perhaps, pingSuccess, and set it to true or false as appropriate. Test its value later like this

if (pingSuccess == true)
{
  //code to execute when ping was successful
}

or more succinctly

if (pingSuccess)
{
  //code to execute when ping was successful
}

so i assume id place the follwoing code the in void loop() section of my code.

if (pingSuccess)
{
  //code to execute when ping was successful
}

ive never used a boolean before (new too programming) ive read loads apon loads of idiots guides to programming, is a boolean something that can have either a true or false statement to it ?

do i declare a boolean in the same part of my code where i declare all my inputs/outputs ?

boolean pingSuccess = false;

thanks to everyone helping me i really appreachate it

do i declare a boolean in the same part of my code where i declare all my inputs/outputs ?

Let me ask a different question. Which is faster? Firing up the IDE and trying something for yourself, or posting a question here and waiting for an answer.

im constantly working on the arduino trying bits and peices and researching online.

im not here looking for people to write code for me, dont get me wrong if ive tried everything i can think of and google is not no help then yes im more then happy for someone to show me where im going wrong.

problem ive got is not yet recieved my ethernet shield threw the post so i cant physically test what im writing, what i dont want to do is write what i can to find out it don't work.

ok so this is what ive modified my code to… (hopefully this is not far off what im trying to get it to do.

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(12,11,5,4,3,2);      // tell lcd what pins are being used to control lcd
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set mac address for ethernet port
    byte ip[] = {192,168,2,177}; // set desired ip address ethernet port
    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    const int plugRelay = 7;
    boolean pingSuccess = false;
    SOCKET pingSocket = 0;
     
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
      digitalWrite(plugRelay, HIGH);
      Ethernet.begin(mac, ip);  // start Ethernet
      lcd.begin(16,2);  // start lcd
      Serial.begin(9600);  // 
      lcd.setCursor(0,0);  // set lcd to print to line 1
      lcd.print("   Power-Ping   ");  // message to print
//      delay(5000);  // delay for message
      lcd.clear();  // clear lcd
      lcd.print("Power    |    ON"); // print
      lcd.setCursor(0,0);  // let lcd to print to line 2
      lcd.print("Ping "); // print
    }
     
    void loop()
    {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
   
      {
        sprintf(buffer,
                "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
                echoReply.data.seq,
                echoReply.addr[0],
                echoReply.addr[1],
                echoReply.addr[2],
                echoReply.addr[3],
                REQ_DATASIZE,
                millis() - echoReply.data.time,
                echoReply.ttl);
                pingSuccess = !pingSuccess; // toggle pingSuccess variable
      }
      else   // if no success the report fail message
      {

        if (pingSuccess)
{
  lcd.print("NOT SUCCESSFUL");
}
      sprintf(buffer, "Echo request failed; %d", echoReply.status);   // failed message
      }
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping  :  Failed");
      lcd.setCursor(0,0);  // let lcd to print to line 1
      lcd.print("Power   |    RST"); // print
      delay(4000);  // small delay for message on screen
      digitalWrite(plugRelay, LOW); // turn the plug power off
      Serial.println(buffer);
      delay(500);  // 0.5 sec delay
    }
                pingSuccess = !pingSuccess; // toggle pingSuccess variable

NO! The first time through loop, if you can ping something, you will set pingSuccess to true. The next time you are successful, you'll set pingSuccess to false. No way in hell is that logic right.

        if (pingSuccess)
{
  lcd.print("NOT SUCCESSFUL");
}

Say that again. I must be operating under a different assumption with respect to what successful means than you.

sorry i put the if (pingSuccess) in the wrong place

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(12,11,5,4,3,2);      // tell lcd what pins are being used to control lcd
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set mac address for ethernet port
    byte ip[] = {192,168,2,177}; // set desired ip address ethernet port
    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    const int plugRelay = 7;
    boolean pingSuccess = false;
    SOCKET pingSocket = 0;
     
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
      digitalWrite(plugRelay, HIGH);
      Ethernet.begin(mac, ip);  // start Ethernet
      lcd.begin(16,2);  // start lcd
      Serial.begin(9600);  // 
      lcd.setCursor(0,0);  // set lcd to print to line 1
      lcd.print("   Power-Ping   ");  // message to print
//      delay(5000);  // delay for message
      lcd.clear();  // clear lcd
      lcd.print("Power    |    ON"); // print
      lcd.setCursor(0,0);  // let lcd to print to line 2
      lcd.print("Ping "); // print
    }
     
    void loop()
    {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      if (pingSuccess)

   
      {
        sprintf(buffer,
                "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
                echoReply.data.seq,
                echoReply.addr[0],
                echoReply.addr[1],
                echoReply.addr[2],
                echoReply.addr[3],
                REQ_DATASIZE,
                millis() - echoReply.data.time,
                echoReply.ttl);
                pingSuccess = !pingSuccess; // toggle pingSuccess variable
      }
      else   // if no success the report fail message
      {

        // code to turn off power here
{
 
}
        //        sprintf(buffer, "Echo request failed; %d", echoReply.status);   // failed message
      }
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping  :  Failed");
      lcd.setCursor(0,0);  // let lcd to print to line 1
      lcd.print("Power   |    RST"); // print
      delay(4000);  // small delay for message on screen
      digitalWrite(plugRelay, LOW); // turn the plug power off
      Serial.println(buffer);
      delay(500);  // 0.5 sec delay
    }

this is the reviced code. is this correct >/

The whole reason for adding the pingSuccess flag was so that you could do this:

void loop()
{
   pingSuccess = false;
   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
         pingSuccess = true;
         // Do whatever you need to with the reply data
         break; // skip the rest of the iterations of the for loop
      }
   }

   if(!pingSuccess)
   {
      // Failed to ping the device in maxNumberOfTimesToTryToPing tries
   }
}

as simple as that.....

it also makes sence now ive seen it, its more logical....
(well now ive seen you can simply set a boolean with = true; / false; )

can i ask whats the best way to learn ? ive tried reading dummys guide's to c++ but its not that well informed..

i do find better if i set my self a task but i feel its the slower way to learn....

any advice ?

Work your way through the examples in the IDE. Understand them, modify them to do something slightly different, combine them to do more than one thing. Use the Web as a reference for C and C++ and Arduino reference for more specific Arduino commands.

Set yourself some simple goals even if it is only to light some LEDs in a pattern or tp prompt for 2 numbers and print the sum. Everybody learns in different ways but for me actually doing something works better than reading about it.