Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 18:44 26 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Christmas LED Lights routine, Time Slicing?

Author Message
GoodToGo!

Senior Member

Joined: 23/04/2017
Location: Australia
Posts: 188
Posted: 10:44am 27 Dec 2019
Copy link to clipboard 
Print this post

Howdy all, and I hope you are all having a great festive season!

Anyway, one of my christmas light crappy multi-function controllers shat itself.
So enter an Explore 28 module! I designed a quick circuit and roughed up some code to imitate some of the routines the crappy 3-wire multi function controller used to do:-

  Quote  'Xmas lights flashing routine v2.1
'by GoodToGo! 06/01/2018
'Program to replicate xmas lights 3-wire multi-function controller via PWM on a Micromite. (driving IRL540N MOSFETS)

'Version 1.0 06/01/18 Rough proof of concept. Tested ok, but never used in actual display.
'Version 2.0 15/12/19 Created Chase routine. Fine tuned other routines. Used to control front fence/gate LED strings in December 2019 display.
'Version 2.1 27/12/19 Cleaned up code, created/fixed up comments.

'pwm format = PWM 2, freq, 2a, 2b where 2a , 2b is the dutycycle

option autorun on
option explicit
option default integer

'********** Setup variables **********

dim i, j, k                             'general variables
dim DutyCycle, DutyCycle2               'variables for the DutyCycles
Dim Channel1 = 26                       'variable for LED string 1
Dim Channel2 = 24                       'variable for LED string 2

'********** Main Loop **********

do                                    
 Twinkle
 Chase
 Flash
 SlowFade
loop

'********** Twinkle Routine **********

' Creates a twinkle effect by alternately increasing one LED string in brightness while decreasing the other.
' The strings never turn fully off.

sub Twinkle
 
Print "Twinkle"
 
for i = 1 to 40                           'loop  the routine 40 times
   DutyCycle2 = 90                         'start LED string 2 dutycycle at 90%
   for DutyCycle = 5 to 90 step 5          'LED string 1 duty cycle in %, increasing in 5% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'pulse width modulation, pwmchannel = 2, frequency = 10khz, duty cycle = variable
     pause 20                              'wait 20 milliseconds
     DutyCycle2 = DutyCycle2 - 5           'decrease LED string 2 dutycycle by 5%
   next DutyCycle                          'increase LED string 1 dutycycle
   DutyCycle2 = 5                          'start LED string 2 dutycycle at 5%
   for DutyCycle = 90 to 5 step -5         'LED string 1 duty cycle in %, decreasing in 5% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'pulse width modulation, pwmchannel = 2, frequency = 10khz, duty cycle = variable
     pause 20                              'wait 30 milliseconds
     DutyCycle2 = DutyCycle2 + 5           'increase LED string 2 dutycycle by 5%
   next DutyCycle                          'decrease LED string 1 dutycycle
 next i                                    'loop back to start
end sub

'********** Slow Fade Routine **********

' Creates a fade out effect by turning both LED strings on full and then slowly reducing the brightness until they are fully off.
' Then it slowly increases the brightness to full. Cycle repeats.

sub SlowFade
 
print "SlowFade"
 
for i = 1 to 3                            'loop the routine 3 times
   DutyCycle = 100                         'start both LED strings dutycycle at 100%
   pwm 2, 10000, DutyCycle, DutyCycle      'pulse width modulation, pwmchannel = 2, frequency = 10khz, duty cycle = variable
   pause 3000                              'wait 3 seconds
   for DutyCycle = 100 to 0 step -1        'the dutycycle for both LED strings, decreasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle    'pulse width modulation, pwmchannel = 2, frequency = 10khz, duty cycle = variable
     pause 20                              'wait 20 milliseconds
   next DutyCycle                          'decrease both LED strings dutycycle
   pause 2000                              'wait 2 seconds
   for DutyCycle = 0 to 100 step 1         'the dutycycle for both LED strings, increasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle    'pulse width modulation, pwmchannel = 2, frequency = 10khz, duty cycle = variable
     pause 20                              'wait 20 milliseconds
   next DutyCycle                          'increase both LED strings dutycycle
 next i                                    'loop back to start
end sub

'********** Flash Routine **********

' Flashes each LED string in turn a few times, then alternates between the two. Switches between full on and full off.

sub Flash
 
print "Flash"
 
pwm 2, stop                               'shutdown pulse width modulation
 setpin Channel1, dout                     'set LED string 1 to digital output
 setpin Channel2, DOUT                     'set LED string 2 to digital output
 for k = 1 to 3                            'loop the routine 3 times
   for j = 1 to 3                          'loop first part of routine 3 times
     for i = 1 to 3                        'loop to flash LED string 1 first
       pin(Channel1) = 1                   'turn LED string 1 on
       pause 60                            'wait 60 milliseconds
       Pin(Channel1) = 0                   'turn LED string 1 off
       pause 60                            'wait 60 milliseconds
     next i                                'flash LED string 2 on/off again
     for i = 1 to 3                        'loop to flash LED string 2 next
       pin(Channel2) = 1                   'turn LED string 2 on
       pause 60                            'wait 60 milliseconds
       Pin(Channel2) = 0                   'turn LED string 2 off
       pause 60                            'wait 60 milliseconds
     next i                                'flash LED string 2 on/off again.
   next j                                  'flash LED string's 1 & 2 on/off again
  for i = 1 to 6                           'loop following alternate flash 6 times
     pin(Channel1) = 1                     'turn LED string 1 on
     pin(Channel2) = 0                     'turn LED string 2 off
     pause 150                             'wait 150 milliseconds
     pin(Channel1) = 0                     'turn LED string 1 off
     pin(Channel2) = 1                     'turn LED string 2 on
     pause 150                             'wait 150 milliseconds
     pin(Channel2) = 0                     'turn LED string 2 off
   next i                                  'flash LED strings 1 & 2 alternately again
 next k                                    'loop back to start
end sub

'********** Chase Routine **********

' Slowly turns the first LED string on, then the other. Then slowly turns the first LED string off, then the other.
' This cycle repeats but the cycle gets faster and faster because the wait time gets reduced every run of the loop.

sub Chase
 
Print "Chase"
 
pause 200                                 'wait 200 milliseconds
 for i = 100 to 10 step -20                'variable "i" is the delay time in milliseconds decreasing in 20 millisecond steps
   DutyCycle2 = 0                          'Set LED string 2 dutycycle to 0% for start  
   for DutyCycle = 0 to 70 step 1          'LED string 1 duty cycle in %, increasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle                          'increase LED string 1 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
   for DutyCycle2 = 0 to 70 step 1         'LED string 2 duty cycle in %, increasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle2                         'increase LED string 2 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
   for DutyCycle = 70 to 1 step -1         'LED string 1 duty cycle in %, decreasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle                          'decrease LED string 1 dutycycle
   for DutyCycle2 = 70 to 0 step -1        'LED string 2 duty cycle in %, decreasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle2                         'decrease LED string 2 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
 next i                                    'decrease delay variable "i"
 
 
for i = 9 to 1 step -1                    'variable "i" is the delay time in milliseconds
   DutyCycle2 = 0                          'Set LED string 2 dutycycle to 0% for start  
   for DutyCycle = 0 to 70 step 1          'LED string 1 duty cycle in %, increasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle                          'increase LED string 1 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
   for DutyCycle2 = 0 to 70 step 1         'LED string 2 duty cycle in %, increasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle2                         'increase LED string 2 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
   for DutyCycle = 70 to 1 step -1         'LED string 1 duty cycle in %, decreasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle                          'decrease LED string 1 dutycycle
   for DutyCycle2 = 70 to 0 step -1        'LED string 2 duty cycle in %, decreasing in 1% steps
     pwm 2, 10000, DutyCycle, DutyCycle2   'Pulse width modulation, channel = 2, frequency = 10khz, duty cycle = variable
     pause i                               'wait "i" milliseconds
   next DutyCycle2                         'decrease LED string 2 dutycycle
   pause i * 3                             'wait 3 times "i" milliseconds
 next i                                    'decrease delay variable "i"
end sub



Works great and is happily driving the 3-wire LED strings on my fence. However I would like to drive another set of 3-wire strings using the other PWM channel in the MM as well. But having the channels run different routines at the same time. e.g. the first 3-wire string on PWM channel 2a/b running, say, the slowfade routine, while the PWM channel 1a/b is running the chase routine.
Is this at all possible? Do I have to timeslice somehow?

Cheers,

GTG!
...... Don't worry mate, it'll be GoodToGo!
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2870
Posted: 12:48am 28 Dec 2019
Copy link to clipboard 
Print this post

Hi GTG,

Any video clip of it working.

I have a retiring reindeer that I want to re-jig for next years Christmas either with discrete LEDs or simply the LEDs with inbuilt colour change flashing cct.

Kind Regards

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 639
Posted: 02:12am 29 Dec 2019
Copy link to clipboard 
Print this post

G'Day GTG

I know this will get me shot down but.
Most of the time your MM is doing nothing, pause x, so can you take advantage of that.

Good luck

Peter
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1593
Posted: 03:08am 29 Dec 2019
Copy link to clipboard 
Print this post

As PeterB said, there is a lot of PAUSEing in the program. Maybe it could be re-written to use timer interrupts? The thought of it makes me shudder. I would just use another Micromite.

Thanks for sharing the code though. I'd love to see a schematic of the hardware too.

Bill
Keep safe. Live long and prosper.
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 639
Posted: 03:26am 29 Dec 2019
Copy link to clipboard 
Print this post

Now I am sticking my neck out

If you time slice into 10ms blocks you should be able to do quite a lot in a block and it should look good.

Peter
 
GoodToGo!

Senior Member

Joined: 23/04/2017
Location: Australia
Posts: 188
Posted: 10:17am 29 Dec 2019
Copy link to clipboard 
Print this post

Howdy all, a couple of pictures and a schematic for you all:-








As you can see, there's not much to it. An ebay-special buck converter drops the incoming 32Vdc down to 5Vdc for the E28 module. The 32Vdc then just goes to the connector for the LED string. The two negative sides on the strings are driven by 2 logic level N-channel IRL540N MOSFET's, whose Gates are connected to pins 24 & 26 of the E28. A 10K resistor connects each Gate to ground.
BigMik, I'll try to get some video footage for you......

Cheers,

GTG!
...... Don't worry mate, it'll be GoodToGo!
 
GoodToGo!

Senior Member

Joined: 23/04/2017
Location: Australia
Posts: 188
Posted: 11:14am 29 Dec 2019
Copy link to clipboard 
Print this post

  bigmik said  Hi GTG,

Any video clip of it working.

I have a retiring reindeer that I want to re-jig for next years Christmas either with discrete LEDs or simply the LEDs with inbuilt colour change flashing cct.

Kind Regards

Mick


Uploaded a video clip here.
Forgive the blurriness of it, the phone had trouble focusing....

Cheers,

GTG!
...... Don't worry mate, it'll be GoodToGo!
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2870
Posted: 01:23am 01 Jan 2020
Copy link to clipboard 
Print this post

Hi GTG,

I like it..

Dismantling our Christmas tree (sits out the front) I noticed a similar circuit driving the LEDs from 31Vdc... If the controller ever dies I will steal your idea.. In fact I think your display looks better than the original ... well at least in my case.

Kind Regards

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024