Potentiometer vs. Button

BrunoCH

Member
good evening everyone,
i have a small problem with my Picaxe 08M2. i have a small program that reads in a potentiometer at C.1 and dims a led at the output C.2 corresponding to the potentiometer. now i would like to replace the potentiometer with 2 buttons (up and down). do any of you experts have an idea?

many thanks for the help.

greetings Bruno

my Prog.

#picaxe 08M2
setfreq m32
init: pwmout C.2,97,0 ; start pwm


main: readadc10 C.1 , w1 ; input Pot

w1= w1/16*6
pwmout pwmdiv64, C.2, 97,w1
Goto main

*******************************************************************************************************************************************
 

matchbox

Senior Member
I'm not an expert by a long shot.
But something like this should work.

Code:
#Picaxe 08m2
Setfreq M32

Symbol Duty = w0
Symbol Intensity = C.2
Symbol Up_sw = PinC.3
Symbol Down_sw = PinC.4

Main:

    PWMOUT pwmdiv16, Intensity, 99 ,0
    Duty = 0
   
Do
   If Up_sw = 1 then
    Duty = Duty + 10 Max 799                     ' change increment UP value to suit
      'Do : Loop Until Up_sw = 0                 ' add if switch free run UP is not required

   Elseif Down_sw = 1 then
    Duty = Duty - 10 Min 0                       ' change increment DOWN value to suit
     If Duty > 65525 then : Duty = 0 Endif       ' zero underflow
     'Do : Loop Until Down_sw = 0                ' add if switch free run DOWN is not required
   Endif

      PWMduty Intensity, DUTY                    ' update PWM duty cycle
      Pause 200                                  ' slow free run speed
Loop
 
Last edited:

PhilHornby

Senior Member
Or as an alternative :) :-

Rich (BB code):
#Picaxe 08m2
#no_data
#terminal 4800

symbol UpButtonChecker  = b1
symbol DownButtonChecker= b2
symbol PWMRatio         = b3              ;0-99

symbol UpSwitch         = C.1
symbol DownSwitch       = C.4

symbol StepAmount       = 10              ;single-step amount
symbol ARDelay          = 100             ;Delay before auto-repeat
symbol ARRate           = 10              ;Rate of auto-repeat


;     setfreq M4                          ;NOTE: default

      PWMRatio = 50                       ;initialise PWM Ratio=~50%
      
      pullup %00010010                    ;Pullup C.1 and C.4 (switches to GND here)
do
      pwmout PWMDIV64,C.2,24,PWMRatio     ;626Hz.
                                          ;See the "PWM characteristics" TAB of the
                                          ;PWMOUT wizard for details
      
      button UPSwitch,0,ARDelay,ARRate,UpButtonChecker,0,UpNotPushed ; Active LOW, with auto-repeat, Fall through on "PUSHED".
      sertxd (cr,lf,"UP, PWMRatio:",#PWMRatio)
      PWMRatio = PWMRatio + StepAmount max 100

UpNotPushed:      
      button DownSwitch,0,ARDelay,ARRate,DownButtonChecker,0,DownNotPushed ; Active LOW, with auto-repeat, Fall through on "PUSHED".
      sertxd (cr,lf,"DOWN, PWMRatio:",#PWMRatio)
      if PWMRatio <= StepAmount then
            PWMRatio = 0
      else
            PWMRatio = PWMRatio - StepAmount
      endif

DownNotPushed:
loop
(tested!)

 
Last edited:

inglewoodpete

Senior Member
I'm not an expert by a long shot.
But something like this should work.

Code:
Do
   If Up_sw = 1 then
    Duty = Duty + 10 Max 799                     ' change increment UP value to suit
      'Do : Loop Until Up_sw = 0                 ' add if switch free run UP is not required

   Elseif Down_sw = 1 then
    Duty = Duty - 10 Min 0                       ' change increment DOWN value to suit
     If Duty > 65525 then : Duty = 0 Endif       ' zero underflow
     'Do : Loop Until Down_sw = 0                ' add if switch free run DOWN is not required
   Endif

      PWMduty Intensity, DUTY                    ' update PWM duty cycle
      Pause 200                                  ' slow free run speed
Loop
The trick with getting Min and Max to work properly is to limit the range first:
Rich (BB code):
Duty = Duty Min 10 - 10
- or -
Duty = Duty Max 789 + 10
 

BrunoCH

Member
I am proud of you, thank you very much, exactly what I wanted, works perfectly SUPER
Greetings from Switzerland

Bruno
 

Technoman

Senior Member
You have the single button solution as seen on some commercial product : while you keep pressing the button the brightness slowly increase then decrease then stop on the wanted brightness when you release it. Touch control is an alternative to mechanical button.
 

BrunoCH

Member
Thank you very much, I will take the liberty to post some pictures when the project is finished, it is for a floor model of a Robinson R66 helicopter.

Greetings Bruno
 
Top