In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Zoot wrote: Yes, use an interrupt. Here is *pseudo* code. The important think is that really, all the ISR is doing is keeping time, both for delays in user interface work (buttons) and counting RCtime decays. I used 10us, but resolution is totally up to you. I'm not showing variable declarations or anything. [code] INTERRUPT 100_000 ' 10us "ticks" Interrupt_Handler: Update_RC_Time_But_Only_If_Flag_Set: IF doRCflag = 1 THEN IF rcPin = 1 THEN ' it did not fall to 0 yet (or rise to 1 depending on your circuit) INC rcTimer ' best as Word to get a decent amount of possible time ENDIF ELSE INC rcTimer ' to track charge time ENDIF Update_User_Delay_Time_If_Flag_Set: IF doUserDelay = 1 THEN INC userTimer ' best as Word to get a decent amount of possible time ENDIF RETURNINT Main_Program: ' the important think here is that the main loop keeps whipping through everything all the time ' and sets flags, pin states, clears counters, as needed... Track_RCtime_Decay: IF doRCflag = 1 THEN ' are you in the middle of measurement? IF rcPin = 0 THEN ' pin finally fell, check and save the reading... doRCflag = 0 rcValue = rcTimer rcTimer = 0 HIGH rcPin ' OUTPUT and high ENDIF ELSE ' otherwise wait 2ms charge time (or whatever you want) IF rcTimer > 200 THEN ' 2ms/10us INPUT rcPin ' set to measure rcTimer = 0 ' clear the count doRCflag = 1 ' set the flag ENDIF ENDIF ' so at this point, you always have the most recent current reading from the RCTIME circuit in rcValue Track_User_Button_Stuff: IF button0pin = 0 THEN ' active low IF userTimer > 50_000 THEN ' half second of "holding" button ' do something ELSEIF userTimer > 5000 THEN ' 50ms hold (so everything is debounced) ' do something else ENDIF ELSE ' released state, clear timer userTimer = 0 ENDIF Do_Something: IF rcValue > 10_000 THEN ledPin0 = 1 ELSE ledPn1 = 0 ENDIF Etc: Done: GOTO Main [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=438115#m438483 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2010 (http://www.dotNetBB.com)