Help with simplifying code

Krafter

Member
Hey guys. I've been beating my head against the wall trying to figure this out. I've been working with APA102 LEDs and in my venture, I've seen some pretty cool things that can be done with them. I have them working just fine with a PICAXE 20x2 but one of the things I'm struggling with is getting them to fade from one color to the next while giving the appearance of pushing the first color down the line. I have it working with the following code but it takes a lot of code to make it happen for only 10 LEDs. The project I have in mind is going to have hundreds of these APA102 LEDs so I need to find a better way of doing what I have below. Any tips and advice would be great.

Code:
#picaxe 20x2

'Set how many LED modules
Symbol HOW_MANY_LEDS = 10 
symbol RED   = w10
symbol GREEN = w11
symbol BLUE  = w12

'Initialise the HSPI interface
#macro init()
	hspisetup spimode00, spifast
#endmacro

'Send a four byte packet out via HSPI
#macro sendPacket( n1, n2, n3, n4 )
	hspiout( n1, n2, n3, n4 )
#endmacro

'Send the start of data header
#macro head()
	sendPacket( $00, $00, $00, $00 )
#endmacro

'Send a LED controlling command
#macro send( red, green, blue)
	sendPacket( $FF, blue, green, red )
#endmacro

PowerOnReset:

'Initialise the HSPI interface
init
'Turn all LED modules off
head
for w0 = 1 To HOW_MANY_LEDS
	send( $00, $00, $00 ) '1 to last = Off
next
head

main:

for b0 = 1 to HOW_MANY_LEDS
	select case b0
	case  1
		head
		send( 255, 000, 003 )
		head
	case  2
		head
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head
	case  3
		head
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  4
		head
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  5
		head
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  6
		head
		send( 115, 000, 143 )
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  7
		head
		send( 087, 000, 171 )
		send( 115, 000, 143 )
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  8
		head
		send( 059, 000, 199 )
		send( 087, 000, 171 )
		send( 115, 000, 143 )
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case  9
		head
		send( 031, 000, 227 )
		send( 059, 000, 199 )
		send( 087, 000, 171 )
		send( 115, 000, 143 )
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head	
	case 10
		head
		send( 003, 000, 255 )
		send( 031, 000, 227 )
		send( 059, 000, 199 )
		send( 087, 000, 171 )
		send( 115, 000, 143 )
		send( 143, 000, 115 )
		send( 171, 000, 087 )
		send( 199, 000, 059 )
		send( 227, 000, 031 )
		send( 255, 000, 003 )
		head
	end select
pause 100
next
 

lbenson

Senior Member
I notice that the values you send for red are 255,227,199,171,143,115,087,059,031,003, and the reverse for blue, so that when red is 255, blue is 003, and when red is 003, blue is 255 (green is always 0 in these examples).

I'm uncertain exactly how the leds are addressed, so may have missed something there, but the following code I think produces the red/green/blue values you are looking for (led addressing perhaps excepted). The above values are stored in eeprom. It uses 91 bytes, and, by omitting macros, runs in PE5 as well as PE6.

For the nth led, the code starts with the nth value in the eeprom array and works backwards to the beginning for red, and the [number of leds - n + 1] position for blue, and works forward to the end of the array.

Code:
#picaxe 20x2

'Set how many LED modules
Symbol HOW_MANY_LEDS = 10 
'symbol RED   = w10
'symbol GREEN = w11
'symbol BLUE  = w12
symbol red   = b4
symbol green = b5
symbol blue  = b6
symbol loopCtr = b7
symbol ndxRed = b8
symbol ndxBlue = b9

eeprom 1,(255,227,199,171,143,115,087,059,031,003)

#rem
'Initialise the HSPI interface
#macro init()
	hspisetup spimode00, spifast
#endmacro

'Send a four byte packet out via HSPI
#macro sendPacket( n1, n2, n3, n4 )
	hspiout( n1, n2, n3, n4 )
#endmacro

'Send the start of data ' header
'#macro ' head()
'	sendPacket( $00, $00, $00, $00 )
'#endmacro
 
'Send a LED controlling command
#macro send( red, green, blue)
	sendPacket( $FF, blue, green, red )
#endmacro
#endrem

PowerOnReset:

'Initialise the HSPI interface
'init
	hspisetup spimode00, spifast
'Turn all LED modules off
hspiout( 0,0,0,0 )
for w0 = 1 To HOW_MANY_LEDS
'	send( $00, $00, $00 ) '1 to last = Off
	hspiout( $FF,0,0,0 )
next
hspiout( 0,0,0,0 )
green = 0

main:

for b0 = 1 to HOW_MANY_LEDS
	hspiout( 0,0,0,0 )
	ndxRed = b0
	ndxBlue = HOW_MANY_LEDS - b0 + 1
	for loopCtr = 1 to b0
'eeprom 1,(255,227,199,171,143,115,087.059,031,003)
	  read ndxRed, red
	  read ndxBlue, blue
	  ndxRed = ndxRed - 1
	  ndxBlue = ndxBlue + 1
  	  hspiout( $FF,red,green,blue )
          sertxd(#red," ",#green," ",#blue,cr,lf)
	next loopCtr
	sertxd(cr,lf)
	hspiout( 0,0,0,0 )
pause 100
next
Sertxd shows the following values for R/G/B:
Code:
255 0 3

227 0 31
255 0 3

199 0 59
227 0 31
255 0 3

171 0 87
199 0 59
227 0 31
255 0 3

143 0 115
171 0 87
199 0 59
227 0 31
255 0 3

115 0 143
143 0 115
171 0 87
199 0 59
227 0 31
255 0 3

87 0 171
115 0 143
143 0 115
171 0 87
199 0 59
227 0 31
255 0 3

59 0 199
87 0 171
115 0 143
143 0 115
171 0 87
199 0 59
227 0 31
255 0 3

31 0 227
59 0 199
87 0 171
115 0 143
143 0 115
171 0 87
199 0 59
227 0 31
255 0 3

3 0 255
31 0 227
59 0 199
87 0 171
115 0 143
143 0 115
171 0 87
199 0 59
227 0 31
255 0 3
Not sure exactly what is implied in scaling up to hundreds of leds.

Elimination of the array could be done by taking advantage of the fact that the difference between each of the values in the array is 28, so each red/blue value could be calculated.
 
Last edited:

BESQUEUT

Senior Member
With some comments for simulation purpose :
Code:
[color=Green]' APA102[/color]

[color=Navy]#picaxe [/color][color=Black]20x2[/color]

[color=Green]'Set how many LED modules[/color]
[color=Blue]Symbol HOW_MANY_LEDS [/color][color=DarkCyan]= [/color][color=Navy]10[/color]

[color=Blue]symbol [/color][color=Purple]RED   [/color][color=DarkCyan]= [/color][color=Purple]w10[/color]
[color=Blue]symbol [/color][color=Purple]GREEN [/color][color=DarkCyan]= [/color][color=Purple]w11[/color]
[color=Blue]symbol [/color][color=Purple]BLUE  [/color][color=DarkCyan]= [/color][color=Purple]w12[/color]

[color=Blue]symbol [/color][color=Purple]Delta[/color][color=DarkCyan]=[/color][color=Purple]w13[/color]

[color=Green]'Initialise the HSPI interface[/color]
[color=Navy]#macro [/color][color=Black]init[/color][color=Blue]()
      hspisetup spimode00[/color][color=Black], [/color][color=Blue]spifast[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send a four byte packet out via HSPI[/color]
[color=Navy]#macro [/color][color=Black]sendPacket[/color][color=Blue]([/color][color=Black]n1, n2, n3, n4 [/color][color=Blue])[/color]
[color=Green]'     hspiout( n1, n2, n3, n4 )
      [/color][color=Blue]sertxd([/color][color=Black]#n1,[/color][color=Red]" "[/color][color=Black],#n2,[/color][color=Red]" "[/color][color=Black],#n3,[/color][color=Red]" "[/color][color=Black],#n4,[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send the start of data header[/color]
[color=Navy]#macro [/color][color=Black]head[/color][color=Blue]()[/color]
[color=Green]'     sendPacket( $00, $00, $00, $00 )
      [/color][color=Blue]sertxd([/color][color=Red]"Head"[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send a LED controlling command[/color]
[color=Navy]#macro [/color][color=Black]Send[/color][color=Blue]([/color][color=Purple]red[/color][color=Black], [/color][color=Purple]green[/color][color=Black], [/color][color=Purple]blue[/color][color=Blue])[/color]
[color=Green]'     sendPacket( $FF, blue, green, red )
      [/color][color=Blue]sertxd([/color][color=Red]"$FF "[/color][color=Black],#[/color][color=Purple]blue[/color][color=Black],[/color][color=Red]" "[/color][color=Black],#[/color][color=Purple]green[/color][color=Black],[/color][color=Red]" "[/color][color=Black],#[/color][color=Purple]red[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Black]PowerOnReset:[/color]

[color=Green]'Initialise the HSPI interface[/color]
[color=Black]init[/color]
[color=Green]'Turn all LED modules off[/color]
[color=Black]head[/color]
[color=Blue]for [/color][color=Purple]w0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]To HOW_MANY_LEDS
      [/color][color=Black]send[/color][color=Blue]( [/color][color=Navy]$00[/color][color=Black], [/color][color=Navy]$00[/color][color=Black], [/color][color=Navy]$00 [/color][color=Blue]) [/color][color=Green]'1 to last = Off[/color]
[color=Blue]next[/color]
[color=Black]head

main:[/color]
[color=Purple]Delta[/color][color=DarkCyan]=[/color][color=Navy]256[/color][color=DarkCyan]/[/color][color=Blue]HOW_MANY_LEDS[/color]
[color=Purple]GREEN[/color][color=DarkCyan]=[/color][color=Navy]0[/color]


[color=Blue]for [/color][color=Purple]b0 [/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]to HOW_MANY_LEDS
  [/color][color=Purple]b2[/color][color=DarkCyan]=[/color][color=Blue]HOW_MANY_LEDS[/color][color=DarkCyan]-[/color][color=Purple]b0[/color][color=DarkCyan]+[/color][color=Navy]1
  [/color][color=Black]head
  [/color][color=Blue]for [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b2 [/color][color=Blue]to HOW_MANY_LEDS
      [/color][color=Purple]BLUE[/color][color=DarkCyan]=[/color][color=Purple]b1[/color][color=DarkCyan]*[/color][color=Purple]delta
      RED[/color][color=DarkCyan]=[/color][color=Navy]256[/color][color=DarkCyan]-[/color][color=Purple]BLUE
      [/color][color=Black]Send[/color][color=Blue]([/color][color=Purple]RED[/color][color=Black],[/color][color=Purple]GREEN[/color][color=Black],[/color][color=Purple]BLUE[/color][color=Blue])
  next [/color][color=Purple]b1
  [/color][color=Black]head[/color]
[color=Green]'  pause 100
  [/color][color=Blue]sertxd ([/color][color=Red]"PAUSE 100"[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])
next [/color][color=Purple]b0
 [/color]
That is the idea. But, when HOW_MANY_LEDS will be bigger, there will be a problem with integer math :
256/HOW_MANY_LEDS will be very imprecise
So we will have a little more math to do...
 

BESQUEUT

Senior Member
If #3 is OK for you, this one will take account of the math problem :
Code:
[color=Green]' APA102 V02[/color]
#simspeed 0
[color=Navy]#picaxe [/color][color=Black]20x2[/color]

[color=Green]'Set how many LED modules[/color]
[color=Blue]Symbol HOW_MANY_LEDS [/color][color=DarkCyan]= [/color][color=Navy]10[/color]

[color=Blue]symbol [/color][color=Purple]RED   [/color][color=DarkCyan]= [/color][color=Purple]w10[/color]
[color=Blue]symbol [/color][color=Purple]GREEN [/color][color=DarkCyan]= [/color][color=Purple]w11[/color]
[color=Blue]symbol [/color][color=Purple]BLUE  [/color][color=DarkCyan]= [/color][color=Purple]w12[/color]

[color=Blue]symbol [/color][color=Purple]Delta[/color][color=DarkCyan]=[/color][color=Purple]w13[/color]

[color=Green]'Initialise the HSPI interface[/color]
[color=Navy]#macro [/color][color=Black]init[/color][color=Blue]()
      hspisetup spimode00[/color][color=Black], [/color][color=Blue]spifast[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send a four byte packet out via HSPI[/color]
[color=Navy]#macro [/color][color=Black]sendPacket[/color][color=Blue]([/color][color=Black]n1, n2, n3, n4 [/color][color=Blue])[/color]
[color=Green]'     hspiout( n1, n2, n3, n4 )
      [/color][color=Blue]sertxd([/color][color=Black]#n1,[/color][color=Red]" "[/color][color=Black],#n2,[/color][color=Red]" "[/color][color=Black],#n3,[/color][color=Red]" "[/color][color=Black],#n4,[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send the start of data header[/color]
[color=Navy]#macro [/color][color=Black]head[/color][color=Blue]()[/color]
[color=Green]'     sendPacket( $00, $00, $00, $00 )
      [/color][color=Blue]sertxd([/color][color=Red]"Head"[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Green]'Send a LED controlling command[/color]
[color=Navy]#macro [/color][color=Black]Send[/color][color=Blue]([/color][color=Purple]red[/color][color=Black], [/color][color=Purple]green[/color][color=Black], [/color][color=Purple]blue[/color][color=Blue])[/color]
[color=Green]'     sendPacket( $FF, blue, green, red )
      [/color][color=Blue]sertxd([/color][color=Red]"$FF "[/color][color=Black],#[/color][color=Purple]blue[/color][color=Black],[/color][color=Red]" "[/color][color=Black],#[/color][color=Purple]green[/color][color=Black],[/color][color=Red]" "[/color][color=Black],#[/color][color=Purple]red[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Black]PowerOnReset:[/color]

[color=Green]'Initialise the HSPI interface[/color]
[color=Black]init[/color]
[color=Green]'Turn all LED modules off[/color]
[color=Black]head[/color]
[color=Blue]for [/color][color=Purple]w0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]To HOW_MANY_LEDS
      [/color][color=Black]send[/color][color=Blue]( [/color][color=Navy]$00[/color][color=Black], [/color][color=Navy]$00[/color][color=Black], [/color][color=Navy]$00 [/color][color=Blue]) [/color][color=Green]'1 to last = Off[/color]
[color=Blue]next[/color]
[color=Black]head

main:[/color]
[color=Purple]Delta[/color][color=DarkCyan]=[/color][color=Blue]HOW_MANY_LEDS[/color][color=DarkCyan]-[/color][color=Navy]1[/color]
[color=Purple]Delta[/color][color=DarkCyan]=[/color][color=Navy]65535[/color][color=DarkCyan]/[/color][color=Purple]Delta
GREEN[/color][color=DarkCyan]=[/color][color=Navy]0[/color]


[color=Blue]for [/color][color=Purple]b0 [/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]to HOW_MANY_LEDS
  [/color][color=Purple]b2[/color][color=DarkCyan]=[/color][color=Blue]HOW_MANY_LEDS[/color][color=DarkCyan]-[/color][color=Purple]b0[/color][color=DarkCyan]+[/color][color=Navy]1
  [/color][color=Black]head
  [/color][color=Blue]for [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b2 [/color][color=Blue]to HOW_MANY_LEDS
      [/color][color=Purple]BLUE[/color][color=DarkCyan]=[/color][color=Purple]b1[/color][color=DarkCyan]-[/color][color=Navy]1[/color][color=DarkCyan]*[/color][color=Purple]delta[/color][color=DarkCyan]/[/color][color=Navy]256
      [/color][color=Purple]RED[/color][color=DarkCyan]=[/color][color=Navy]255[/color][color=DarkCyan]-[/color][color=Purple]BLUE
      [/color][color=Black]Send[/color][color=Blue]([/color][color=Purple]RED[/color][color=Black],[/color][color=Purple]GREEN[/color][color=Black],[/color][color=Purple]BLUE[/color][color=Blue])
  next [/color][color=Purple]b1
  [/color][color=Black]head[/color]
[color=Green]'  pause 100
  [/color][color=Blue]sertxd ([/color][color=Red]"PAUSE 100"[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])
next [/color][color=Purple]b0
 [/color]
 

Krafter

Member
Thank you guys. These are some great examples that I think I can work with. The execution time is a bit slower than the method I used but I achieved similar speeds by increasing the clock up to 64.

This is similar to what I have in mind to make. I'm just now starting on the different patterns that I want to display and a "rainbow" effect from one side to the other is one of them and that is the code that I'm working on now and that you've helped me with.
 

jims

Senior Member
Krafter...I'm curious. Are you planning to use the WS2812 like this video, or the APA102? The reason that i'm asking is that I've been working with a 1 meter long
Dotstar strip with 60 APA102 LED modules. It requires a "tail" to be sent after the last of 60 data sent to the string. I use a macro like this for the "tail".....
Code:
;** Send the end of data tail.
#macro tail()
  sendPacket( $FF, $FF, $FF, $FF )
#endmacro
None of your example code has the "tail". Does the code actually work without the "tail"? Thank you, JimS
 

Krafter

Member
You are correct that I'm not using an end frame or "tail". This is by design. I found that using an end frame, at times it will force the LED that follows the end frame to go full white. I then used the suggestion as in the following video series. I'm sure you've noticed that sending a start frame, a global brightness, a blue byte, green byte and red byte only, only works on the first LED. After that, each LED requires another four bytes to turn on (start frame, a global brightness, a blue byte, green byte, red byte and something else bytes). It doesn't matter if it's and end frame or a start frame.

 
Last edited:

jims

Senior Member
Krafter... You're correct. I removed the "tail" from some test code & it works the same as with the "tail". JimS
 

jims

Senior Member
Krafter...Here's some test code that I find useful. I'm using spi because the 20x2 Hspi was used for something else. The IR clicker lets you select a color and the brightness. you might want to give it a try. JimS
Code:
'**************************************************************
'* 20x2 controls one 60 LED Dotstar APA102 LED strip with Spiout.
'* Uses Macros.  Turns all LEDs ON then OFF. LED color and 
'* brightness set with IR clicker as prompted on OLED display.
'* May want to set frequency to vary speed of LED Strip.
'* SEE (clicker key functions) at the end of the code.
'* NOTE: Program is now setup to use scratchpad memory. This 
'* can be changed to use internal EEPROM by replacing 
'* the commands ( PUT to WRITE and GET to READ ).
'***************************************************************

;          20X2                +5V -.  APA102 Light strip
;     .-----__-----.                |
;     |            |                `--> RED +V
'	|        B.3 |11---> spi_sck ----> YEL CK
;     |        B.4 |14---> spi_sdo ----> GRN DI
;     |            |                .--> BLK 0V
;     |            |           0V _|_   (Wire colors are for my LED strip)
'     |            |
'     |        C.2 |8<----- CLICKER
'	|        B.1 |17----> AXE133Y Serial OLED
'	|            |
'     .------------.

Symbol CLICKER =  C.2	'Pin 8; Clicker will select routine or interrupt.
Symbol OLED = B.1		'Pin 17.
symbol irdata = b0
symbol Spi_Sck = B.3	'pin 15.
symbol Spi_Sdo = B.4	'pin 14.
symbol addr = b1
symbol red = b2
symbol green = b3
symbol blue = b4
symbol counter = b5
symbol color = b9
symbol brightness =b10 
symbol speed = M16	'Controls speed with "setfreq" command in code,(max64).
Symbol HOW_MANY_LEDS = 60  '* Number of LED modules in the strip.


;** Initialise the SPI interface
#macro Init()
  high spi_sck
#endmacro

;** Send the start of data header
#macro head()
  sendPacket( $00, $00, $00, $00 )
#endmacro

;** Send the end of data tail
#macro tail()
  sendPacket( $FF, $FF, $FF, $FF )
#endmacro

;** Send a four byte packet out via SPI
#macro sendPacket( n1, n2, n3, n4 )
  spiout Spi_sck,Spi_sdo,MSBFirst_H,(n1,n2,n3,n4)
#endmacro

;** Send a LED controlling command
#macro send(  red, green, blue )
  sendPacket( $FF, green,blue, red )
#endmacro

;** Turns all LED modules off.
  '"delay" sets speed that LEDs turn OFF.
#macro AllOff( delay )
	head	
      for b0 = 1 To HOW_MANY_LEDS
     	 SpiOut Spi_Sck,Spi_Sdo,MSBFirst_H,( $FF,$00,$00,$00 )'; Turn all LED modules off
	 pause delay 
	next
      tail	; Send the end of data tail.
#endmacro

#macro clearoled
	serout B.1,n2400,(254,1):pause 30
#endmacro

#picaxe 20x2
	pause 1000
	Init		'Macro initializes the HSPI interface.
	AllOff(0)	'Macro turns all LEDS OFF.

Main:
	clearoled
	call SetColor
	call SetBrightness
	setint %00000000, %00000100, C	'Clicker will cause interupt.
	clearoled	'Clear OLED display
	serout oled,n2400,(254,128,"Any Clicker Key ")
	serout oled,n2400,(254,192,"Resets Program  ")
	setfreq speed	'Set 20X2 frequency to LED strip (max m64 for 20x2).
	do
	 let ptr=0
	 head  ;* Send the start of data header.
	 for counter= 0 to 59 
	  b0=@ptrinc	'Pick off the bits.
	  red  = bit0 * brightness
	  green = bit1 * brightness
	  blue = bit2   * brightness
	  send(red,green,blue) 'Send 60 data stored in Scratchpad.
	  'sertxd (red,green,blue,cr,lf) 'Use for testing.
	  'pause 20
	 next counter
	'tail	;* Send the end of data tail.
	pause 3000	'Pause "X" seconds before turn LEDs OFF.
	AllOff(10)	'Turns all LEDs OFF with 10 Msec delay between each LED OFF.
	loop

'* Set LED color from 0 to 9
'* when prompted on OLED display.
'* See NOTE at end of code.
Setcolor:
	serout oled,n2400,(254,192,"Set Color _") 'Positions the underscore for data entry.
	serout oled,n2400,(254,201)	 'Places cursor.
	call GetIR
	let color = IRData
	serout oled,n2400,(#color):pause 10
'*Load 60 x 1's (Red) into scratchpad memory.	
LoadColors: 
	for addr = 0 to 59
	 put addr,color	'Load color from IR clicker.
	next addr
	return
	
'* Set LED brightness level from 0 to 9
'* when prompted on OLED display.
'* See NOTE at end of code.
SetBrightness:
	serout oled,n2400,(254,192,"Set Brightness _") 'Positions the underscore for data entry.
	serout oled,n2400,(254,207)	 'Places cursor.
	call GetIR
	let Brightness = IRData	'Load "brightness" from IR clicker.
	serout oled,n2400,(#brightness):pause 10
	return
	
GetIR:	
	irin clicker,irdata	'Read data from IR clicker.
	if irdata >9 then reset:endif  'Non Numeric key resets & starts over.
	do:let b5=0:count C.2,80,b5:loop until b5=0 'Loop until key is released.
if irdata<10 then let irdata=irdata+1//10:endif 'Corrects numeric key offset.
	return
'* When code is running, any clicker key will
'* interrupt and force a "reset".
Interrupt:	
	pause 100
	reset		'Force 20x2 to "RESET".
'*** NOTE:

' CLICKER KEY FUNCTIONS FOR COLOR.
'
' KEY   COLOR
' ---   ----------------		 
' 0	00	LEDs OFF
' 1	001	Red
' 2	010	Green
' 3	011	Yellow
' 4	100	Blue
' 5	101	Purple
' 6	110	Light Blue
' 7	111	White
' 8	000 	LEDs OFF
' 9	001	Red
'
'
' CLICKER KEY FUNCTIONS FOR BRIGHTNESS.
'
' KEY   BRIGHTNESS LEVEL
' ---   ----------------		 
' 0	---------- LEDs OFF
' 1	BRIGHTNESS LEVEL 1  (Low)
' 2	BRIGHTNESS LEVEL 2    |
' 3	BRIGHTNESS LEVEL 3    |
' 4	BRIGHTNESS LEVEL 4    |
' 5	BRIGHTNESS LEVEL 5    |
' 6	BRIGHTNESS LEVEL 6    |
' 7	BRIGHTNESS LEVEL 7    |
' 8	BRIGHTNESS LEVEL 8    |
' 9	BRIGHTNESS LEVEL 9   (High)
'
' NOTE: When code is running, any clicker key
'       resets the chip & starts over.
 

BESQUEUT

Senior Member
Any tips and advice would be great.
#1 is the fastest way to do things. But you can use less code :
Code:
[color=Black]main:[/color]
[color=Purple]b1[/color][color=DarkCyan]=[/color][color=Blue]HOW_MANY_LEDS[/color][color=DarkCyan]-[/color][color=Navy]1[/color]
[color=Blue]for [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Purple]b1
      [/color][color=Black]head
      [/color][color=Blue]on [/color][color=Purple]b0 [/color][color=Blue]gosub [/color][color=Black]A01,A02,A03,A04,A05,A06,A07,A08,A09,A10[/color]
[color=Blue]next
end[/color]

[color=Black]A10:send[/color][color=Blue]( [/color][color=Navy]003[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]255 [/color][color=Blue])[/color]
[color=Black]A09:send[/color][color=Blue]( [/color][color=Navy]031[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]227 [/color][color=Blue])[/color]
[color=Black]A08:send[/color][color=Blue]( [/color][color=Navy]059[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]199 [/color][color=Blue])[/color]
[color=Black]A07:send[/color][color=Blue]( [/color][color=Navy]087[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]171 [/color][color=Blue])[/color]
[color=Black]A06:send[/color][color=Blue]( [/color][color=Navy]115[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]143 [/color][color=Blue])[/color]
[color=Black]A05:send[/color][color=Blue]( [/color][color=Navy]143[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]115 [/color][color=Blue])[/color]
[color=Black]A04:send[/color][color=Blue]( [/color][color=Navy]171[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]087 [/color][color=Blue])[/color]
[color=Black]A03:send[/color][color=Blue]( [/color][color=Navy]199[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]059 [/color][color=Blue])[/color]
[color=Black]A02:send[/color][color=Blue]( [/color][color=Navy]227[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]031 [/color][color=Blue])[/color]
[color=Black]A01:send[/color][color=Blue]( [/color][color=Navy]255[/color][color=Black], [/color][color=Navy]000[/color][color=Black], [/color][color=Navy]003 [/color][color=Blue])
      [/color][color=Black]head
      [/color][color=Blue]pause [/color][color=Navy]100
      [/color][color=Blue]RETURN[/color]
 

hippy

Ex-Staff (retired)
Note that the end frame is used to clock data into the final LED. If an end frame is not sent the final LED may not update until the next set of data is sent. That probably isn't much of a problem with a continuously updated string of LED's but could become a problem if updates are only sent infrequently.

I believe it is only an issue when more than 32 (64?) LED's are being used, and it can be necessary to send multiple end frames when more than 64 LED's are being used.

The end frame only sets a LED to white when less data than LED's is sent. It seems possible to send $FF $00 $00 $00 to set the final LED to off but this is not described in the datasheet so you are on your own if you choose to use that and no guarantees it will work with all APA102 LED's.
 
Top