One Pass

14 Feb 2012

Hi everyone, Could someone give me some advice on how to write a simple piece of code so i have something happen just once, i suppose you could say for one pass of the code and then ignore it after that. The following is just to have the display show a different message for each switch position. The "on" and "off" are going to be different modes of a device. Eventually i will have a lot of code after the display shows each mode but i dont want it to show it again once it passes through what is there.

if(toggleswitch==1)

{

lcd.cls();

lcd.locate(2,7);

lcd.printf("on");

wait(2);

}

if(toggleswitch==0)

{

lcd.cls();

lcd.locate(2,7);

lcd.printf("off");

wait(5);

}

Thanks for any help.

15 Feb 2012

Hi,

Many ways to skin a cat! Depends a lot on what you mean by "just once" You could do a display at the beginning of your main, or... have an init method, which you call once, or use a flag

using a flag. Useful, because you can reset it.

int flag = 1;

void main() {

while (1) {

if (flag) {
Flag=0;
do this thing just this once. 
.... your print position code ....
....
} // endif

.... do stuff ...
...If a status changes you can reset the flag to 1.
...Say your switch was toggled. 

}// endwhile

}//endmain

or you really could do it once,

void init() {
do something...
}

void main() {
init();
while (1) {
Other code
}
}

depends what exactly you are trying to do really. I'm afraid I come from a procedural background originally, before I went all "OO", So I don't mind flags. Some people hate them though. There are many ways to program flow, just make sure you don't keep testing a value if you don't need to. The first one above tests the "if" on every loop. Highly inefficient if you really only display just once at the beginning, but useful if you may want to reset the flag when your switch changes position.

15 Feb 2012

Thats great thankyou. the flag will probably be best because the toggle switch can be switched as many times as needed so each time it is switched it will have to display the mode its in. I take it the second way wont do this as it will only do display a message the once and not again? Thanks again.

15 Feb 2012

I thought so ;-)

in your main looping bit,

keep the status of the switch...

oldSwitchPosition, and reset the flag as and when needed.

PSEUDO CODE DISCLAIMER !

int flag = 1;
int oldSwitchPostion;

void main() {

//first initialise the switch so we can see when it changes.
oldSwitchPosition = switch.read();

while (1) {

currentPosition = switch.read();

if (currentPostion!=oldSwitchPosition) {
//it's changed.
oldSwitchPostion=currentPostion;
flag=1;
}

if (flag) {
Flag=0;
do this thing just this once. 
.... your print position code ....
.... using current postion ....
....
} // endif

.... do stuff ...
... using current position....


}// endwhile

}//endmain

rather than keep reading the switch, I don't know how long your other stuff will take, and the switch could be changed while the other stuff is happening, leading to odd results, I'd read it into a local variable just the once everytime at the beginning of the loop, then I'd test against that local variable, that way, it'll be consistent all through the main loop.

<EDIT>

Changed the code, to show what I mean by the last bit.

Disclaimer.

Many ways to skin a cat, this is just a construct I use. I bet others could show you other ways.