PDA

View Full Version : Push Button 'Menu' to Choose Variable.



Tissy
- 8th March 2005, 16:15
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

Melanie
- 9th March 2005, 09:17
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.



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

Tissy
- 9th March 2005, 10:27
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!!



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

Melanie
- 9th March 2005, 12:14
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.

Tissy
- 9th March 2005, 12:26
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:



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

Melanie
- 9th March 2005, 13:55
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.

Tissy
- 9th March 2005, 17:17
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.



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

Melanie
- 10th March 2005, 07:00
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.