Post about your Basic project here

Moderator: Mmiscool

User avatar
By AndyGadget
#37772 Hi All - My first post here.
MM - I like what you're doing. Great work!

I've recently been reading about the IOT and came up with a few ideas for projects so I ordered a 'yellow' 8266 test board to have a play with and have spent the last few hours getting to know the quirks and foibles of the 8266 and the 8266 Basic.
[url]Cheerlights[/url] caught my attention and I thought I'd use this as a simple first project. Getting data from the ThingSpeak AOI was laughably easy with ESP8266 Basic and thanks to the extremely free and easy type definitions I could easily do the hex code to decimal conversions (but see my feature request) I needed to drive the PWM outputs.

To use this program properly with standard RGB LEDs you'd need to tweak the resistor values to get more accurate colours from the colour codes, but the plan is to drive a NeoPixel array (ordered yesterday) with these which can use the codes directly with no hex to decimal conversion and will hopefully give a better colour rendition.

I've listed the code twice - First with comments and then a stripped down version for copying to the 8266.

Code: Select all' *****************************************************
' Andy's 8266 CHEERLIGHTS proggie for yellow test board
' V1.00
'
' Colours are off for RGB LED as supplied but could be
' made more accurate by resistor colour value matching.
' Should be more acccurate for NEOPIXEL LEDs which can
' use the colour codes directly with no conversion.
' *****************************************************
'
' Pin definitions for RGB LED outputs
RedPin = 15
GrnPin = 12
BluPin = 13
'
'Lookup string for hex to decimal conversion subroutine
HexConv = 123456789ABCDEF
'
TOP
delay 5000
' Get current Ccheerlights colour code from ThingSpeak API
colcode = readts(key,1417,2)
'
' Simple check to make sure 7 character string returned
if len(colcode) <> 7 then goto TOP
'
' Extract red, green then blue codes from string
' and convert to decimal equivalent.
Tmp1 = mid$(colcode,2,2)
gosub TODEC
RedVal = DecVal
Tmp1 = mid$(colcode,4,2)
gosub TODEC
GrnVal = DecVal
Tmp1 = mid$(colcode,6,2)
gosub TODEC
BluVal = DecVal
'
' Set PWM for each colour
pwo RedPin RedVal
pwo GrnPin GrnVal
pwo BluPin BluVal
'
goto TOP
'
'
'
TODEC
' Extract left then right character from hex pair
LSB = left(Tmp1,1)
MSB = right(Tmp1,1)
' and look for position in hex lookup string
LSB = instr(HexConv,LSB)
MSB = instr(HexConv,MSB)
' Calculate decimal value (16*MSB + LSB)
DecVal = MSB * 16
DecVal = DecVal + LSB
' Double the value as 8266 PWM uses 0 to 511
DecVal = DecVal * 2
return


And the minimal version :
Code: Select all' *****************************************************
' Andy's 8266 CHEERLIGHTS proggie for yellow test board
' V1.00
' *****************************************************
'
RedPin = 15
GrnPin = 12
BluPin = 13
'
HexConv = 123456789ABCDEF
'
TOP
delay 5000
colcode = readts(key,1417,2)
'
if len(colcode) <> 7 then goto TOP
'
Tmp1 = mid$(colcode,2,2)
gosub TODEC
RedVal = DecVal
Tmp1 = mid$(colcode,4,2)
gosub TODEC
GrnVal = DecVal
Tmp1 = mid$(colcode,6,2)
gosub TODEC
BluVal = DecVal
'
pwo RedPin RedVal
pwo GrnPin GrnVal
pwo BluPin BluVal
'
goto TOP

'
TODEC
LSB = left(Tmp1,1)
MSB = right(Tmp1,1)
LSB = instr(HexConv,LSB)
MSB = instr(HexConv,MSB)
DecVal = MSB * 16
DecVal = DecVal + LSB
DecVal = DecVal * 2
return
User avatar
By AndyGadget
#37825 Updated shorter version using new (01/03/2015) HEXTOINT function.
With comments :
Code: Select all' *****************************************************
' Andy's 8266 CHEERLIGHTS proggie for yellow test board
' v1.0 - First issue
' V1.1 - Smaller version using new "hextoint" function
'
' Colours are off for RGB LED as supplied but could be
' made more accurate by resistor colour value matching.
' Should be more acccurate for NEOPIXEL LEDs which can
' use the colour codes directly with no conversion.
' *****************************************************
'
' Pin definitions for RGB LED outputs
RedPin = 15
GrnPin = 12
BluPin = 13
'
'Lookup string for hex to decimal conversion subroutine
HexConv = 123456789ABCDEF
'
TOP
delay 5000
' Get current Ccheerlights colour code from ThingSpeak API
colcode = readts(key,1417,2)
'
' Simple check to make sure 7 character string returned
if len(colcode) <> 7 then goto TOP
'
' Extract red, green then blue codes from string and
' convert to decimal equivalent. Mult by 2 for PWM value.
'
RedVal = mid$(colcode,2,2)
RedVal = hextoint(RedVal)
RedVal = RedVal * 2
GrnVal = mid$(colcode,4,2)
GrnVal = hextoint(GrnVal)
GrnVal = GrnVal * 2
BluVal = mid$(colcode,6,2)
BluVal = hextoint(BluVal)
BluVal = BluVal * 2
'
' Set PWM for each colour
pwo RedPin RedVal
pwo GrnPin GrnVal
pwo BluPin BluVal
'
goto TOP


And minimal version:
Code: Select all' *****************************************************
' Andy's 8266 CHEERLIGHTS proggie for yellow test board
' V1.0 - Initial release
' v1.1 - Smaller version using new "hextoint" function
' *****************************************************
'
RedPin = 15
GrnPin = 12
BluPin = 13
'
TOP
delay 10000
colcode = readts(key,1417,2)
'
if len(colcode) <> 7 then goto TOP
'
RedVal = mid$(colcode,2,2)
RedVal = hextoint(RedVal)
RedVal = RedVal * 2
GrnVal = mid$(colcode,4,2)
GrnVal = hextoint(GrnVal)
GrnVal = GrnVal * 2
BluVal = mid$(colcode,6,2)
BluVal = hextoint(BluVal)
BluVal = BluVal * 2
'
pwo RedPin RedVal
pwo GrnPin GrnVal
pwo BluPin BluVal
'
goto TOP
User avatar
By viscomjim
#37835 Excellent example of how easy Mike has made things for us!!!! Should be neat to see your simple code for using the neopixels. I tried your code and since I don't have anything hooked up to my board, I just am serial printing the red, green and blue values coming from TS and am watching them change. I have never heard of cheer lights, but am reading up on it. Pretty cool stuff, keep it up!!!!

Question for you, why do you multiply the values by 2?
User avatar
By AndyGadget
#37838 Actually. I've just realised it should be x 4 for the PWM.
Reason is the Cheerlights RGB colour values are 0 to 255 ($FF) and the PWM outputs are 0 up to 1023 for max brightness.