Products > Programming

While {you're down there ...}

(1/4) > >>

PerranOak:
In one of my first exercises learning C, I had to: light a LED, wait with a while loop for a button press then go on.

I can't think what syntax to use for a completely empty while.

Is it just:

while(button!=0) {}


BTW is this for "Beginners" or "Programming"?

dave j:

--- Code: ---while(condition){}
--- End code ---
will work, as will

--- Code: ---while(condition);
--- End code ---

The former is preferable as it is more obvious that you meant to have an empty loop.

Don't forget, in your code you'll need to make sure button gets updated otherwise you'll be stuck in an infinite loop.

dcbrown73:
This is not an electronics question, it's a programming question.

As for your question, button is not defined.    How is button defined?  Is it a variable that is Boolean true or 1 if the button has been pushed? 

If it's a boolean, you do not even need to test if it's != 0.

EDIT:  I'm guessing this is C programming you're referencing?

// Button not True (or 0)
while  !(button) {
    // do whatever
};

// Button is true (or anything other than 0)
while (button) {
    // do whatever
};
   

PerranOak:
Cheers all.

So next time should I post a dopey noob question re C in "Programming"?

rstofer:

--- Quote from: PerranOak on September 25, 2019, 03:14:19 pm ---In one of my first exercises learning C, I had to: light a LED, wait with a while loop for a button press then go on.

I can't think what syntax to use for a completely empty while.

Is it just:

while(button!=0) {}


--- End quote ---

I would probably do something like

while( button != 0 ) {
}

And somebody won't like it...

The problem I have is what exactly is 'button'.  Is it a variable that is changed in an interrupt routine?  If so, it needs to be declared as 'volatile'.  If it is a function that returns the state of the switch then the statement is wrong and should probably be:

while( button() != 0 ) {
}

if button is actually a MACRO like

#define button ( PORTB & 0x0001 )

it would probably work but macros should be all caps like

#define BUTTON ( PORTB & 0x0001 )

and

while( BUTTON != 0 ) {
}

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod