----- Original Message ----- From: Mccauley, Daniel H To: Sent: Tuesday, May 20, 2003 2:54 PM Subject: Re: [PIC]:Pushbutton Timing Routines Was: IDEAS for Pushbutton M ode Selections > I came up with the following routine for the following. Let me know what > you think. > If the pushbutton is pressed and released less than 2 seconds, the program > runs MODE1 and if the button is held for > greater than 2 seconds, SET-UP mode is run > > > char count; > count=0; > if (input(PB1)==0) > { > delay_ms(20); // Simple Debounce > while (input(PB1)==0) > { > count++; > delay_ms(10); > } > if (count < 200) > RunMode1 (); > else > SetUpMode (); > } > > Anyone see any problems (debouncing etc...) with this simple routine??? > Thanks much. > > Dan > Hi Dan, As Dale has already pointed out the count only being 8 bits wide would give you problems. I would suggest a further refinement: while (input(PB1)==0 && count < 200) { count++; delay_ms(10); } if (count < 200) { RunMode1 (); } else if (count > 3) { SetUpMode (); } Also note that if the switch is not released before this section of code is encounted again you may get a false trigger to RunMode1 or SetUpMode. You should be looking for a "switch released" somewhere. You might like to take a look at http://www.xcprod.com/titan/XCSB/CONTRIB/lcd-04.bas This does switch debouncing in a seperate user task and greatly simplifies dealing with switch events and timing contraints involving multiple switches complex states. Regards Sergio Masci -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body