-->
Page 1 of 1

Cheerlights with Neopixels.

PostPosted: Sat Aug 19, 2017 1:55 pm
by AndyGadget
Here is the latest iteration of my program for reading the current Cheerlights colour via Thingspeak and displaying it on neopixels. It also uses the ThingTweet API to tweet a random colour every now and then. The neopixels are installed in a 3D printed cat who glows very nicely. Processor is a 'yellow' ESP8266 board lurking in the background. I use the Twitter handle ArmyOfThings to tweet from.

GlowyCat.jpg


Code: Select all' *************************************************
' ESP8266 Cheerlights for V3 Basic             V1.2
' Now tweets colours to Cheerlights
' *************************************************

' Set up Neopixel strip.
neo.setup(15)
neo.cls()

' Initialise colour values
redval = 0
grnval = 0
bluval = 0

loopcnt = 0

' API key from Thingspeak.
key$ = "Your Thingspeak Key Here"

' Set up array of messages. # is placeholder for colour.
dim optext(10) as string
optext(0)="Let's make it all go #"
optext(1)="I'm in the mood for #"
optext(2)="I feel some # coming on"
optext(3)="Bringing a little # into your life"
optext(4)="#, #, everything's going #"
optext(5)="Let's paint the whole world #"
optext(6)="The next colour will be . . . #"
optext(7)="# is my favourite colour"
optext(8)="Look out! There's some # on the way!"
optext(9)="Let's try a bit of #"

' Set up array of colours
dim colours(11) as string
colours(0)="red"
colours(1)="green"
colours(2)="blue"
colours(3)="magenta"
colours(4)="cyan"
colours(5)="yellow"
colours(6)="pink"
colours(7)="orange"
colours(8)="purple"
colours(9)="warmwhite"
colours(10)="white"

rf = ramfree()
ff = flashfree()

do
   ' Retrieve current hex colour code from
   ' Thingspeak Cheerlights API in form #rrggbb.
   '(Key value can be any string)
   colcode = readts("xxx","1417","2")
   curcol$ = readts("xxx","1417","1")

   ' Store old colour values
   ' These will be modified in fade loop.
   redvalx = redval
   grnvalx = grnval
   bluvalx = bluval

   'Extract R, G and B bytes from new colour code.
   redval = hextoint(mid(colcode,2,2))
   grnval = hextoint(mid(colcode,4,2))
   bluval = hextoint(mid(colcode,6,2))

   ' Fade routine - output colours increment / decrement to new colour.
   do
      if redvalx > redval then redvalx = redvalx - 1
      if redvalx < redval then redvalx = redvalx + 1
      if grnvalx > grnval then grnvalx = grnvalx - 1
      if grnvalx < grnval then grnvalx = grnvalx + 1
      if bluvalx > bluval then bluvalx = bluvalx - 1
      if bluvalx < bluval then bluvalx = bluvalx + 1

      ' Send intermediate colour to neopixels
        ' I'm using a 7 neopixel disc.
      neo.stripcolor(0,6,redvalx,grnvalx,bluvalx)
   
   ' Exit loop when intermediate R, G and B values match new colour.     
   loop until (redvalx = redval) and (grnvalx = grnval) and (bluvalx = bluval)

    delay 10000
   
    ' Randomly choose a new colour number and load text equivalent from array
    newcol = rnd(11)
    col$ = colours(newcol)
      
    ' Counter to provide time delay between colour tweets
    loopcnt = loopcnt + 1
   
    ' Program loops every 15 seconds so loopcnt of 80 is 20 minutes.
    ' Rnd(120) gives 30 minute variation on top on this.
    if rnd(120) == 0 and loopcnt > 80 and instr(col$,curcol$) = 0 then
       loopcnt = 0
       newtxt = rnd(10)
       twitstr$ = optext(newtxt)
   
       ' Replace # placeholder in string with new colour name
       twitstr$ = replace(twitstr$,"#",col$)   
       ' Assemble the string to be sent . . .
       twitstr$ = "@Cheerlights cat says '" & twitstr$ & "'."
       ' And send it to the ThingSPeak API.
       opstring = "api.thingspeak.com/apps/thingtweet/1/statuses/update?api_key=" & key$ & "&status=" & twitstr$
      print wget(opstring)      
       returngui
   end if

   delay 5000
loop until 0