i'm assuming you have multiple buttons each with a different R4 so that in this example ---- with b1 pressed adc read is < 600,b2 pressed adc read is < 500,b3 pressed adc read is < 400.
this pretty much the same as my first example but it keeps track of which button of the three is pressed and for how long.
untested
Code:
@ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON
DEFINE OSC 4
long_press_threshold con 40 ;2 sec
press_threshold con 8 ;.4 sec
trigger_thresshold_1 con 600 ; adc reading must fall below this to count as a pressed button 1
trigger_thresshold_2 con 500 ; adc reading pressed button 2
trigger_thresshold_3 con 400 ; adc reading pressed button 3
CMCON = 7
TRISIO = %00001001
INTCON = 0
IOC = 0
GPIO = 0
ANSEL = %00110001
ADCON0.7 = 1
last_b_level var byte ; last button
b_level var byte ; which button
DataW var WORD ' WORD Temporary working variable
b_cnt var byte
b_ACT var byte ;0 not pressed ,1 pressed for 1 sec ,2 pressed for 2 sec
clear
Main:
gosub chk_sw
Pause 50
if b_ACT then
if b_level==2 then ;button 2
if b_ACT ==1 then
GPIO.2 = 0
else
GPIO.2 = 1
endif
b_ACT=0
b_cnt=0
b_level=0
endif
endif
Goto Main
chk_sw:
ADCON0 = %10000001 ;ch0
Pauseus 50 ' Wait for channel to setup
ADCON0.1 = 1 ' Start conversion
While ADCON0.1=1:Wend ' Wait for conversion
DataW.HighByte=ADRESH ' Read variable from ADC and save
DataW.LowByte=ADRESL
if DataW < trigger_thresshold_1 then
b_level =1
elseif DataW < trigger_thresshold_2 then
b_level =2
else DataW < trigger_thresshold_3 then
b_level =3
endif
if b_cnt==0 then last_b_level= b_level ;first press begin
if b_level= last_b_level then ; same btn is active add to count
b_cnt=b_cnt+1 ;if btn is active add to count
IF b_cnt > long_press_threshold THEN b_ACT= 2
ELSE ;button released
if b_cnt > press_threshold THEN ;press_threshold met
IF b_cnt > long_press_threshold THEN
b_ACT= 2 ;LONG press >2 SEC
ELSE
b_ACT= 1
ENDIF
ELSE ;press_threshold not met
b_ACT= 0
b_cnt=0 ;reset count
b_level=0
ENDIF
endif
return
Bookmarks