Pages: [1]   Go Down
Author Topic: "OR and "AND" question  (Read 339 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm working on a router reseter ..

basically sends out 4 pings to two differnet IP addresses.

if either one returns I want a positive result.

If both fail I want a Negative result.

getting confussed with my "OR" and "AND".

Not sure how to tidy up my else.. as I need ELSE and two false pingRet , pingRet2.

cheers

david
Code:
delay(2000);
  bool pingRet; // pingRet stores the ping() success (true/false)
  bool pingRet2; // pingRet stores the ping() success (true/false)

#ifdef ledOut
  startPing();
#endif
  ICMPPing ping(pingSocket);
  pingRet = ping(4, pingAddr, buffer);
  pingRet2 = ping(4, pingAddr2, buffer);
  Serial.println(buffer);

#ifdef ledOut
  delay(250);
  endPing();
  delay(2000);
#endif
//




#ifdef ledOut
  if(pingRet) // Failure  I want to have an OR here so any Successful ping shows Network OK.
  if(pingRet2) // Failure
  {
    pingSuccess();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    ping ok");
    lcd.setCursor(0,1);

   
    {
      lcd.print(buffer);
      delay(20);


      {
      }
    }
  }
  else
  {
// I need pingRet and pingRet2 to both be false here to generate this network fail.
    pingFail();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("  Network FAIL ");
    delay(2000);
    lcd.setCursor(0,0);
    lcd.print("  Sarian Reset  ");
    delay(10000);
  }
#endif
  delay(delayMS);

« Last Edit: January 31, 2013, 07:51:22 am by mrfunk » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 599
Posts: 48525
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you used { and } with every if statement, you'd know the answer.
Code:
  if(pingRet)
  { // Failure  //////////// I want this to be OR by here
    if(pingRet2) // Failure
    {
      pingSuccess();
    }
  }
Both pingRet and pingRet2 must be true for pingSuccess to be called.

Of course, you can combine them into one statement, using either && (and) or || (or), whichever meets your needs:
Code:
  if(pingRet && pingRet2)
  {
     // Both must be true to get here
  }

  if(pingRet || pingRet2)
  {
     // Either one of the statements being true will get to here
  }
Logged

Online Online
Faraday Member
**
Karma: 78
Posts: 3707
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Both being true is no more positive than just one, one is enough...

I'd take the fail by default view and go something like this:

Code:
// set both pingRet and pingRet2 as false out here to start
// then do the stuff which pings and sets pingRet and pingRet2 to actual values

  if(pingRet || pingRet2)
  {
     // Either one of the statements being true will get to here so do success stuff
  }
 else
 {
    // Neither was true to get here so do failure stuff
 }
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Both being true is no more positive than just one, one is enough...

I'd take the fail by default view and go something like this:

Code:
// set both pingRet and pingRet2 as false out here to start
// then do the stuff which pings and sets pingRet and pingRet2 to actual values

  if(pingRet || pingRet2)
  {
     // Either one of the statements being true will get to here so do success stuff
  }
 else
 {
    // Neither was true to get here so do failure stuff
 }

thats exactly what i've done.. many thanks.. all working
Logged

Pages: [1]   Go Up
Jump to: