Pages: 1 [2] 3   Go Down
Author Topic: Help with Loops and IFs??  (Read 1231 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
If StarterIn = HIGH THEN StarterCheck

The C equivalent would be

Code:
If (StarterIn == HIGH) {StarterCheck;}
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A loop version of it would be

Code:
while(StarterIn == HIGH) continue;
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

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

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:
if(this == that)
   doThis();
or
Code:
if(them == those)
{
   doThis();
   andThis();
   andDontForgetThis();
   meToo();
}
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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 smiley

But the equivalent to the above exactly would be:
Code:
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
Logged

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

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:
while(digitalRead(somePin) == LOW)
{
   keepDoingThis();
}
?
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

should do the same as

Code:
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?
Logged

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

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:
while (StarterIn==LOW)
and
Code:
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.
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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.
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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....
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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!
Logged

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2775
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..."

Logged

Pages: 1 [2] 3   Go Up
Jump to: