Dimming many Leds via pwmout 28X1

BCJKiwi

Senior Member
Program and circuit development continues on my current project and one aspect which I have working under test involves reading ambient light levels with an LDR via ReadADC and then modulating the LED brightness by varying the output via pwmout.

Code:
' Vary LED Intensity depending on ambient brightness
ReadADC LDR,b0
If          b0>b1 then let b1=b0-b1     ' test to see if READADC value changed.
     elseif b1>b0 then let b1=b1-b0     ' ensure a positive value for b1
EndIf
If  b1>20 then pwmout 2,62,b0     ' If READADC changed enough
     b1=b0                        ' store last LDR value used
Endif
I only have a couple of LEDs with resistors on the PWMOUT pin for test.

In the final application I am looking to vary the supply for around 50 LEDs in total although no more than 30 or so would be on at any one time. Most of these will be controlled via MCP23016 I/O expanders.

I would appreciate some suggestions on the sort of FET or whatever to put on the PWMout pin to create the supply for the LEDS at the required current from the 5V supply.

An associated question relates to the best frequency to run pwm at. Have noted much discussion about the noise coming from different pwm frequencies but assume this relates to the devices being driven which would not apply to LEDs. Some seem to favour high pwm frequencies, some lower.

At the moment I have
pwmout 2,62,b0 - see code above
where b0 varies from 0 to 254 which keeps the values in the byte range. This means a 64KHz frequency at 16Mhz clock speed which is the final app clock speed (testing at 8Mhz at present)
I could of course slow this by using a word value but would prefer to keep this a byte value.

So the question is a multi part;
what device do I need on the end of the pwm out pin to;
1. handle the current
2. switch fast enough to maintain the pwm characteristics
3. allow the mcp23016 sink in comfort.

The mcp23016 has an internal clock of 1MHz and the digital I/O ports are TTL and latched.
 
Last edited:

Michael 2727

Senior Member
There does not seem to be a great deal of choice when it comes
to "readily available" MOSFETs (here in OZ anyway).
You have the 2N7000 which is good for 200mA ,TO-92 package.
Then you go up to the IRF range of TO-220 packages which
range from 4A to 60A or so.
The IRF530 or IRF540 (N-ch, low side devices, load to +POS, switch the NEG ON/OFF)
devices or the IRF9540 (P-ch, high side device, Load to -NEG, switch the +POS ON/OFF)
(turn on with Hi or Low gate inputs respectively) depending on your requirements,
would be your best and cheapest option, easy to obtain and @ around $5.00 AUD ea.

You should always over rate MOSFETS, this saves replacing them in the long run.
If you have a load that draws 1A then use a 5A, 10A or 20A MOSFET, this will
allow your 1A Fuse (which you WILL include in the circuit, won't you ! ) to
blow long before the MOSFET does in the event of any accidents.
1Amp fuse + 1Amp MOSFET + accident, the MOSFET will lose every time, guaranteed.

As far as frequency goes, anything above say 40Hz to 60Hz should work.
( the baseline for PWMOUT is 4KHz, unless you poke the prescale register )
I tend to stick to the lower end of the PWMOUT 4KHz, but in some cases
e.g. a motor and even some Light Globes you will be able to hear the PWM
noise. Above 10KHz to 15KHz you shouldn't hear anything much on any device.
I would stick to under 20KHz, thats my personal choice.

I've never seen/used an MCP23016 , so can't offer much there.
 

Shafto

Senior Member
Hey, thanks for that code, I've been having troubles with PWM LEDs... I think it's my PSU though, I haven't been able to rig up something better.. this helps, but I'm still getting some flicker at some levels.

What I'm using to deliver the power is sharp voltage regs, 3.3,5,9, and 12v, they have a built in transistor for on/off use, so you just ground pin 4 with a 10k and then PWM signal to pin 4, turns the reg on and off to PWM your LEDs... I'm doing this for tail lights on a vehicle, so PWM is marker lights.. and for the brakes the brake signal goes right to pin 4 for full on... no code to waste time or anything, works well... blocking diodes on both inputs to pin 4 obviously... I dunno if that helps you for your app but I thoguht it might... part number PQ09RD21 for 9V version.
 

BCJKiwi

Senior Member
@ Michael 2727
Thanks, As you might gather from my posts, the hardware side is not my strong point. It so happens I have an IRF530 on hand so I'll give it a try. The manual (sect 3, Page 7) implies a direct connect from the pwmout pin to the gate but is this OK? The IRF530 spec sheet indicates the gate needs from 2 to 4 V to turn on but I can't see where the gate current is specified so I'm not sure if a limiting resistor is required.

@Shafto
If I understand your post correctly you are pwm-ing the on/off pin of the regulator. I would not have tried that myself as the regulator presumably has caps etc around it to clean up the input/output and will need time to turn on/off and stablise its voltage level. I would not expect it to be very stable if turned on and off at the rate you would be with pwm.

My approach is to provide a pwm-ed supply rail via pwmout and the IRF530 which is then used to feed the LEDs. The on/off of the LEDs is handled separately - i.e. by turning on/off the ports on the MCP23016 (1 port per LED) which sink the IRF530 supply to 0V. Perhaps you could test this alternate approach and see if it helps.

The code above was a refinement on my earlier code so as to only reset pwmout when necessary as Technical suggested in a recent post that it was not a good idea to be constantly resetting pwm.
 

Shafto

Senior Member
I'm sure using PWM on the on/off pin of the reg is fine.. it's a proven design not of my own.. many PWM kits for motors and such all use transistors for switching the load, which is exactly what's built into the reg... I remember reading about it in one of the data sheets for them, but I haven't been able to find that nice detailed version of the data sheet again.

If I don't use a variable for the PWM, just a value, it works perfectly.. it's the reseting that's giving me flicker... and I think my power supply must not be giving a good reference voltage.. I just haven't been near a soldering iron in a couple days to put the SMT cap onto my 5V reg to start using that instead.
 

Technical

Technical Support
Staff member
The problem with constantly re-issuing pwmout commands is that each pwmout command resets the internal timer, which effectively disrupts the current cycle. If you do this very often you may notice some flicker.

One way to overcome this on X1 parts is to use the new 'pwmduty pin,value' command. This pokes the 'duty' register with the new value without affecting the timer, and so can help in this type of situation. Naturally you still have to use pwmout once to start the function.

'pwmduty' command is at beta at the moment, but you can try it out if you have the latest version editor (5.1.5)
 
Last edited:

BCJKiwi

Senior Member
When you say you are getting flicker "at some levels" would you please explain further - i.e. does it get worse at one end of the range or randomly.

What frequency are you running pwm at - have you tried much slower / faster frequencies?
 

BCJKiwi

Senior Member
Thanks Technical
I have my PWM testing on a 14M at the moment but will try the pwmduty when I move it to the 28x1.
 

Shafto

Senior Member
I'm using a 28x1, so I could give the duty command a try..

I get varying flicker at different levels, mostly lower duty cycles though, it's worse at 15khz using pwmout 62,b0, so I went to a longer period and a word value for the variable and used pwmout 255... this longer period helps, but as the duty cycles gets down there I get flicker, it seems almost like 2 PWM signals intersecting... as I change the frequency or the duty the flickering characteristics change aswell. Odd that when I debug the inputs though the value doesn't fluctuate, and if I set it to a state where I'm getting horrible flicker, and debug the value, and then just insert it as a constant and run and the code everything is fine.. no flicker at all.

I'll report back as soon as I've tried it with the regulator supplying the voltage instead of straight from the 5V rail of a computer PSU.
 

Shafto

Senior Member
Alright, got my SMT cap soldered right onto the leads of the reg, they said put it as close as possible, so I did.. hah.. though my main drive for that was the fact that the SMT version of the 47uF industrial grade low ESR tantalum caps were far cheaper than any suitable axial or radial version.. and had better characteristics as well... I also decided to go this route for all of my 9V regs for the LEDs that use a 0.33uF tantalum on the inputs, there's a pin in between though, so I modified the legs on the caps, I just bent the out from underneath and now they span the 1 pin gap... I took a pic, but photobucket is acting up.. I'll post it as soon as it starts working again.

As for the PWM I've been cured.. it was the power supply as suspected, I put together a suppressor very similar to the one suggested by Hippy in another thread seen here: http://www.picaxeforum.co.uk/attachment.php?attachmentid=173&d=1193139252

Thanks for that.. it consists of a 1amp fuse, blocking diode, 500uH ferrite choke, 0.22uF X2 cap (doesn't have 47R resistor to ground yet because I just didn't have the room to breadboard it, but it will be in the final version, is a standard 1/2 watt good for this?) then a 00.1uf X2 cap.. no varistor yet either.. because it's not in the car, but I have a heavy duty automotive varistor to use in the final version.

I now can only see some very minor flickering at certain levels, but a very slight adjustment takes care of it, the code I found best worked though was this: (Thanks to Technical)

Code:
main:
ReadAdc ADC_PIN, b2
if b2 <> b3 then
PwmOut 2, 62, b2
b3 = b2
end if
goto main
There's more adjustability with this code because it doesn't bottom out on the 20 value, give it a shot if you wish.

Might want to just try these voltage regs, it'd be a cheaper option anyway.. and they work great for PWM... I know many people who use this unit with success: http://cgi.cafr.ebay.ca/PWM-driver-regulator-for-dimming-SuperFlux-LEDs-arrays_W0QQitemZ280161186915QQihZ018QQcategoryZ66954QQcmdZViewItem

Edit:

I know you said you wanted to keep it a byte value and not use a word, but I just tried this code and compared it to the above:

Code:
main:
	
	readadc 2,b2

	b3 = 4

	w1 = b2 * b3

	if w1 <> w2 then

	PwmOut 2,255,w1

	w2 = w1

	end if

goto main
I imagine my setup will be used from 3-20% duty cycle, so I want good adjustability down low.. the longer period used here made it much easier to fine tune down low.. dunno if that's a concern of yours though. I could also observe no flicker at all with this period.. hopefully 3900hz isn't going to interfere with anything?
 
Last edited:

Michael 2727

Senior Member
BCJ MOSFETS don't require much current to switch ON,
not like a transistor does, they work on voltage.
It is always a good idea to include a current limit
resistor to every output always.
This will offer protection to the Picaxe in the event
of the device failing ( going short ).
Sometimes a MOSFET Gate pulldown resistor is used
to eliminate capacitance which can hold the Gate ON.
( 10K to 100K works well usually )

This is how I would do it - IRF530
1K on the picaxe output to the MOSFET Gate.
Then a 22K to 47K from the gate to -NEG or 0V.
And use a PWMOUT of 4K to 20K.
And a F U S E !
 

Shafto

Senior Member
I'm pretty sure there's a built in resistor for the pin.. I just tested how much current goes there
straight from the picaxe and I get 0.45mA, it was the same whether I was driving a 20mA source or 160mA.

I can't find any documentation on it though. There was a very detailed data sheet for these guys I saw once.. but I just can't find it again...

I also just measured how much current the picaxe draws with my program.. I get 12.5mA max with PWM to one of these on/off pins, and 2 others hooked to digital outputs, plus another 8 outputs driving a darlington array.
 
Last edited:

Michael 2727

Senior Member
Any internal limiting is a safety precaution and should
not be relied upon to do the heavy work.

What happens if the MOSFET goes Short Circuit and
you are running it from a 12V DC or 20V DC supply ?

Sometimes you have to read between the lines.

And you may only see 12.5 mA from the PWMOUT
because @ 50% Duty the average will only show as half.
 

Shafto

Senior Member
I set all ouput pins high and turned everything on full when I tested... that's the max it's gonna draw. I don't know anything about mosfets, haven't read about them yet.

...adjusting from 0-100% duty only changed output from the reg 0.2 miliamp.. and the other 2 switches on the regs were on full with digital outputs.

I shouldn't then use the 8 channel darlington array chip designed for 5V cmos with internal limiting resistors connected directly to digital outputs either? (those were all running too full on when I tested the draw of the picaxe aswell) that seems like overkill to me, but who am I to say... seems fine though.
 
Last edited:

BCJKiwi

Senior Member
@ Michael
Thanks very much - will implement the resistors you recommend (and the fuse - I didn't miss the bold text the first time around!)

@Shafto
The reason I use the code;
Code:
ReadADC LDR,b0
If      b0>b1 then let b1=b0-b1 ' test to see if READADC value changed.
       elseif b1>b0 then let b1=b1-b0
EndIf
If  b1>20 then pwmout 2,64,b0  ' If READADC changed enough
     b1=b0      ' store last LDR value used
Endif
is because I expect the adc values to be fluctuating reasonably quickly and I don't need to vary the output smoothly. By implementing a +- 20 step before a change, the number of timer resets is reduced dramatically compared to the <> approach that will cause a timer reset for every single change in adc even if only by a value of 1.

You can adjust the sensitivity of the code I use by altering the amount of change you test for by changing the '20' to any value you like.

Have you tested 'pwmduty' yet? - might have saved you the effort on the PSU since you are still getting some flicker.

The reason for not wanting to use a word var is simply code efficiency and available variables. This is just a small part of a bigger project and I will be struggling with speed and space once all the elements are combined. If the pwmduty works OK this code will be changed to;
Code:
init:
pwmout 2,64,20 ' establish pwm pin, frequency - 64 allows duty to 258
.
.
main:
.
.
ReadADC LDR,b0
pwmduty 2,b0
.
.
which is much more efficient, does not reset the timer, and allows b1/w0 to be used elsewhere in the program. It would also mean I could use lower frequency pwm but that would require multiplying the bo and storing it to a word value to be able to use a duty value above 254 - more code ho hum...
 

BCJKiwi

Senior Member
Moved my test code from the 14M to the 28X1 and implemented the pwmduty approach
Code:
#PicAXE 28X1
setfreq m8
'
INIT:
' setup pwm ( for pwmduty mode of operation on 28X1
pwmout 1,64,100       ' establish pwm pin, frequency - 64 allows duty to 258
'
Main:
' Vary LED Intensity depending on ambient brightness
ReadADC 0,b0       ' read ambient light intensity
pwmduty 1,b0       ' output new pwm value
sertxd(" ADC = ",#b0,cr,lf)
pause 1000
Goto Main
and it works well with much less code!

Thanks Technical
 
Last edited:

Shafto

Senior Member
I also just tried out the duty command, I'm now using this:

Code:
init:
	
	pwmout 2,255,20

main:

	readadc 2,b2

	b3 = 4

	w1 = b2 * b3

	if w1 <> w2 then

	Pwmduty 2,w1

	w2 = w1

	end if
It nets the same results, but I suppose it's better not to be resetting the clock.. I still don't know much about the code, I didn't try the duty command because I didn't know how to run a command only once (pwmout to get it started) now I see how you do that..

I'm using precision 18 turn pots for my inputs... and no matter what I do I can see some some flicker at some levels, but I have to really tweak the screw on the pot to the right area, and it's easily fixed by a half mm more turn so it's not a big deal.

Edit:

Now using this... my full code.. I implemented the switch for the marker lights aswell.. and took out comparing whether w1 has changed or not as it doesn't seem needed with the PWMduty command, thanks again to technical for that... this whole unit is just to make some sequential flashers for a car and deal with the marker lights which are PWM controlled.. and then the brakes get full power... I made my own thread about it but since I've been talking about it so much here's the little video if you want to see what the code does visually: http://www.youtube.com/watch?v=fpeS-HkaYHU

Code:
init:
	
	pwmout 2,255,0

main:

	readadc 0,b0

	readadc 1,b1

	readadc 2,b2

	b3 = 4

	w1 = b2 * b3

	if pin0 = 1 and pin1 = 1 then flashhazard

	if pin0 = 1 then flashleft

	if pin1 = 1 then flashright

	if pin5 = 1 then marker

	if pin5 = 0 then init

marker:

	pwmduty 2,w1

	if pin5 = 0 then main

	goto main


flashleft:

	high portc 7

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 7

	goto main

flashright:

	high portc 6

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 6

	goto main

flashhazard:

	high portc 7

	high portc 6

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 7

	low portc 6

	goto main
I'm sure there's a better way to write it out.. but not one I've found yet.. I tried this:

Code:
symbol Loop1 = b3

main:
	low portc 7
	low portc 6
	
	readadc 0,b0
	readadc 1,b1
	readadc 2,b2

	pwmout 2,63,b2
	
	if pins > 0 then flash
	
	goto main
	
Flash:
	select pins
		case 1	'leftflash
			high portc 7
		case 2	'lrightflash
			high portc 6
		case 3	'hazardflash
			high portc 7
			high portc 6
	endselect
	for Loop1 = 0 to 7
		high Loop1
		pause b0
	next Loop1
	for Loop1 = 7 to 0 step -1
		pause b1
		low Loop1
	next Loop1
	pause b1
	pause 50
	goto main
Suggested by someone else.. but there's about a half second delay before each flash when the input is received.

The only problem I'm having is there's now some capacitance going on somewhere or something?.. and when I fire it up the red LEDs are very dimly lit until I flash a signal.. and sometimes they pulse a little.. hmm.. I have pin 4 on the reg (the pin the PWM signal goes to) tied down with a 10k.. ground keeps it off, should I maybe try a 4k7 instead? that would allow more current to leak... little reminder this is my first electronics project, so I am very new here...

If I could deal with the very slight pulsing on the marker lights even when pin 5 isn't high (it's tied down with a 10k aswell) then I could get rid of the capacitance problem at start up by just leaving the pixace powered all the time, I was thinking I would do this anyway.. because the marker lights should be able to come on while the car is off as per stock operation.. and the picaxe circuit only draws 2.59mA at idle, perfectly acceptable for a car. Is there any detriment to the picaxe leaving it powered 24/7?

Maybe after I solder on the 0.33uF on the input of the reg I won't get any pulsing.. we'll see then... it's only very faint, barely visible.. and it doesn't happen all the time.. I dunno what's causing it.
 
Last edited:

BCJKiwi

Senior Member
In your second program you should move the pwmout 2,63,b2 ahead of main:

The logic is a little different at the front end to decide when to go into the flash routines but its not obvious why there should be along delay.
Do you need the last two pauses at the end?

Suggest you try one change at a time i.e. use the old 'if pin0=' etc instead of the case statements with the loops. When that's working, try adding the case statements - this will help to determine where the delay is occurring.
 

Shafto

Senior Member
Ya like I say I didn't write it, it was a suggestion to me.. ignore the PWM, I should have erased it, that's totally different now anyway...

I dunno.. mine might be longer but I'm fine with it.. it works well.
 

Shafto

Senior Member
I'm having trouble figuring out a way to code the PWM to turn on and off with a digital input while not executing the PWMout command over and over..

If pin5 is high then I want the PWM on, and low, then off.. but I don't know how to code it to just do the PWMout command once when the pin goes high and keep checking the pin.. but not keep executing PWMout over and over.. just keep executing the PWMduty command after PWMout has been done once.

Then when pin5 goes low execute PWMout off.

I had it working before setting just using the duty command and setting it to 0.. but I'm pretty sure that was the source of the slight pulsing I was seeing.
 
Last edited:

BCJKiwi

Senior Member
You would need to execute the
PWMOUT pin OFF
command at the approriate part of your program. You would then need to restart with the full
PWMOUT pin,period,duty cycles
command then continue with the
pwmduty.

i.e. after the marker: label, replace the pwmduty with
PWMOUT pin,period,duty cycles
then just before the goto main
PWMOUT pin OFF.
 

Shafto

Senior Member
Well I've been experimenting but still haven't been able to come up with a solution.. I thought turning PWM on and off wouldn't be hard.. but then the problem of not reissuing the PWMout command is turning into quite the pain for a beginner like me.

I've tried this, which I think should work, but it doesn't, PWM won't start.

Code:
main:

readadc 2,b2

pwmduty 2,b2

let pin5 = b4

if b4 > b5 then

pwmout 2,255,1

b5 = b4

else if b4 = 0 then

pwmout 2 off

end if

goto main
can someone point me in the right direction here? I'm sure this can be done.. I just can't figure it out.

When pin5 goes high it's greater than b5, so why doesn't the PWMout command issue?
 
Last edited:

BCJKiwi

Senior Member
There appears to be two conflicting concepts at work here.

The idea behind the pwmduty is that you can set up the pwm's timer function (the frequency) once in the initialisation of the program and then vary the output with pwmduty as required - see my program and your second code block in post #17. PWM is running continuously when you do this. In my code I am establishing a supply rail with the pwm output and supplying downstream logic for the LEDs from there.

In your program, because you want to turn on and off the supply regulator via pwm itself then you must start and stop pwm - the pwmduty is of no use to you here.

Without your circuit diagram I don't know how you are actually supplying and controlling all the components so I don't know how to advise you further.

It's also not clear to me why you are actually using pwm in this application. If these are vehicle lights then I would have thought each type of light would have a fixed output intensity. This could be acheived by a combination of the voltage, LED type and dropping resistor. You would then only need to use the PICAXE to control on/off and flash type functions - no pwm required.

Perhaps its time to see the actual circuit diagram.

I have just re-read you post #5 Turning on/off the regulator as you are doing is not the same as turning on/off the PWM Load device. the transistor in the regulator shuts down the regulator. A transistor downstream of the regulator just controls the load and leaves the regulator running. Thsi was my concern back in post #4.
 
Last edited:

Shafto

Senior Member
Hmm.. my circuit is fine.. and I don't understand why it needs to be involved.. there is no problem using these regs for PWM uses.. that all works with no problems and isn't of concern.

I just need to know how I can issue the PWMout command once when pin5 goes high, and then not issue that command again unless pin5 were to go low, which would turn off PWM, and then go high again to turn it back on... just run PWMout once.. then then PWMduty from there out to continue to adjust the PWM (if it is on) according to b2.

This is purely a coding question.. if the PWM command is issued my circuit works as designed, no problems there... I just can't figure out how to code this to work.
 

hippy

Technical Support
Staff member
I just need to know how I can issue the PWMout command once when pin5 goes high, and then not issue that command again unless pin5 were to go low, which would turn off PWM, and then go high again to turn it back on... just run PWMout once.. then then PWMduty from there out to continue to adjust the PWM (if it is on) according to b2.
This should do what you want and avoids any 'race conditions' which could occur with multiple reads of pin 5; ie, it only reads pin 5 once per loop ...

Code:
Do
  ReadAdc 2, b2                 ' Read the PWM duty rate
  If pin5 = b4 Then             ' No change in pin 5
    If b4 = 1 Then              '   Pin 5 is high ...
      PwmDuty 2, b2             '   ... Update duty only
    End If
  Else                          ' Pin 5 changed
    b4 = b4 ^ 1                 '   Update b4 to reflect pin 5
    If b4 = 1 Then              '   Pin 5 went high ...
      PwmOut 2, something, b2   '   ... Start PWM
    Else                        '   Pin 5 went low ...
      PwmOut 2, 0, 0            '   ... Turn off PWM
    End If
  End If
Loop
 
Last edited:

BCJKiwi

Senior Member
@Shafto
I was just trying to give you a better response as you are having issues with flickering, are switching the regulator itself (which is not the same as switching the load) and are having issues getting the code to work as well as you would like.

A circuit helps to get the whole project into perspective in a case like this and to provide alternative recommendations but I appreciate that you may not wish to share your circuit.
 

Shafto

Senior Member
Sorry if that came off wrong, it's not that I do not wish to share it, I just haven't drawn it, I breadboarded the whole thing, I find it much easier to design something when I'm actually doing it... though now I am in the process of drawing it up so I can make the PCB..

hippy.. thank you thank you thank you! it works flawlessly.. I still don't quite understand why mine didn't work, but I'm glad to come to a conclusion. Just so I can understand a little better what I'm doing, why the 'do' 'loop' commands? I notice taking them out has no effect, what is the purpose?.. I am now using this as my full code and everything works as per the plan:

Code:
main:
	readadc 0,b0

	readadc 1,b1

	readadc 2,b2

	b3 = 4

	w1 = b2 * b3



  If pin5 = b4 Then             ' No change in pin 5
    If b4 = 1 Then              '   Pin 5 is high ...
      PwmDuty 2, b2             '   ... Update duty only
    End If
  Else                          ' Pin 5 changed
    b4 = b4 ^ 1                 '   Update b4 to reflect pin 5
    If b4 = 1 Then              '   Pin 5 went high ...
      PwmOut 2, 255, w1   '   ... Start PWM
    Else                        '   Pin 5 went low ...
      PwmOut 2 off            '   ... Turn off PWM
    End If
  End If


	if pin0 = 1 and pin1 = 1 then flashhazard

	if pin0 = 1 then flashleft

	if pin1 = 1 then flashright

	if pin0 = 0 and pin1 = 0 then main



goto main

flashleft:

	high portc 7

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 7

	goto main

flashright:

	high portc 6

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 6

	goto main

flashhazard:

	high portc 7

	high portc 6

	high 0

	pause b0

	high 1

	pause b0

	high 2

	pause b0

	high 3

	pause b0

	high 4
	
	pause b0

	high 5

	pause b0

	high 6

	pause b0

	high 7

	pause b1

	low 7

	pause b1

	low 6

	pause b1

	low 5

	pause b1

	low 4

	pause b1

	low 3

	pause b1

	low 2

	pause b1

	low 1

	pause b1

	low 0

	pause b1

	pause 50

	low portc 7

	low portc 6

	goto main
Thanks again! I just have one more question that was maybe missed that I posted a bit earlier about the picaxe being powered 24/7, any detrimental effects to that? It's only drawing 2.59mA at idle so that's no problem on the car battery..
 
Last edited:

hippy

Technical Support
Staff member
The Do-Loop is really just the same as "main: .... Goto main", simply there to show the code goes round in circles, equivalent to a "Do Forever" loop.

There shouldn't be any problem running 24/7 but the less drain on the battery the better. What you can do when 'nothing is happening' is to either switch to a lower operating speed or enter sleep for a while ( or do both ) and then set the speed back to what it should be whenever something needs to be done. Altering operating speed is a little long-winded to repeat here but a search on "OSCCON" should turn up useful discussion, and underrstanding the PICmicro datasheet will help.

If you have an ignition line sense you could use that to determine if the PICAXE should be sleeping or not, or you could just power it via the ignition switch ;-)
 

hippy

Technical Support
Staff member
For your flasher code, I'd change that to something like ...

Code:
  b13 = pins & 3
  Select Case b13
    Case %01 : Goto FlashLeft
    Case %10 : Goto FlashRight
    Case %11 : Goto FlashHazard
  End Select
  Goto Main
Because you read the pins multiple times there's a chance that if the two pins do not go high exactly together ( which is possible even if wired together ! ) or the code has just passed the check for hazard flashing before the pins go high you could put a turn flash on instead of a hazard and have to wait for the turn to finish before the hazards come on.

It's hard to quantify what the risk of that happening is, but Sod's Law says you'll be unlucky when you come to need it.
 

Shafto

Senior Member
I just want to retain the stock operation.. which is that the signal lights, brakes, and markers can all be turned on without the car on..

I'm going to measure the current parasitic draw on the vehicle and compare to that what I'll be adding to to know.. but I'm sure 2.59mA will be ok.

I haven't overclocked it or anything.. it's only running at the regular 4mhz, so I dunno if that's what you were referring to when talking about slowing it down.. unless you meant slowing it beyond that.

I just noticed that I can actually have 2 totally different programs on this chip while peroozing the manual... I really don't think I need to spend too much time getting it down from that 2.59mA, but if it was as easy as running a second program that made it sleep if there were no inputs for an hour or so... and then make it wake up from any input? (switch back to the main program) ..I'll look into it.

I can't get your version of the flasher code to work.. the left signal will flash if I hold both inputs high (which should be hazard) but nothing else works.. I don't think it's really that touchy, because I just have 2 momentary push buttons on here and I can synchronize my button pushing to get them to fire at the same time, surely if my thumbs can do it electrical impulses can.... but I do see your logic.
 
Last edited:

kevrus

New Member
It doesnt matter how accurate you think you are with the manual button pushes, there will always be one push that will lead the other, (often used for priority selection in quizes..i.e. who pressed first).
Maybe you could add a line of code to the left and right subroutines to check if the other input (button push) is high and if so then jump to the hazard routine.
As a matter of interest, are these lights road leagal where you are?
 

Shafto

Senior Member
hmm... I wasn't claiming to be accurate with pushing the buttons... I was using how inaccurate that is to show that when I push the hazard light button both inputs will be triggered even more accurately.. thus why I don't think I should have a problem with it, but there is no reason why I shouldn't improve the code either.. I just couldn't get it to work.. I'll play with the case situation a little more.

Legal? no.. but those laws are meant to stop people from doing stupid things.. if I build them well enough then they won't be noticed, and I won't be bothered about them.. and then their legality isn't really an issue to me... I'm using Lumileds Superflux LEDs, DOT approved amber and red/orange used by OEM manufacturers.. almost all of them (the good ones anyway)

Lighting infractions like PNP HID kits in halogen reflector housings not designed for HID that throw massive glare in everyone's eyes is something of concern... (and largely not dealt with here.. though not a huge problem either)

Leds with their instant on action compared to incandescent lights can save you 20 meters stopping distance at highway speeds.. and because they suck less power, ultimately your alternator works less, and you use less fuel...

There will be no incandescent bulbs on my car very soon.. only LED, and the halogen custom retrofitted to accept DOT OEM HID projectors... 40watts less consumption on headlights alone, and 6800lumen vs 2000lumen with halogen.
 

Shafto

Senior Member
Alright, got my SMT cap soldered right onto the leads of the reg, they said put it as close as possible, so I did.. hah.. though my main drive for that was the fact that the SMT version of the 47uF industrial grade low ESR tantalum caps were far cheaper than any suitable axial or radial version.. and had better characteristics as well... I also decided to go this route for all of my 9V regs for the LEDs that use a 0.33uF tantalum on the inputs, there's a pin in between though, so I modified the legs on the caps, I just bent the out from underneath and now they span the 1 pin gap... I took a pic, but photobucket is acting up.. I'll post it as soon as it starts working again.
Alright.. finally it's working, I know it's all kinda over now but I said I would put up the pics.. 47uF on the 5V reg, and 0.33s on the 9V regs.. I like this, less board space and cheaper.. Anyone else every do it this way?



 

hippy

Technical Support
Staff member
Neat trick. I doubt the caps will unsolder themselves as the regs are soldered up and easy enough to fix if they do.
 

Shafto

Senior Member
Thanks, I had thought about that.. I think I'll just tie a little wire around the pins to hold the cap there.. if the solder does melt as they go onto the board the cap will still be held in place.
 
Top