Led fader, again?

clasicrocker

New Member
Hi All,
I have a strange problem when I run this code in the simulator it performs just as I would think it should but when I program the chip 08m it does strange things. The code:

Code:
b0 = 255 'b0 is a variable full bright is 1 out is 255, I think

Main:    'where it all starts

if pin4 = 1 then goto Brighten ' they are pushing the button
if pin4 = 0 then goto Dim      ' they arn't pushing the button
Goto Main 'repeat

Brighten:
if b0 = 255 then Letemwait  'I don't want it to start as soon as they touch
Brighten2:   'where to come back to
if b0 = 1 then goto main  'we are the brightest we can get so just go look at the button
b0 = b0 - 1  ' go up in brightness
b2=255-b0/62*5+10  'half hearted attempt to linearise
pwm 2,b0,b2  ' I don't understand this but it works almost
goto main 'go look at touch pads

Dim:
if b0 => 250 then goto alltheway  ' we don't want to go to next step it will start over
b0 = b0 + 10 'next step down
b2=255-b0/62*5+10 'half hearted attempt to linearise
pwm 2,b0,b2 'set pwm output on pin2 duty b0 duration b2
goto main  'go back and look at button

alltheway: ' must be 250 so make it go out
b0 = 255  ' 255 sets led off well.....
goto main  'go back and look at button

Letemwait:  'don't let em think there in control
wait 2  ' don't know how long to mak em wait good for start
goto Brighten2  'this is like a return for a gosub cant use them because the stack up
Button is on pin4
led is on pin2

Do I have to do something with other pins or can they just float at any state?

What I see happening when the chip is running.

With no button pushed it should just go round and round it doesn't. I see the led start from dim and get brighter about 12 steep and it goes out for about the same time and does that until button is pushed.

When the button is pushed it slowly goes to full bright as I hoped, at that point I wanted it to stay at full but when it gets to the top, it goes out and stays out.

When I let go of button It does odd blinks for a time and goes back to the way I described originally.

ok around 40 cycles of that and it went full bright and cycled to dim. Just not what I expected.
Can anyone see what I am doing wrong.

Gripe *%($@@#
basic commands section 2 page 139 is the pwmout command and it shows that the 08m can use that command. Next page140 gives some example code

init:
pwmout 2,150,150 ‘ set pwm duty
main:
pwmduty 2,150 ‘ set pwm duty
pause 1000 ‘ pause 1 s
pwmduty 2,50 ‘ set pwm duty
pause 1000 ‘ pause 1 s
goto main ‘ loop back to start
That took me some time to find that it doesn't work on the 08m.
pwmduty that is.
Just had to say that feel better now http://www.picaxeforum.co.uk/images/smilies/redface.gif
Thanks for the help in advance
Jim
 

212

Senior Member
Do I have to do something with other pins or can they just float at any state?
Welcome to the forum. I usually ask questions on here myself, but I think I may be able to help this time. Unused inputs need to be tied, or put, either high or low, or you may see strange things happen like that. I think it is pretty standard to put the input, pin3 to ground through a 10K ohm resistor. Pins 0 and 1 can be made into outputs just by telling them low 0 and low 1, so put that at the top.

I type so slow, that a better reply may have been posted already, but I hope this helps :)
 

inglewoodpete

Senior Member
"Chip does strange things", especially for new users, usually means the "minimum operating circuit" requirements have not been met. Is the Serial In pin connected to the download circuit resistors as per manual 1?
 

clasicrocker

New Member
212,
I did try what you suggested but everything is the same
inglewoodpete,
I am using the rjhackett.net program adapter

And it has the needed minimum operating circuit resistor.

is it the code?
WOW, at a lose.
Thanks for looking

Another thing pin one is blinking at times also.
 
Last edited:

inglewoodpete

Senior Member
Put a debug command immediately after the "b2=255-b0/62*5+10" line. What values are being displayed? Are they a steady up-and-down?

Later edit: PWM command. Have a close read of the command manual. I think you want the PWMOut command.
 
Last edited:

hippy

Ex-Staff (retired)
A major problem is in the "b2=255-b0/62*5+10" line as inglewoodpete notes. The PICAXE only handles positive integer numbers. Use "b2=255-b0" to start with or whatever is required.

For PWMOUT, driving a LED to dim, the higher the value of duty the higher the brightness. You may want to redesign the code to take that into account. It's also recommended to start simple and build up. Fior example, write a program that simple dims up and stops at full brightness, another which starts full then dims down then combine the two and any other button handling.

Code:
DimUpAndStop:
  Wait 2
  b0 = 0
  Do
    If b0 < 255 Then
      b0 = b0+1
    End If
    PwmOut 2, 63, b0
    Pause 10
  Loop
 

clasicrocker

New Member
Hippy and all,
I have found that with the debug statements it does work most of the time. I think that the debug slows everything down. oh yes there is a large bling because of the debug command i'm sure. It still blinks when b0 should be just holding at 255. I see b0 holding at 254 with steps down to 252 and then a jump back up I'm thinking you are correct and I need to use pwmout because it will run in the background. I will try that this evening and let you all know what I find. Thank you all for the suggestions.
 

clasicrocker

New Member
This is the code I am now testing.

Code:
DimUpAndStop:
  Wait 2
  b0 = 0
  Do
    If b0 < 254 Then
      b0 = b0+5 ' didn't want to wait for 1 step at a time
      'debug
      PwmOut 2, 63, b0
      pause 10 'without the pause it is so fast it just goes dim. FAST
                   'and I did comment it out when I used the debug commend
     'else 
     ' without the else and b0 at 255 the led is vary dim  
     'tried each of these by themselves with the else uncommented
     'pwmout 2, off 'this one the led full bright
     'pwmout 2, 0, 0 'this also full bright at end
     'low 2 ' this one the led is vary dim like without the else
     'b5 = 23 'just to make sure it was going through this part of code
     'debug
     End If
  Loop
What I found is in the code but is there a chance that this chip is defective?
ok I pulled the 08m and replaced everything the same outcome.

I would like to get the led to go out all the way before I can continue with this project. I just don't see why.
Thanks again all
jim
 

hippy

Ex-Staff (retired)
There's very little chance the chip is defective and it seems to be behaving as expected. As you need a high duty rate to dim the LED, is the LED+R connected between the output pin and +V ?

I don't see why the LED won't go completely out but that could be because "PWMOUT 2,63,255" isn't full 100% duty. To achieve that would likely be something like "PWMOUT 2,63,256". You will need to use a word variable rather than a byte variable for the duty and ensure its value reaches whatever is required for 100% duty, 0% brightness.
 

clasicrocker

New Member
is the LED+R connected between the output pin and +V ?
yes, if the r is resistor

and when I changed to a word the led went out. W0 now counts to 256 and the led is off. and it is smooth looks really good. If I could understand what the pwmout was all about. Need more examples.
This forum is great, would be no ware without it and everyone. Thanks Now on to the next step. Make the up side. Easy now.
 

clasicrocker

New Member
I have narrowed it down to the part where I am looking at pin4. I have a switch on that pin and the problem starts as soon as I start looking at the pins.

Code:
Main:

if pin4 = 0 then
'don't do anything
endif

if pin4 = 1 then
b4 = b4 + 1
endif
debug
goto Main
When I run this code with the simulator It works the way I would think it should. When I program the chip and watch the debug screen as soon as the program starts there is a slow count on b4 even though the pin is open. When I press the button the count on b4 is faster. Do I have to...
OK I will read more.

Never mind
 

hippy

Ex-Staff (retired)
It sounds like the switch is not wired properly. An active high input has to have a switch between the pin and +V and a pull-down R to 0V. Details are in PICAXE Manual 3 - Electrical Interfacing.
 

BCJKiwi

Senior Member
If the code in post#11 creates problems only when in circuit and the switch is used, then there are a few things to check;

1. try serout instead of debug
2. How clean is the switch action - perhaps the switch has noisy contacts and is generating lots of counts - check the manual and posts for debouncing switch contacts.
 

BeanieBots

Moderator
I think the clue is here:-

I have narrowed it down to the part where I am looking at pin4..... I have a switch on that pin and the problem starts as soon as I start looking at the pins.

When I run this code with the simulator It works the way I would think it should. When I program the chip and watch the debug screen as soon as the program starts there is a slow count on b4 even though the pin is open. When I press the button the count on b4 is faster.
Literally, "open". Sounds like no pull-down resistor has been fitted, leaving the pin floating in the breeze to pick up mains hum when the switch is "open". Would expain the two count rates, one at mains, the other being the code as expected.
 

clasicrocker

New Member
Just a newbie that just want to make it happen. Manuals, I need that stinkin manual and all your help. As I thought about it and was typing I thought that I had noticed something about resistors and hippy had said something also. How do you say thanks? I guess by saying I will read more and only ask if I can't find what I need. Page 25 of manual 3 has the answer. I learned a lot today
Thanks all
Jim
 

inglewoodpete

Senior Member
Jim, Your sample code (above).....
Code:
Main:

if pin4 = 0 then
'don't do anything
endif

if pin4 = 1 then
b4 = b4 + 1
endif
debug
goto Main
Could be simplified:
Code:
Main:	If Pin4 = 0 Then
		'Do the increase part
	Else
		'Do the decrease part
	EndIf
	GoTo Main
 

boriz

Senior Member
“that stinkin manual”

I have certainly encountered the occasional manual like that, but the Picaxe manual is aimed at students and beginners and it’s quite easy to follow for the most part. Though some parts could be considered a little ambiguous (See BUTTON command).
 

clasicrocker

New Member
boriz.
That stinkin manual was a line from a movie, but it was:

badges, badges, we don't need no stinking badges.

I do need and was saying that I do need to read more, lots more. It would be better if it wasn't ambiguous. A wiki might be the answer, where everyone could fix things, (within reason) add examples, and do it as it happens not weighting for the next update.

I do need it and didn't mean that the manual was totally ambiguous just in places like you said.
Thanks all this is a good forum, nice people, and I have enjoyed my first try with the picaxe and I do have more questions. Reading. Reading.

Jim
 

Dippy

Moderator
A brief tangent:-

Wiki has been discussed numerous times... yawn.

Much general and basic information is already in Manuals.

"...as maximum current cabaility and the input pin types on each chip"
- all in Manual 1, though a wee bit basic for serious amateurs.

I would like to see fuller and better explanations in many areas.
e.g. ST vs TTL input threshold levels - which, before anyone starts moaning, HAS caught many people out.
And a few extra specs lifted from PIC Data Sheets under each PICAXE model would be good too in Manual 1.

Trouble is that a lot of people leap to the Forum before reading.
If the Manuals were expanded then there wouldn't be much difference between on online Manual and certain types of Wiki entry as 'cast in stone' data should be read only.

Wiki entries are very useful, but, without checking/reviewing, I can foresee piles of rubbish being published along with really good stuff.
Whats wrong with using the finished projects section?. Ever read it?
The Internet is full of rubbish - but because 'computer says' (a nod to Little Britain) people think its accurate. Wiki is not a magic Grimoire.
 

SilentScreamer

Senior Member
I think a wiki would come into its own with tutorials and reference threads. The forum is for discussion IMHO but threads like these don't really fit:

Those are merely examples that I've quickly found. Having a link to a thread at the bottom of each wiki page would be useful for any discussion around the topic and the wiki would prove ideal for reference.

EDIT:

Wiki entries are very useful, but, without checking/reviewing, I can foresee piles of rubbish being published along with really good stuff.
Whats wrong with using the finished projects section?. Ever read it?.
The wiki would be ideal for referencing to when answering questions. Its easier to extract information for a page of facts rather than a thread of questions and answers.

I often use the finished projects section for searching but there are as many threads containing discussion as there are threads containing useful information.
 
Last edited:
Top