Author Topic: if() vs while()  (Read 1651 times)

0 Members and 1 Guest are viewing this topic.

Offline GrooveHolmesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
if() vs while()
« 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
The difference between the student and the master, is that the master knows what he does not know.

Offline Ryltar

  • Full Member
  • ***
  • Posts: 74
  • Helpful? 1
    • GlitchTech Science
Re: if() vs while()
« Reply #1 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.

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: if() vs while()
« Reply #2 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.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: if() vs while()
« Reply #3 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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk