I need a bit of code that will only run if two conditions are true. Right now I have
if (digitalRead(11) == HIGH)
{
if (ammo > 0);
{
int ammo = Rammo;
}
}
I need a bit of code that will only run if two conditions are true. Right now I have
if (digitalRead(11) == HIGH)
{
if (ammo > 0);
{
int ammo = Rammo;
}
}
if (digitalRead(10) == LOW && digitalRead(11) == HIGH)
{
if (ammo > 0);
{
int ammo = Rammo;
}
}
You could do it this way, no problem, but you have to drop the int inside the inner if.
if (digitalRead(11) == HIGH && someOtherCondition)
{
if (ammo > 0)
{
ammo = Rammo;
}
}
if (ammo > 0);
And don't forget to drop the extra ; at the end of the if statement.
Maybe I need to reword a bit. I guess what I was looking for is
if (if (ammo > 0) && digitalRead(11) == HIGH)
{
int ammo = Rammo;
}
No, this is not what you are looking for. See post above.
if(condition) construct can have theoretically unlimited parts of the "condition" as long as it is logically sound
if(1 | any other "condition" - is no longer evaluated and will always be true
if(today is Tuesday & it is after 10 pm & I got nothing else to do) is valid "condition ".
It is important to pay attention to evaluating precedence of "condition" - it is generally "read" from left to right.
Compound conditions can be tricky
if( (A==1 & B==1 )| ( C == 0 & D ==1)) - the ( C == 0 & D ==1) won't get evaluated when (A==1 & B==1 ) evaluates to true etc.
My apologies, I definitely screwed that one up
if (ammo > 0 && digitalRead(11) == HIGH)
{
int ammo = Rammo;
}
fastenedrex:
My apologies, I definitely screwed that one upif (ammo > 0 && digitalRead(11) == HIGH)
{
int ammo = Rammo;
}
No need to apologize.
Small addition - variables have "scope' - a valid range where they can be accessed / used.
Since you have declared int ammo and entered the if condition with same variable name - ammo they are NOT the same - one declared INSIDE code block { int ammo.... } is valid only inside that code block.
Generally not a good idea, source of hard to find bugs.
But I figured you used is an example only, but now you know to keep track of variables of same name.
Best not to reuse names - does not save anything worth much ( memory) , and creates unnecessary grief.
BTW if(ammo > 0 ) is same as if(ammo)
if (ammo > 0 && digitalRead(11) == HIGH)
and
if (ammo && digitalRead(11))
are same conditions. But the first construct is more humanly readable.
Condition is evaluated for true or false - so when ammo = 0 is false.
Hi fastened rex,
Try it like this:
if ((digitalRead(11) == HIGH) && (int ammo > 0)) // if both conditions are true ...
{
ammo = Rammo;
}
Once you have that working, reduce the amount of code by combining things. Note I put the int into the first instance of ammo. I think it'll work the other way, but I like my code to be as human readable as possible, at least at first.
Thank you ChrisTenone for the almost perfect code and Delta_G for the clarification as to what is going on. I didn't realize that both variables had to be encased within parenthesis. Thank you
if (digitalRead(11) == HIGH && ammo > 0) {
// RELOAD!!
ammo = Rammo;
}
So this will set ammo to the value of Rammo provided that ammo is not zero and the pin is high. Once the value of ammo hits zero, it never gets set back again (by this fragment, anyway).
fastenedrex:
Thank you ChrisTenone for the almost perfect code
Chris's code is wrong. The "if (....(int ammo > 0)) construct is completely wrong. Do not use it.
I didn't realize that both variables had to be encased within parenthesis.
They don't. See reply #14 for good clean code.
J
Delta_G:
By putting that "int" there you are testing a new variable with some unknown content. It is most assuredly wrong. It has nothing to do with readability. The "int" does something and it is something you definitely don't want to do there. The OP surely wants to test some variable that already exists, not whatever garbage happens to be in the location where this new uninitialized variable is created.
I see now. I thought he was creating ammo as a local variable to be thrown away after this one use.