Go Down

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

LockDots

Jan 12, 2013, 12:20 am Last Edit: Jan 12, 2013, 06:54 am by LockDots Reason: 1
First time programmer here, please be gentle  :P

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?

tuxduino

That would be GoTo, I guess.

LockDots

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

LarryD

The way you have it in your schematic isn't the same as how you have it wired up!

tuxduino

#4
Jan 12, 2013, 01:06 am Last Edit: Jan 12, 2013, 01:08 am by tuxduino Reason: 1
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.

PeterH


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: [Select]


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 only provide help via the forum - please do not contact me for private consultancy.

LockDots


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


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: [Select]


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   :~

pYro_65

Will this suffice the flow chart?

Code: [Select]
loop(){

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

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

LockDots


Will this suffice the flow chart?

Code: [Select]
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.

pYro_65

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: [Select]

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: [Select]
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

LockDots

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

UKHeliBob

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

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

tuxduino

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.

PeterH


Code: [Select]

  if( Variable != HIGH ) return;



I don't think this statement contributes anything to the behaviour.
I only provide help via the forum - please do not contact me for private consultancy.

pYro_65

#14
Jan 12, 2013, 02:17 pm Last Edit: Jan 12, 2013, 02:19 pm by pYro_65 Reason: 1
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.

Go Up