Project - Colour Changer 2

This colour-changing mood light effect might be described as a colour 'bounce'. It fades up to the brightness setting, waits for the Top pause, then fades back down to Off, waits for the Bottom pause, then repeats the cycle with the next colour  (see demo video)
.
The colours are assigned in a list, and can either be sequentially cycled, or randomly chosen.
Either way, the next colour should always be different to the present colour to keep it more interesting.
If you prefer not to include white cos its not interesting enough, simply delete it from the list.
The brightness, colour change speed, and top and bottom pauses, are all individually adjustable.
The 'bounce' action can be toggled On or Off to freeze the bounce at any time.
All the defaults are commented at the top of the script for easy modification if wished.
It is designed to work with neo-pixels or Sonoff B1 lamps - just set the required type=1 in the script.
The script was developed on Annex 1.41 beta 2, so needs that version or later for all features to work.
It may eventually be combined with the previous colour 'blend' into a 'mood lighting' script - some of the similarities are repeated below... 
The Brightness control reduces the peak brightness level by decreasing the required number of discrete steps. This also affects the colour-change speed, because although the rate of change stays the same, there will be a differing number of steps to sequence through.This is easily shown by setting the Speed slider at it fastest (right) while first trying the Brightness at dim (left), then turning up the brightness.

The startup speed and brightness can be set at the top of the script, if the optional title$ is non-blank it will be displayed.

You can set the script name into Config Autorun for automatic startup rather than needing to Run it from the browser each time.
In which case you may wish to enter credentials to automatically log on to your router and display the Output control page.
So a word of caution for Sonoff B1 users... be sure you can connect to the device in the browser before you set it to Autorun, because shorting TX and RX together to prevent Autorun on a Sonoff lamp is not quite so straightforward as on other devices.

Make sure to set either neo=1 or sonoff=1 according to which type of RGB device you are using, otherwise you won't see anything.
neo=1                                             'set=1 if using neo-pixels
sonoff=0                                          'set=1 if using a Sonoff B1 Lamp

Basic:
'Colour Bouncer for NeoPixels or Sonoff B1 Lamps - Electroguard - Developed on Annex 1,41 beta 2
title$="RGB Colour Bounce"              'title will be displayed if not blank
neo=1                                              'set=1 if using neo-pixels
sonoff=0                                           'set=1 if using a Sonoff B1 Lamp
l=50                                                 'brightness (Lux) from 0 (Off) to 255 (max)
bounce=1                                         '1=bounce On, 0=bounce Off
speed=30                                         'pause between individual bounce steps
up=0                                                'bounce direction            
top=1300                                          'top of bounce pause                            
bottom=500                                      'bottom of bounce pause
random=1                                         '0=sequential colours, 1=random colours
r=0:g=0:b=0:c=0                               'startup defaults
if neo=1 then                                     'data-in should be connect to gpio2
 pixels = 12                                       'declare how many pixels to be addressed (12 for this example)
 neo.setup pixels                               'initalise for declared number of pixels
 neo.strip 0, pixels-1 ,r,g,b                  'turn all pixels off
endif
if sonoff=1 then
 sonoffb1.init                                      'initialise Sonoff B1 Lamp
 sonoffb1.rgb r,g,b                              'display default RGB values
 sonoffb1.white 0,0                             'display default white values
endif
colours$="red,green,blue,yellow,magenta,cyan,orange,purple,white"
colours=word.count(colours$,",")
colour=rnd(colours)+1
colour$=word$(colours$,colour,",")
old$=""
HtmlEventVar$=""
onhtmlchange change                         
gosub screen                                     
gosub bounce
wait

getcolour:
if random=1 then
 old$=colour$
 do
  colour$=word$(colours$,rnd(colours)+1,",")
 loop until colour$<>old$
else
 colour=colour+1
 if colour>colours then colour=1
 colour$=word$(colours$,colour,",")
endif
gosub colours
rc=r/l: gc=g/l: bc=b/l
return

bounce:
while bounce=1
do
 if c=0 then gosub getcolour
 if up=1 then c=c+1: if c>=l then c=l: up=0: pause top
 if up=0 then c=c-1: if c<=0 then c=0: up=1: pause bottom
 if sonoff=1 then cmd$="sonoffb1.rgb R,G,B"
 if neo=1 then cmd$="neo.strip 0, pixels-1,R,G,B"
 cmd$=replace$(cmd$,"R",str$(c*rc))
 cmd$=replace$(cmd$,"G",str$(c*gc))
 cmd$=replace$(cmd$,"B",str$(c*bc))
 command cmd$
 pause speed
wend
return

change:
if HtmlEventVar$="l" then gosub colours
if bounce=1 then gosub bounce
HtmlEventVar$=""
return

colours:
select case colour$
case "red":    r=l: g=0: b=0:
case "orange": r=l: g=int(l/2): b=0:
case "green":  r=0: g=l: b=0:
case "blue":   r=0: g=0: b=l:
case "yellow": r=l: g=l: b=0:
case "magenta":r=l: g=0: b=l:
case "purple": r=int(l/2): g=0: b=l:
case "cyan":   r=0: g=l: b=l:
case "white":  r=l: g=l: b=l:
end select     
return

screen:
cls
a$=""
a$=a$+|<br>|
a$=a$+|<div style='display: table; margin-right: auto; margin-left: auto;font-size: 110%'>|
if title$<>"" then a$=a$+|<font color="teal">|+title$+|</font><br>| else a$=a$+"<br>"
a$=a$+|<br>|
a$=a$+|</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;'>|
a$=a$ + checkbox$(bounce)
a$=a$ + |<br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 80%'> Bounce|
a$=a$ + |<br><br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;'>|
a$=a$ + checkbox$(random,"random")
a$=a$ + |<br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 80%'> Random|
a$=a$ + |<br><br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;'>|
a$=a$ + replace$(slider$(l, 0, 255,"L"), "onchange", "oninput") + |<br>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 80%'>|
a$=a$ + |<font color="black">|+" Brightness"+|</font><br><br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto; transform: rotate(180deg);'>|
a$=a$ + replace$(slider$(Speed,0,999,"S"), "onchange", "oninput") +|<br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 80%'>|
a$=a$ + |<font color="black">|+" Speed"+|</font><br><br>|
a$=a$+|</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;'>|
a$=a$ + replace$(slider$(top, 0, 5000,"P"), "onchange", "oninput") + |<br>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 70%'>|
a$=a$ + |<font color="black">|+" Top pause"+|</font><br><br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;'>|
a$=a$ + replace$(slider$(bottom,0,3000,"P"), "onchange", "oninput") +|<br>|
a$=a$ + |</div>|
a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 70%'>|
a$=a$ + |<font color="black">|+" Bottom pause"+|</font><br><br>|
a$=a$ + |</div>|
a$=a$ + cssid$("P","width:90;")
html a$
return

end  '-----------end------------






Comentarios