Pushbutton Value Changer

techElder

Well-known member
This is a problem program for me.

I have 3 buttons and 3 values to either increase or decrease.

With one of the values, I can get by with increasing only with a rollover to the minimum value after the maximum value.

There is a "heartbeat" routine that interrupts the program at BPM intervals, so I am trying to avoid commands that are problematic to return to.

What I've implemented here is not working well from a user's perspective. It is clunky at best.

I am seeking a better solution for changing these three values with three switches.


OPERATING METHOD THAT DOESN'T WORK WELL

Change Value DutyCycle: This value is the one most often changed or the default value. Simply press the INCREASE or DECREASE button to change.

Change Value BPM: This value changes sporadically. One must hold down the DUTY_BPM button while pressing either INCREASE or DECREASE button.

Change Value ScriptNumber: This change doesn't happen often. One must hold down BOTH INCREASE AND DECREASE and press DUTY_BPM button to change value.

Values for DutyCycle range from 0 to 1000.

Values for BPM range from 5 to 100.

Values for ScriptNumber range from 1 to 7.


Code:
[color=Green]'-----------------------------
[PLAIN]'------[ PROGRAM START ]------[/PLAIN]
'-----------------------------[/color]

[color=Blue]do[/color]
[color=Green]' pushbutton change routines here

      ' Change the BPM
      [/color][color=Blue]do while [/color][color=Purple]DUTY_BPM [/color][color=DarkCyan]= [/color][color=Blue]TRUE
            if [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE then
                  [/color][color=Purple]BPM [/color][color=DarkCyan]= [/color][color=Purple]BPM [/color][color=DarkCyan]+ [/color][color=Blue]BPMCHANGE [/color][color=DarkCyan]MAX [/color][color=Navy]100
                  [/color][color=Green]'pause KEYPRESSDELAY
                  [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]TRUE
                  gosub [/color][color=Black]BeepNoise
            [/color][color=Blue]elseif [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE then
                  [/color][color=Purple]BPM [/color][color=DarkCyan]= [/color][color=Purple]BPM [/color][color=DarkCyan]- [/color][color=Blue]BPMCHANGE [/color][color=DarkCyan]MIN [/color][color=Navy]5
                  [/color][color=Green]'pause KEYPRESSDELAY
                  [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]TRUE
                  gosub [/color][color=Black]BeepNoise
            [/color][color=Blue]endif
            do while [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=DarkCyan]OR [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=Black]: [/color][color=Blue]loop    [/color][color=Green]; until button released
      [/color][color=Blue]loop

      [/color][color=Green]' Change the DutyCycle
      [/color][color=Blue]do while [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=DarkCyan]AND [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]FALSE
            [/color][color=Purple]DutyCycle [/color][color=DarkCyan]= [/color][color=Purple]DutyCycle [/color][color=DarkCyan]+ [/color][color=Blue]DUTYCHANGE [/color][color=DarkCyan]MAX [/color][color=Blue]DUTYMAX
            [/color][color=Green]'pause KEYPRESSDELAY
            [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]TRUE
            gosub [/color][color=Black]BeepNoise
            [/color][color=Blue]do while [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=Black]: [/color][color=Blue]loop                       [/color][color=Green]; until button released
      [/color][color=Blue]loop
      do while [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]FALSE [/color][color=DarkCyan]AND [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE
            [/color][color=Purple]DutyCycle [/color][color=DarkCyan]= [/color][color=Purple]DutyCycle [/color][color=DarkCyan]- [/color][color=Blue]DUTYCHANGE [/color][color=DarkCyan]MIN [/color][color=Blue]DUTYMIN
            [/color][color=Green]'pause KEYPRESSDELAY
            [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]TRUE
            gosub [/color][color=Black]BeepNoise
            [/color][color=Blue]do while [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=Black]: [/color][color=Blue]loop                       [/color][color=Green]; until button released
      [/color][color=Blue]loop

      if [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]TRUE then gosub [/color][color=Black]AdjustPWMoutput : [/color][color=Purple]resetPWM [/color][color=DarkCyan]= [/color][color=Blue]FALSE

      [/color][color=Green]' Change current script 1 - 7
      [/color][color=Blue]do while [/color][color=Purple]INCREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=DarkCyan]AND [/color][color=Purple]DECREASE [/color][color=DarkCyan]= [/color][color=Blue]TRUE
            if [/color][color=Purple]DUTY_BPM [/color][color=DarkCyan]= [/color][color=Blue]TRUE then
                  inc [/color][color=Purple]ScriptNumber
                  [/color][color=Blue]if [/color][color=Purple]ScriptNumber [/color][color=DarkCyan]> [/color][color=Navy]7 [/color][color=Blue]then                        [/color][color=Green]; only 7 scripts
                        [/color][color=Purple]ScriptNumber [/color][color=DarkCyan]= [/color][color=Navy]1
                  [/color][color=Blue]endif
                  [/color][color=Purple]resetScript [/color][color=DarkCyan]= [/color][color=Blue]TRUE
                  gosub [/color][color=Black]BeepNoise
            [/color][color=Blue]endif
            do while [/color][color=Purple]DUTY_BPM [/color][color=DarkCyan]= [/color][color=Blue]TRUE [/color][color=Black]: [/color][color=Blue]loop                       [/color][color=Green]; until button released
      [/color][color=Blue]loop
      if [/color][color=Purple]resetScript [/color][color=DarkCyan]= [/color][color=Blue]TRUE then
            gosub [/color][color=Black]StartNewScript
            [/color][color=Purple]resetScript [/color][color=DarkCyan]= [/color][color=Blue]FALSE
      endif
loop[/color]

[color=Green]'---------------------------
[PLAIN]'------[ PROGRAM END ]------[/PLAIN]
'---------------------------[/color]
[color=Blue]END[/color]
 

Technical

Technical Support
Staff member
short press +1, long press -1 (or +10/-10 etc)?

Then you can have an individual switch per function, much cleaner.
 

hippy

Technical Support
Staff member
The only way I can see your original DUTY plus INC and DEC buttons working is to react on button release rather than initial push. Otherwise pressing INC, then DEC then DUTY to change the Script Number would have triggered a Duty Change etc. I suspect that is partly what is making it feel clunky.

I would perhaps use the third button to select what is being altered and have INC and DEC alter on pushes, auto-alter faster while held. That would necessitate some sort of display to show which is being changed but I guess there may be a display already.

Or perhaps add another button so INC/DEC acts according to which or none of those buttons is held.
 

techElder

Well-known member
Thanks, Hippy. That's a good suggestion, too.

I do have extra pins for LEDs that I could use to display which value is being changed. Then the "long press" and "short press" that Technical suggests would only have to apply to the INCREASE and DECREASE buttons.

Unfortunately I can't add more buttons, because my AXE091 PICAXE DEVELOPMENT board only has 3 switches on it. :rolleyes: <WINK> :rolleyes: <WINK>
 

Technical

Technical Support
Staff member
But it does have a pot - select one of the three modes via the threes switches and then twist the pot for up/down.
 

AllyCat

Senior Member
Hi,

Or (easier to code), use the Pot to select the mode (left/centre/right) and use two buttons for Up/Down. Maybe then press also the third button to give Fast Up/Down on the larger ranges.

Cheers, Alan.
 

techElder

Well-known member
With a pot being in common with all three values, there is a problem with pushing one of three buttons but not changing the pot position. However, one can specify ... "Don't do that!"

Calibrate the pot for 0-100% = 0 to 1 volt. EDIT: Actually, with readADC10 converting 5 volts to 0-1023, when divided by 10 that is close enough to 0-100% for me.

do

do while button1 is pushed
read the pot and save the 0-100% reading for value1​
loop

do while button2 is pushed
read the pot and save the 0-100% reading for value2​
loop

do while button3 is pushed
read the pot and save the 0-100% reading for value3​
loop

Make adjustments for the different ranges based on the 0-100% return readings.​

loop
 
Last edited:

SAborn

Senior Member
You have 3 buttons (A,B,C), that will give you up to 7 different functions by using one or more buttons together, i often do this and use button A&B together to select the mode then button A or C to INC or DEC, then perhaps button B&C together to store the value to eeprom and exit the mode.
There is still other combinations like A&C, A&B&C.
 

techElder

Well-known member
Sounds easy to explain ... to a programmer, but what about my wife? :)


You have 3 buttons (A,B,C), that will give you up to 7 different functions by using one or more buttons together, i often do this and use button A&B together to select the mode then button A or C to INC or DEC, then perhaps button B&C together to store the value to eeprom and exit the mode.
There is still other combinations like A&C, A&B&C.
 

techElder

Well-known member
Pushbutton Select Routine

Here's what I've come up with so far. Only I don't have time to even simulate right now as I am getting away from it all for a few days.

Thanks to all of you for any comments and thanks to IWP for his button timing code in the link above.

I've added an outer loop, because I'm not sure yet what else will be done in the idle/main loop just yet.

What do you think? Do you think it will be clunky?

Code:
[color=Blue]do
      do while [/color][color=Black]DUTY [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=DarkCyan]OR [/color][color=Black]BPM [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=DarkCyan]OR [/color][color=Black]SCRIPT [/color][color=DarkCyan]= [/color][color=Black]TRUE
            [/color][color=Blue]do while [/color][color=Black]DUTY [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=Green]; loop while the button is held down beyond HELDLONGTIME
                  [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]SwitchHeld [/color][color=DarkCyan]MAX [/color][color=Navy]254 [/color][color=DarkCyan]+ [/color][color=Navy]1
                  [/color][color=Blue]if [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Navy]255 [/color][color=Blue]then
                        [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  [/color][color=Blue]else [/color][color=Black]SwitchHeld [/color][color=DarkCyan]> [/color][color=Black]HELDLONGTIME [/color][color=Blue]then
                        readADC10 [/color][color=Black]POTPIN, tempWord3
                        tempWord3 [/color][color=DarkCyan]= [/color][color=Navy]1023 [/color][color=DarkCyan]/ [/color][color=Black]tempWord3 : DutyCycle [/color][color=DarkCyan]= [/color][color=Black]DUTYMAX [/color][color=DarkCyan]/ [/color][color=Black]tempWord3
                        resetPWM [/color][color=DarkCyan]= [/color][color=Black]TRUE
                        [/color][color=Blue]gosub [/color][color=Black]BeepNoise
                  [/color][color=Blue]endif
            loop
            do while [/color][color=Black]BPM [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=Green]; loop while the button is held down beyond HELDLONGTIME
                  [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]SwitchHeld [/color][color=DarkCyan]MAX [/color][color=Navy]254 [/color][color=DarkCyan]+ [/color][color=Navy]1
                  [/color][color=Blue]if [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Navy]255 [/color][color=Blue]then
                        [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  [/color][color=Blue]else [/color][color=Black]SwitchHeld [/color][color=DarkCyan]> [/color][color=Black]HELDLONGTIME [/color][color=Blue]then
                        readADC10 [/color][color=Black]POTPIN, tempWord3
                        tempWord3 [/color][color=DarkCyan]= [/color][color=Navy]1023 [/color][color=DarkCyan]/ [/color][color=Black]tempWord3 : BPM [/color][color=DarkCyan]= [/color][color=Black]BPMMAX [/color][color=DarkCyan]/ [/color][color=Black]tempWord3
                        resetPWM [/color][color=DarkCyan]= [/color][color=Black]TRUE
                        [/color][color=Blue]gosub [/color][color=Black]BeepNoise
                  [/color][color=Blue]endif
            loop
            do while [/color][color=Black]SCRIPT [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=Green]; loop while the button is held down beyond HELDLONGTIME
                  [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]SwitchHeld [/color][color=DarkCyan]MAX [/color][color=Navy]254 [/color][color=DarkCyan]+ [/color][color=Navy]1
                  [/color][color=Blue]if [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Navy]255 [/color][color=Blue]then
                        [/color][color=Black]SwitchHeld [/color][color=DarkCyan]= [/color][color=Black]ZERO
                  [/color][color=Blue]else [/color][color=Black]SwitchHeld [/color][color=DarkCyan]> [/color][color=Black]HELDLONGTIME [/color][color=Blue]then
                        readADC10 [/color][color=Black]POTPIN, tempWord3
                        tempWord3 [/color][color=DarkCyan]= [/color][color=Navy]1023 [/color][color=DarkCyan]/ [/color][color=Black]tempWord3 : DutyCycle [/color][color=DarkCyan]= [/color][color=Black]DUTYMAX [/color][color=DarkCyan]/ [/color][color=Black]tempWord3
                        resetScript [/color][color=DarkCyan]= [/color][color=Black]TRUE
                        [/color][color=Blue]gosub [/color][color=Black]BeepNoise
                  [/color][color=Blue]endif
            loop

            if [/color][color=Black]resetPWM [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=Blue]then gosub [/color][color=Black]AdjustPWMoutput : resetPWM [/color][color=DarkCyan]= [/color][color=Black]FALSE
            [/color][color=Blue]if [/color][color=Black]resetScript [/color][color=DarkCyan]= [/color][color=Black]TRUE [/color][color=Blue]then gosub [/color][color=Black]StartNewScript : resetScript [/color][color=DarkCyan]= [/color][color=Black]FALSE

      [/color][color=Blue]loop
loop[/color]
 

1968neil

Senior Member
Tex,

I have some nice high quality pots that have click positions, the sort that you'd find in very high end hifi gear, i could post a few if you'd like ?

Regards
Neil
 

techElder

Well-known member
That's mighty magnanimous of you, Neil. I'm probably like you in that I tend to overbuy some items knowing I'll need them. I have some "precision" pots, some 5-turn pots and some 10-turn pots. I think I'll be able to use one of those varieties in this project. That's why when Technical mentioned "pots", the "little grey cells" started remembering what I had on hand.

Thanks so much, anyway!


I have some nice high quality pots that have click positions, the sort that you'd find in very high end hifi gear, i could post a few if you'd like ?
 

1968neil

Senior Member
No worries, They're here if you need them.
Thought as they had clicks they could be used as a "mode" switch, i used a couple a while back to select different colours on an RGB lamp i made for a friend and its still going happily 2 years later :)
Drop me a PM if you change your mind.

Best Regards
Neil
 
Top