Long / short press button...but reading ADC


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Thanks again for support !
    Unfortunately I can not solved this ... because "For accurate reading of ADC I use bubble-sort algoritm, how it's presented on this forum."
    No matter what I tried ; it's available ONLY short press.
    Regards !

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    What if you start a timer?

    DT's routines could tell you how long a button is pressed.

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  3. #3
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Thanks !
    I thought about this ... Should I use this ?
    I don't think I have "space" ...

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    can you explain what you are trying to achieve ?
    do you in fact have multiple buttons each with a different R4 ?
    did you try my second example ? is it even relevant if not what needs to happen differently ?

    at this stage I still can't see how an array sorting routine is in any way useful for detecting button presses long or short .
    why not post the code for what have you tried

    dt_elapsed will not run on a12f675

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Fratello,

    I would suggest downloading and looking at the original for 18F PICs here:

    http://www.picbasic.co.uk/forum/show...271#post130271

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  6. #6
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Tried hard to solve this problem, using a timer ... but without succes .
    Any advice will be very appreciated !


    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON 
    
    DEFINE OSC  4
    
    CMCON    = 7
    TRISIO   = %00001011
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    ADCON0.7 = 1
    
    nr        var word
    adca 	  var word
    adcb	  var word
    
    ADCValue  var WORD		' Final ADC Result
    CounterA  var BYTE		' Just a BYTE Temporary working variable
    DataW     var WORD		' Just a WORD Temporary working variable
    RawData   var word [10]		' Array holding ADC Result
    TIP_press VAR BIT
    
    j  VAR word
    
    GPIO.2 = 0
    GIE VAR INTCON.7
    GPIE VAR INTCON.3
    GPIF VAR INTCON.0
    Timer1  var word EXT :@Timer1 = TMR1L
    TMR1ON  VAR T1CON.0
    TMR1IF  VAR PIR1.0
    LONGPRESS   VAR BIT
    T1CON =  %00110100
    T1COUNT VAR BYTE
    GIE =0
    GPIE=0
    IOC.0 = 1
    ok var bit
    
    ;============================================================================
    Main: 
    ok=0
    tip_press=0
    TIMER1 =3037
    T1COUNT=0
    LONGPRESS=0
    nr=1
    
    ADCON0 = %10000001
    		Pauseus 50			
    	        ADCON0.1 = 1			
    		While ADCON0.1=1:Wend		
    		DataW.HighByte=ADRESH		
    		DataW.LowByte=ADRESL
    
    	if dataw < 631 then
        		TMR1ON=1
        		pause 1
    
    	    	WHILE dataw < 631
        	    	T1COUNT=T1COUNT + 1
                	CALL CHECK
                	WEND
    
        		IF LONGPRESS = 1 THEN
        		TIP_press = 1
        		ELSE
        		TIP_press = 0
        		ENDIF
    
        		T1COUNT = 0
        		pause 1
    		call achizitie
    	endif
    
    gosub efectuez
    gpif = 0
    GOTO MAIN
    
    
    achizitie:
    for nr = 1 to 2
    For CounterA=0 to 9
    ADCON0 = %10000001
    		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
    		RawData(CounterA)=DataW
    		Next CounterA
    CounterA=0 
    Gosub Getsort
    next nr
    return
    
    CHECK: 
      IF TMR1IF then
          TMR1IF  = 0
          Timer1  = 3037
          T1Count = T1Count + 1
          if T1Count = 2 then 
            TMR1ON = 0
            T1Count = 0
            LongPress = 1
          endif
      ENDIF
    RETURN
                             
    ;============================================================================
    GetSort:
    	If RawData(CounterA+1) < RawData(CounterA) then
    		DataW=RawData(CounterA)
    		RawData(CounterA)=RawData(CounterA+1)
    		RawData(CounterA+1)=DataW
    		If CounterA>0 then CounterA=CounterA-2
    		endif
    	CounterA=CounterA+1
    	If CounterA<10 then goto GetSort
    
    
    	DataW=0
    	For CounterA=4 to 7 
    		DataW=DataW+RawData(CounterA)
    		Next CounterA
    	ADCValue=DataW>>2
    if nr = 1 then 
    adca = adcvalue
    else
    adcb = adcvalue		
    endif
    
    if adcb=adca then ok = 1
    
    Return
    ;============================================================================
    efectuez:
    if ok=1 then
          IF adcb > 300 AND adcb < 350 THEN 
    			if tip_press = 1 then 
    				gpio.2 = 1 : pause 2000 : gpio.2 = 0
    			else
    				gpio.2 = 1 : pause 500 : gpio.2 = 0
    			endif
          endif
    endif
    Return
    ;============================================================================
    Name:  press_test.jpg
Views: 6683
Size:  180.9 KB

  7. #7
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Why do you insist on sorting the ADC readings? What problem is that trying to solve?

    I'd get rid of the "bubble sort" and try starting with Richard's code.

Similar Threads

  1. Replies: 5
    Last Post: - 26th February 2011, 05:51
  2. Button press or not
    By lerameur in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th November 2010, 20:37
  3. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  4. Sleep until button press?
    By kevj in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th October 2007, 03:47
  5. Button press and press & hold how to ?
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd August 2007, 03:37

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts