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.
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.