Push Button 'Menu' to Choose Variable.


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154

    Default Push Button 'Menu' to Choose Variable.

    I would like to set up a simple 'menu' system at the beginning of some code to choose a Variable number.

    At the moment i have a reset switch on MCLR, so i want to change that be the switch input (as the circuitry is already there) for the variable selection. I already have a switch on the INTRUPT which is used later on in the code.

    So each time the button is pressed the variable counts from 1 to 10 and then back to 1 again. If there is no button push input for 5 seconds, the variable number is set (at whatever the last button push denotes) and then main code starts.

    Has anybody performed a similar task and can provide an example piece of code?

    Many thanks,

    Steve

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Just think about what you need to do and program accordingly...

    Rule 1. Increment a variable 1-10 if a Button is pressed.
    Rule 2. This variable loops back to 1 if value 10 is exceeded.
    Rule 3. Timeout and exit if Button has not been pressed for 5 Seconds.

    Code:
    	MyButton var PortB.0 	' Pin goes LOW when Button pressed
    
    	MyNumber var Byte	' Variable 1-10
    	MyTimeOut var Byte	' 5 second Timeout (100 loops of 50mS)
    
    	MyNumber=1		' Power-On with this starting value
    	MyTimeOut=0		' TimeOut starts from zero at Power-Up	
    
    	While MyTimeOut<100
    		If MyButton=0 then
    			MyNumber=MyNumber+1
    			If MyNumber>10 then MyNumber=1
    			MyTimeOut=0
    			Pause 50	
    				' This Pause DEBOUNCES Button - do not remove it
    			While MyButton=0:Wend
    				' Wait here if Button held down
    			else
    			MyTimeOut=MyTimeOut+1
    			Pause 50
    				' This Pause is the Timeout Pause
    			endif
    		Wend

  3. #3
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Many thanks for your help Melanie, almost there.

    I have tried to set up a visual represenattion of where the variable is at. To make things easier i have set it from 1 to 9. Depending on what number it is, depends what LEDs are lit.

    I have used PWM on each LED so 1-3 is RED, lit dim for 1 and bright for 3. Then GREEN 4-6 and BLUE 7-9.

    After the 5 second time out, i thought it would goto to the MAIN code, again this is signalled by a flashing LED sequence so i know it has times out on the 5 seconds.

    A couple of problems at the moment, the code seems slow to show the coloured PWM LED. Also after the 5 second time out, it doesn't goto the main code.

    I would like the coloured LED to stay lit at whatever stage until the next button push or unitl the time out occurs. Does the code below look right to you to achieve this? I'm sure i have done something wrong.

    The idea is there, just not the practicle side of things for me yet!!

    Code:
    TRISB = %00000001
    
    MyNumber    VAR Byte	' Variable 1-10
    MyTimeOut   VAR Byte	' 5 second Timeout (100 loops of 50mS)
    
    Red         VAR PORTB.1 ' All LEDs
    Green       VAR PORTB.2 ' Connected between
    Blue        VAR PORTB.3 ' RC pins and ground
    Orange      VAR PORTB.4
    
    MyButton    VAR PortB.0 	' Pin goes LOW when Button pressed
    
    MyNumber=1		' Power-On with this starting value
    MyTimeOut=0		' TimeOut starts from zero at Power-Up	
    
    
    	While MyTimeOut<100
    		If MyButton=0 then
    			MyNumber=MyNumber+1
    			If MyNumber>9 then MyNumber=1
    			gosub MyNumberPWM
                MyTimeOut=0
    			Pause 50	
    				' This Pause DEBOUNCES Button - do not remove it
    			While MyButton=0:Wend
    				' Wait here if Button held down
    			else
    			MyTimeOut=MyTimeOut+1
    			Pause 50
    				' This Pause is the Timeout Pause
    			endif
    		Wend
    
    main:
        high Orange
        pause 500
        low Orange
        pause 500
    goto main
    
    MynumberPWM:
        If Mynumber = 1 then PWM Red,1,255
        If Mynumber = 2 then PWM Red,25,255
        If Mynumber = 3 then PWM Red,255,255
        If Mynumber = 4 then PWM green,1,255
        If Mynumber = 5 then PWM green,25,255
        If Mynumber = 6 then PWM green,255,255
        If Mynumber = 7 then PWM BLUE,1,255
        If Mynumber = 8 then PWM BLUE,25,255
        If Mynumber = 9 then PWM BLUE,255,255
    RETURN
    Thanks again,

    Steve

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I assumed the following that either... You will have a Pull-Up resistor on your Button pin (button pulls pin down to 0v), or you will have switched-on the internal weak pull-up's for PortB. If that button pin is floating, you will have unpredicatable results.

  5. #5
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Indeed you are right, the switch has a 47k pull-up resistor.

    This is needed further on in the code as an interupt. How can i change this piece of code to reflect when it goes HIGH instead of LOW. I thought it may be just a case of changing:

    Code:
    While MyTimeOut<100
    		If MyButton=1 then  ' Changed 0 to 1
    			MyNumber=MyNumber+1
    But it seems there is more to it than that? When MyButton=1, the LEDs just seem to cycle through there brightness levels.

    Can you help??

    Cheers,

    Steve

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    The code that I posted in its existing form is not suitable for interrupt use. It was posted to get you started. Consult the "ON INTERRUPT" statement in the PBP manual to see how you could modify for that purpose.

    Changing the polarity of that statement would only be valid if the button pulled up to +Ve and the pin normally idled high.

  7. #7
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    I believe it wasn't working properly due to the Interrupt settings which i have now disabled on PortB.0.

    I have it working the way i want apart from one thing.

    It recognises the push switch, displays the appropriate PWM and then if it reaches 9 goes to 1 again. Also, if no input for 5 seconds it starts the flashing sequence (just to signal the end of the 5 seconds).

    How does one get the LEDs to stay in their PWM routine until the switch is pressed again, without using Interrupts. When the swicth is pushed it goes to the appropriate PWM routine but after the PWM cycle, the LED goes off.

    Here is the code.

    Code:
    MyNumber    VAR Byte	' Variable 1-9
    MyTimeOut   VAR Byte	' 5 second Timeout (100 loops of 50mS)
    
    Red         VAR PORTB.1 ' All LEDs
    Green       VAR PORTB.2 ' Connected between
    Blue        VAR PORTB.3 ' RC pins and ground
    Orange      VAR PORTB.4
    
    DISABLE INTERRUPT
    MyButton    VAR PORTB.0 	' Pin goes LOW when Button pressed
    
    MyNumber=1		' Power-On with this starting value
    MyTimeOut=0		' TimeOut starts from zero at Power-Up	
    
    	While MyTimeOut<100
            If MyButton=0 then
    			gosub mynumberpwm
                MyNumber=MyNumber+1
    			If MyNumber>9 then MyNumber=1
                MyTimeOut=0
    			Pause 50	
    				' This Pause DEBOUNCES Button - do not remove it
    			While MyButton=0:Wend
    				' Wait here if Button held down
    			else
    			MyTimeOut=MyTimeOut+1
    			Pause 50
    				' This Pause is the Timeout Pause
    			endif
    		Wend
    
    main:
        high Orange
        pause 500
        low Orange
        pause 500
    goto main
    
    MynumberPWM:
        If Mynumber = 1 then PWM Red,1,255
        If Mynumber = 2 then PWM Red,25,255
        If Mynumber = 3 then PWM Red,255,255
        If Mynumber = 4 then PWM green,1,255
        If Mynumber = 5 then PWM green,25,255
        If Mynumber = 6 then PWM green,255,255
        If Mynumber = 7 then PWM BLUE,1,255
        If Mynumber = 8 then PWM BLUE,25,255
        If Mynumber = 9 then PWM BLUE,255,255
    Return
    Many thanks,

    Steve

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    How does one get the LEDs to stay in their PWM routine until the switch is pressed again, without using Interrupts? When the swicth is pushed it goes to the appropriate PWM routine but after the PWM cycle, the LED goes off"

    Unless you are able to write a program with an "active real-time" loop that executes continuous PWM (I do recall posting an example of such code dimming multiple LED's continuously sometime ago), the only option you have is chosing a PIC with multiple hardware PWM.

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  3. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  4. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  5. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06

Members who have read this thread : 1

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