Switch in PIC BASIC HELP!!!!


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Program selection on button press

    Try this idea, it works with PBP in a PIC18F4620 and should work with any PIC that has EEPROM.

    I set a byte variable "ProgramState" to 1 in a data statement and write this to EEPROM when the program is loaded into the PIC.

    At power up, the code reads the value stored in ProgramState, here set to 1, and jumps to the appropriate code block.

    MAIN:
    READ 0, ProgramState
    If ProgramState = 1 then RunCode1
    If ProgramState = 2 then RunCode2
    If ProgramState = 3 then RunCode3

    If ProgramState > 3 then
    write 0, 1 'reset ProgramState to first code block
    goto MAIN
    endif

    RunCode1:
    execute codeblock 1
    if ButtonDown = 1 then
    write 0, 2
    endif
    goto MAIN

    RunCode2:
    execute codeblock 2
    if ButtonDown = 1 then
    write 0, 3
    endif
    goto MAIN

    etc

    HTH
    BrianT
    Last edited by BrianT; - 14th May 2010 at 01:45. Reason: pushed TAB by mistake

  2. #2
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Thanks,Braian,for help,
    I try your idea,but doesn't,work.
    I try explain what I need,example:

    program1:
    HIGH PORTB.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    GOTO program1

    program2:
    HIGH PORTB.2
    PAUSE 500
    LOW PORTB.2
    PAUSE 500
    GOTO program2

    program3:
    HIGH PORTB.3
    PAUSE 500
    LOW PORTB.3
    PAUSE 500
    GOTO program3 .........

    and now I don't now how,how I make code for switch!

    I need,when I press switch,JUST ONE TIME,
    and every time when I press switch again,
    to change program1,program2,program3,......

    that is problem!

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default with two buttons

    Hope this helps.

    This code will need changing as it's in PBP, but the logic should still work with PB. I've used this to select options in a menu by pressing one button and then another to jump to the option selected, so in theory it could be used to select which program you run

    Code:
    LCDOUT $FE,2,"Main Menu"
    lcdout $FE, $C0, "Select Option"
    
    if Option = 1 THEN lcdout $FE, $D4, "Set Time and Date  "
    if option = 2 then lcdout $FE, $D4, "Set Night Period   "
    if option = 3 then lcdout $FE, $D4, "Set Normal Temps   "
    if option = 4 then lcdout $FE, $D4, "Run                "
    
    IF !H_butt THEN Option = option + 1 ; if button is low
    pause 150
    if option >4 then option = 0
       
    If option = 1 Then ; if option =1 and S_but is taken low then reset LCD and gosub debounce followed by jump to option selected
    If S_butt = 0 Then
    LCDOUT $FE,1 
    Gosub SetButtonRelease
    goto  setup
    endif
    endif
    
    
    If option = 2 Then
    If S_butt = 0 Then 
    LCDOUT $FE,1
    Gosub SetButtonRelease
    goto nitetime
    endif
    endif
    
    
    If option = 3 Then
    If S_butt = 0 Then 
    LCDOUT $FE,1
    Gosub SetButtonRelease
    goto GetSetpoint
    endif
    endif
    
    If option = 4 Then
    If S_butt = 0 Then
    LCDOUT $FE,1 
    Gosub SetButtonRelease
    goto main
    endif
    endif
    
    Goto mainmenu
    The reason I use two buttons (one to select the option and one to confirm it) is that if you don't I found it hard to get passed the first option. For example, if you had

    Code:
    program1:
    HIGH PORTB.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    GOTO program1
    
    program2:
    HIGH PORTB.2
    PAUSE 500
    LOW PORTB.2
    PAUSE 500
    GOTO program2
    
    program3:
    HIGH PORTB.3
    PAUSE 500
    LOW PORTB.3
    PAUSE 500
    GOTO program3
    
    
    switch =0
    if portB.0 = 0 then switch = switch +1
    
    If switch = 1 then goto program 1
    If switch = 2 then goto program 2
    If switch = 3 then goto program 3
    if switch >3 then switch=0
    then as soon as you press your switch and the variable switch=1 it would jump to program 1 and never reach program 2 or 3.

    Hope this helps, and sorry it's not converted into PB that you can use directly, but hopefully that won't be too hard to cchange
    Last edited by malc-c; - 14th May 2010 at 12:42.

Members who have read this thread : 0

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