Pages: [1] 2 3   Go Down
Author Topic: Help with Loops and IFs??  (Read 1068 times)
0 Members and 1 Guest are viewing this topic.
Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First time programmer here, please be gentle  smiley-razz

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

How would I do this in C?
« Last Edit: January 12, 2013, 12:54:05 am by LockDots » Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That would be GoTo, I guess.
Logged

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

It wouldn't cause an issue using GoTo to send it to itself?
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2145
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See this example:
http://arduino.cc/en/Reference/Continue
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 11, 2013, 07:08:24 pm by tuxduino » Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11153
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

I only provide help via the forum - please do not contact me for private consultancy.

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


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.

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.


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, 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 smiley-sad)

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   smiley-confuse
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Will this suffice the flow chart?

Code:
loop(){

  //return on LOW, loop will automatically restart.
  if( Variable != HIGH ) return;

  while( Variable == HIGH  ){
   //Do stuff while true.
  }
}
Logged


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

Will this suffice the flow chart?

Code:
loop(){

  //return on LOW, loop will automatically restart.
  if( Variable != HIGH ) return;

  while( Variable == HIGH  ){
   //Do stuff while true.
  }
}

pYro, would you mind explaining how that works? I'm trying to step through it, but I'm confusing myself.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure,

The arduino has a hidden function in the core called main ( a standard c++ entry point ), it is the first* function to run, it has these few lines:

Code:
int main(){

  init();

  setup();

  while( true ){
    loop();
  }
}

init(); sets up the arduino timers/pwm/other stuff
 
setup(); calls the setup function you define in your sketch.

loop(); this also calls your sketch loop function, notice it is inside a never ending loop.



Now the code I provided:

Code:
loop(){

  //return on LOW, loop will automatically restart.
  if( Variable != HIGH ) return;

  while( Variable == HIGH  ){
   //Do stuff while true.
  }
}

As this function is called in an infinite loop, it will repeat as soon as it finishes.

if( Variable != HIGH ) return; This will cause the function to exit when the variable is low.

When HIGH, the code is allowed to flow into the 'while( Variable == HIGH  ){' loop, which will not exit until the variable goes LOW.

As the loop function continually loops, it will return to the if statement constantly exiting the function until another HIGH
Logged


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

pYro you are awesome! That makes a lot more sense now. Thank you so much!
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is this being made complicated ?
Reply #5 had the answer.

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11153
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if( Variable != HIGH ) return;

I don't think this statement contributes anything to the behaviour.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 12, 2013, 08:19:28 am by pYro_65 » Logged


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