Go Down

Topic: "OR and "AND" question (Read 394 times) previous topic - next topic

Hackdub

Jan 31, 2013, 01:40 pm Last Edit: Jan 31, 2013, 01:51 pm by mrfunk Reason: 1
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: [Select]
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);



PaulS

If you used { and } with every if statement, you'd know the answer.
Code: [Select]
  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: [Select]
  if(pingRet && pingRet2)
  {
     // Both must be true to get here
  }

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

JimboZA

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: [Select]
// 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
}
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Hackdub


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: [Select]
// 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

Go Up