Society of Robots - Robot Forum

Software => Software => Topic started by: GrooveHolmes on December 04, 2010, 10:36:56 PM

Title: if() vs while()
Post by: GrooveHolmes on December 04, 2010, 10:36:56 PM
Code: [Select]
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopstart){

if(button_pressed()){
rprintf("Pressed\n");
}
return 200000; //1,000,000 = 1 second
}

Seems timed with the return value of the appControl() function.


Code: [Select]
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopstart){

while(button_pressed()){
rprintf("Pressed\n");
}
return 200000; //1,000,000 = 1 second
}

This seems to bash out 20x the amount of "Pressed" strings sent to the uart.


'xplain!  ;D
Title: Re: if() vs while()
Post by: Ryltar on December 04, 2010, 11:06:29 PM
Not quite sure what the question is if there is one, but the if statement will fire once a loop. The while statement runs repeatedly when the button is pressed, extending the initial length before the return delay is factored in.
Title: Re: if() vs while()
Post by: Soeren on December 05, 2010, 05:46:49 PM
Hi,

'xplain!  ;D
I see you have a problem with spelling as well ;)

It's for a reason they're called High Level Languages, as the commands are pretty near plain English and have roughly the same meaning (although some of them needs "closing tags").


IF [you have no money] THEN
   Get a job
ENDIF
(Singular event)


WHILE [you have no money]
  You cannot buy stuff
WEND
(Ongoing situation until resolved, perhaps by fulfilling first bit of code)

The repeat rate of the WHILE/WEND will depend on what needs done - print commands are slow.
Title: Re: if() vs while()
Post by: Webbot on December 06, 2010, 03:53:15 PM
In your first example the IF statement is processed once and then you return 20,000 ie a 20ms delay. So your if statement wont be called until 20ms later.

In the second example you are constantly looping around (with no delay) whilst the button is pressed.