Go Down

Topic: Help with Loops and IFs?? (Read 1 time) previous topic - next topic

dhenry

Code: [Select]
If StarterIn = HIGH THEN StarterCheck


The C equivalent would be

Code: [Select]
If (StarterIn == HIGH) {StarterCheck;}

dhenry

A loop version of it would be

Code: [Select]
while(StarterIn == HIGH) continue;

LockDots


Quote
I meant to write just the name of the tag (StarterCheck) without GoSub before it.


A label is _not_ a sub(routine). GoSub is basic dialect for "call a subroutine". Goto is _not_ the same as gosub.

Did you read http://arduino.cc/en/Tutorial/HomePage Section 1. Basics ? You seem to miss some fundamental bits.


I understand the difference between a GoSub and a Goto. A Goto does just what it implies, it goes to a certain part of the program and continues on to the rest of the code from there whereas a GoSub goes to a subroutine and returns to the following line from where it was called.

In pBasic to accomplish what I have in my flowchart I would write the following:

Code: [Select]

StarterCheck:
If StarterIn = LOW THEN StarterCheck


StarterCheck: is a placemarker, tag, or label (whatever you want to call it).
The "IF" statement would check to see if variable StarterIn is LOW then it will force the program to return to the tag/label "StarterCheck:". This keeps the program looping from "StarterCheck:" to the IF statement as long as variable StarterIn is LOW. Once it goes HIGH, the IF statement is no longer true and so the program falls through to whatever line of code is after the IF statement.

note: In my original post my IF statement was written incorrectly (used HIGH, instead of LOW), the correction has been made above.

So I guess my real question is, how do I create a tag, label, or placemarker in C for the program to go to when an IF condition has been met?

PaulS

Quote
So I guess my real question is, how do I create a tag, label, or placemarker in C for the program to go to when an IF condition has been met?   

You don't. It executes the next instruction (or block of instructions.
Code: [Select]
if(this == that)
   doThis();

or
Code: [Select]
if(them == those)
{
   doThis();
   andThis();
   andDontForgetThis();
   meToo();
}

LockDots

Haha. Well that kind of stinks.... There's got to be a way to do it, no? :(

I need the program to get to a certain point where it gets stuck in a loop waiting for an input to go HIGH. Once it goes HIGH I would like it to fall through to the rest of the code.

johncc



In pBasic to accomplish what I have in my flowchart I would write the following:

Code: [Select]

StarterCheck:
If StarterIn = LOW THEN StarterCheck



note: In my original post my IF statement was written incorrectly (used HIGH, instead of LOW), the correction has been made above.

Pretty hard to answer definitively when it keeps morphing :)

But the equivalent to the above exactly would be:
Code: [Select]

while (StarterIn==LOW){ /* do absolutely Nothing */ }

which means "do absolutely NOTHING ({}) until StarterIn is HIGH".  The question (for both pieces of equivalent code) then is "What makes StarterIn go from LOW to HIGH?"
Quote

So I guess my real question is, how do I create a tag, label, or placemarker in C for the program to go to when an IF condition has been met?

As PaulS reply, you don't.  You should think of it as "there is no GoTo, there is only GoSub".

Cheers,
John

PaulS

Quote
I need the program to get to a certain point where it gets stuck in a loop waiting for an input to go HIGH. Once it goes HIGH I would like it to fall through to the rest of the code.

As in
Code: [Select]
while(digitalRead(somePin) == LOW)
{
   keepDoingThis();
}

?

LockDots

Hmmm, tags are going to be a tough habit to break. I got so used to them with pBasic.

Code: [Select]
while (StarterIn==LOW){ /* do absolutely Nothing */ }

should do the same as

Code: [Select]
while(digitalRead(somePin) == LOW)
{
   keepDoingThis();
}


right?

If so, I just have to think of something to give it to do .....as in nothing. Or can I just leave it blank?
Also, the "WHILE" command, does it cause the program to stay in the WHILE or does it perform whatever's inside the WHILE, while it continues onto the rest of the program?

PaulS

Quote
If so, I just have to think of something to give it to do .....as in nothing. Or can I just leave it blank?

Yes, but the comment that you showed is a better idea. Make it clear that the Arduino is doing nothing for a reason.

Be aware, though, that:
Code: [Select]
while (StarterIn==LOW)
and
Code: [Select]
while(digitalRead(somePin) == LOW)
are NOT the same thing.

In your snippet, the value of StarterIn is used to determine whether there is nothing to do, and it that is the case, it starts doing nothing. Forever, since the value of StarterIn never changes while the loop is running.

In my code, a pin is checked. The state of the pin can change, so there is a way to get the Arduino doing something again.

LockDots

I think I see what you're getting at now. Are you saying that in the two codes that I posted, the first one only checks the variable StarterIn once whereas using digitalRead like yours keeps checking it?

PaulS

Quote
Are you saying that in the two codes that I posted, the first one only checks the variable StarterIn once whereas using digitalRead like yours keeps checking it?

Precisely.

LockDots

You just blew my mind lol. Thank you so much PaulS!

johncc



Also, the "WHILE" command, does it cause the program to stay in the WHILE or does it perform whatever's inside the WHILE, while it continues onto the rest of the program?


It stays in the  { ... } while the condition is true, it does not go on to the rest of the program until the condition becomes false....

LockDots



It stays in the  { ... } while the condition is true, it does not go on to the rest of the program until the condition becomes false....



Perfect, thank you too johncc!

michinyon

Reply #5 said:  In C/C++ you'd use a while loop:

Code:

while(StarterIn == HIGH)
{
    // update StartIn here by some means
}


If StartIn is a variable (as implied in the code above) then you'd need to either do something inside the loop that caused it to become HIGH when you wanted to exit the loop.....



I would not normally be so picky,  but since the OP said he could not understand this,   the last sentence quoted above should actually say "do something inside the loop which caused it ( StartIn ) to CEASE TO BE high, when you wanted to exit the loop..."


Go Up