I'm used to programming in pBasic from taking a course in it in school but I'm trying to teach myself C/C++ or whatever the Arduino programming language is called and having writer's block so to say.
I need to write a bit of code that uses (if need be) an IF statement where if the condition is true it needs to loop to itself endlessly until the condition is not met.
For example, in pBasic I would write:
StarterCheck:
If StarterIn = HIGH THEN StarterCheck
LockDots:
I need to write a bit of code that uses (if need be) an IF statement where if the condition is true it needs to loop to itself endlessly until the condition is not met.
For example, in pBasic I would write:
StarterCheck:
If StarterIn = HIGH THEN GoSub StarterCheck
In C/C++ you'd use a while loop:
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, or have some other means to update it (for example you might have an interrupt handler updating it). Usually, you'd execute some code inside the loop to work out the current value.
I'm a little confused by this one. Care to explain how it works? The explanation isn't so clear, at least not to me.
tuxduino:
A sub calling gosub itself causes infinite recursion and eventually a crash due to memory exhaustion.
edit: that is, if there's no end condition or it's triggered too late.
I meant to write just the name of the tag (StarterCheck) without GoSub before it.
PeterH:
In C/C++ you'd use a while loop:
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, or have some other means to update it (for example you might have an interrupt handler updating it). Usually, you'd execute some code inside the loop to work out the current value.
Hmm, this one confuses me too (I hope I'm not hopeless :()
This is the part of my flowchart that I would like to code:
What's the easiest or simplest way to do this?
I don't mean to make it sound like I'm asking you to just write it for me, but I'm stuck pretty bad right now :~
No, not to this one single example, but usually sketches are more complex than two conditional statements.
If you consider an idea that is fractionally more involved, say,
If things need to happen once on a high value, the loop by itself will not suffice either.
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:
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?
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.