So I added the interrupt. The idea is to make the tempo LED blink in time with the echoes, so that if you turn the delay time knob clockwise, the echoes go slower and the LED blinks slower, and if you turn the delay time knob counter clockwise, the echoes go faster and the LED blinks faster. It's working pretty well but there is one thing it is doing that is weird, and I can't figure out why.

If you have the delay time pot full turn counter clockwise when you power on, the tempo LED gets stuck. If you turn the delay time knob full turn counter clockwise quickly, the tempo LED gets stuck and does not blink. However, if you turn the delay time knob full turn counter clockwise slowly, it works OK.

I'm sure the problem is in my code. Probably has something to do with the potentiometer debounce, but I can't figure out what I'm doing wrong. I remove the code that pertains to the bypass switching to make it easier to read.


Code:
#config 
    __CONFIG _CP_OFF & _WDTE_OFF & _BOREN_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _IOSCFS_8MHZ
#endconfig

ADCON0 = %00000000
ADCON1 = %00110000
ANSEL = %00000110
define ADC_SAMPLEUS 50
define ADC_BITS 8


CM1CON0 = %00000000
CM2CON0 = %00000000  

TRISA = %00001111
TRISC = %00000000

' Set TMR0 interrupt prescaler to 1:64
   OPTION_REG = %10000101        ' Set TMR0 configuration and enable PORTB pullups
   INTCON = %10100000           ' Enable TMR0 interrupts
   On Interrupt Goto tickint

    
    '                      
    '    Hardware connection
    '    ===================
    CS          VAR PORTA.5
    SCK         VAR PORTC.2
    SDI          VAR PORTC.1
    tempoLED var PORTC.0
    tempobutton var PORTA.0
    bypassbutton var PORTA.3
    bypassLED var PORTC.4
    fetA var PORTC.5
    fetB var PORTA.4
    fetC var PORTC.3
    
    
    
    '   
    '    Variables definition 
    '    ===================
    w var byte                          ' division toggle switch
    x var byte                          ' delay time knob
    xprevious var byte
    z var byte
    
    ticks var word
    tapcount var word
    LEDcounter var word
    LEDcounterlimit var word
    LEDrate var word
    
    trailsmode var byte
    trailsmodecounter var word 
    LEDon var byte
    LEDoff var byte
    
    LEDon = 1
    LEDoff = 0
                             
    '
    '    begin in bypass
    '    ===============
    
    tempoLED = 0
    fetA = 0
    fetB = 0
    fetC = 1
    bypassLED = LEDoff
    trailsmode = 0
    gosub readpot
    gosub movepot
    if xprevious <= 0 then
        xprevious = 1
    endif
    
main: 
        if LEDcounter > LEDrate then
            tempoLED = 1
            pause 5
            tempoLED = 0
        endif
        gosub potcheck
goto main


potcheck:       
        gosub readpot
        if x <= 0 then
            x = 1
        endif
        if x >= xprevious + 5 or x <= xprevious - 5 then               ' analog potentiometer debounce tolerance
            gosub movepot
        endif
return        

movepot:        
        CS = 0
        shiftout SDI, SCK, 1, [00000000,x]
        shiftout SDI, SCK, 1, [00010000,x]
        CS = 1
        xprevious = x
        gosub tempoLEDadjust
return
         
readpot
    adcin 2,x
    pause 10
return

tempoLEDadjust:                                           '  compensate for TMR0 not being 1:1 with actual delay time
    LEDrate = x + 10
    LEDcounterlimit = LEDrate + 1                       'counterlimit must always be 1 greater than rate
    LEDcounter = 0                                       ' reset LED counter
return 
  
Disable                                 ' Disable interrupts during interrupt handler
tickint:                                    ' Interrupt routine to handle each timer tick   
    
    LEDcounter = LEDcounter + 1            ' add 1 to LEDcounter on every interrupt
   if LEDcounter > LEDcounterlimit then      ' prevent LEDcounter from overflowing
        LEDcounter = 0
   endif
tiexit: 
   INTCON.2 = 0                             ' Reset timer interrupt flag
   Resume
enable
end