Beginners question

BrunoCH

Member
Good evening, dear forum.
I am once again facing a problem with which I am hoping for your help.
I would like to use a PICAXE (preferably 08M2) to dim an LED from 0% to 100% with a pushbutton and then dim it back down from 100% to 0% with the same pushbutton after a while.
Do any of you have an idea?
THANKS for your help
Greetings from Switzerland

Bruno

Addendum:
The button should only be pressed briefly at a time, not until the LED is fully lit or goes out completely when the unit is switched off.
 
Last edited:

Jack Burns

New Member
Have a look at the following post, it’s not quite what you want, but certainly a good place to start.


Looking at the code, it looks like a press of the button causes the LED to go from dim to full brightness and back to dim again. With a few changes you should be able to achieve what you need.
 

PhilHornby

Senior Member
Here's some code that ChatGPT generated when I typed your query in o_O. It doesn't quite do what you've asked for, but it runs!

With just the slightest tweaks :-
Rich (BB code):
#picaxe 08m2      

symbol ledPin = C.2   ' Pin connected to the LED
symbol buttonPin = PinC.3   ' Pin connected to the pushbutton
symbol pwmValue = b1   ' Variable to store the current PWM duty cycle
symbol pwmDirection = b2   ' Variable to store the direction of PWM change (0 for decrease, 1 for increase)

' Initialize the PWM pin and duty cycle
pwmout ledPin, 0, 255

' Main program loop
do
  ' Read the state of the pushbutton
  if buttonPin = 0 then
    ' Button is pressed, change the PWM duty cycle
    if pwmDirection = 1 then
      ' PWM duty cycle is increasing
      pwmValue = pwmValue + 1
      if pwmValue > 255 then
        pwmValue = 255
        pwmDirection = 0
      endif
    else
      ' PWM duty cycle is decreasing
      pwmValue = pwmValue - 1
      if pwmValue < 0 then
        pwmValue = 0
        pwmDirection = 1
      endif
    endif
    
    ' Update the PWM output
    pwmout ledPin, pwmValue, 255
    pause 10 ' Add a short delay to debounce the pushbutton
  endif
loop
 

papaof2

Senior Member
Considering all the other "don't work as implemented" things our elected leaders have put in place over time, do you think we'll be able to keep them from suggesting that ChatGPT knows enough and is smart enough to run the countries? Not without their sage advice and biases, of course - just so it does what they want...
 

PhilHornby

Senior Member
I just idly wondered what it would do, faced with a specific requirement. It wrote the code above ... paused and then decided it could do better. At which point ... it got stuck and I cancelled it. Subsequent to that, it would only speak "Python" :) ... despite my repeated protests.

But of the code it did write, I only changed a few pin definitions and ended up with code that did "some" of what I asked for. It doesn't appear to know that Picaxe only does integers, so the if pwmValue < 0 then section needs work!

Probably good enough to "put a man on the moon" - but not get him home again :eek:
 

Buzby

Senior Member
Don't trust chatGPT for anything logical.

It's great at writing poems and essays, but not very good at simple things that humans can do.

I said,
swap the first and last letter of each word in this list : fish, dog, heron, fly, flamingo, zebra

chatGPT replied,
Sure, here's the list with the first and last letters of each word swapped:
"hisi, god, neroh, ylf, oamingfl, arebz"
 

papaof2

Senior Member
About like one punster's definition of why "fish" should be spelled "ghoti".
If you haven't seen it before, try to figure it out or go search for ghoti
 

hippy

Technical Support
Staff member
It doesn't appear to know that Picaxe only does integers, so the if pwmValue < 0 then section needs work!
Yes. 'if < 0' doesn't work and neither does 'if > 255' when it's a byte variable. That means it's never going to change direction. It also doesn't initialise direction to be increasing so it's always going to be an immediate jump to full, then decreasing to zero, then back to full again. With a maximum period of 255 it needs a duty of 1023 rather than 255.

It's hard to tell whether it 'invented' this solution from first principles or fudged code for something else without understanding the constraints a PICAXE has or has simply presented someone's 'why doesn't this work?' code as its own. I suspect the later.

It seems to me that ChatGPT is like that familiar chap down the pub who always knows the answer to everything but actually doesn't.
 

BrunoCH

Member
Tanks all for help,
this is my code that works but is certainly not perfect.
I'm sure there are still some points to make it easier but for now it's ok.
The goal is to debounce the key.
thanks a lot
Bruno

#picaxe 08M2

output C.2
input C.1
symbol ea = b2

w0 = 99
ea = 0

Schleife:
Pause 10
if pinc.1=1 then goto einblenden
Pause 10
if pinc.1=0 then goto Schleife

einblenden:
ea=0
for ea = 0 to 255 step 5
if ea=255 then goto halten
pwmout C.2,w0,ea
pause 20
next ea

ausblenden:
ea=255
for ea = 255 to 0 step -5
pwmout C.2,w0,ea
pause 20
next ea
goto Schleife

halten:
high C.2
Pause 10
if pinc.1=1 then goto ausblenden
goto halten
 
Top