How to save memory on a multiplexed L.E.D pattern array?

Basrad

Member
Hi,

Sorry I've been away for a while, but I'm back :)

I'm running 20 L.e.d's from a 20M2. the leds are plexed in 4 x 5 array. Leds 1 to 5 on output pins 3 to 7 and the 4 arrays switched on and off using C.0 to C.3

I've made one pattern, but I've nearly used half the memory. I was hoping to put a few diffident patterns in..

Is there a different way to do these type of patterns using less memory?

the massive code below simply makes the following pattern from all L.e.d's on to all off:

11111111111111111111 - step1
11111111100111111111 - step2
11111111000011111111 -step3
11111110000001111111 - & so on looping
11111100000000111111
11111000000000011111
11110000000000001111
11100000000000000111
11000000000000000011
10000000000000000001
00000000000000000000
11111111111111111111

code and diagram (hopefully added here)

Code:
main:
setfreq m32
label_118:
		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		high 3
		high 4
		high 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		high 3
		high 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		high 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		low 3
		high 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		high 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		low 3
		low 4
		high 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		high 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		low 3
		low 4
		low 5
		high 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		high 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		low 3
		low 4
		low 5
		low 6
		high 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		high 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		varG = 0		' repeat - reset var
		do until varG = 20		'loop
		inc varG		'inc loop counter
high c.2
high c.1
high c.3
low c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
low c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
low c.2
high c.1
high c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
high c.2
high c.1
low c.3
high c.0
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		low 3
		low 4
		low 5
		low 6
		low 7
		pause 2	'Wait command
		loop		'End of repeat loop 

		goto label_118
 

Attachments

nick12ab

Senior Member
Use table to store the pattern, then use the readtable command and some let commands to process the data in a loop using a counter variable (which wil be the lookup address for the table command).
 

geoff07

Senior Member
Or you could write a subprogram that returns 'on' or 'off' if you pass it the coordinates (cycle no, position in row) of the led you want to light. Then in the subprogram, you can calculate using algebraic expressions, the pattern you want. E.g., in your example,

(in pseudo-code):
Code:
on_or_off:
    if cycle =0 or cycle = 10 then 'on' : return
    temp = 10 - cycle 
    for led_no = 1 to temp
      led(led_no) = 'on'
      led (20-ld_no) = 'on'
    next
return
Obviously you wauld have to code this in Basic and check I got it right, but the idea should be clear.

This is a useful technique where you want to store a lot of data, there is a lot of repetition and where explicitly recording each item would be very costly. I wouldn't suggest it for your simple example but few programs stay simple!
 

Basrad

Member
Nick, I tried reading up on the table, but couldn't get my code to do anything.. I am very new to Basic, and mainly used the flow chart method. Now I am venturing into basic command. I tried this as a test, but couldn't make it work:
Code:
 table 0, (%00000000)
 table 1, (%11111111)

main:

led1:
 readtable 0,b1
 let pins=b1
 pause 1000
 readtable 1,b1
 let pins=b1
 pause 1000
 goto led1
Geoff - tried your example, but didnt do anything. . That really is confusing to me.. I'd have to look up the command in the manual, as never used a cycle or any algebraic expressions before :(
 
Last edited by a moderator:

nick12ab

Senior Member
Untested...
Code:
#picaxe 20m2
table 0,(%11111,%11111,%11111,%11111,%11111,%11110,%01111,%11111,%11111,%11100)  'Continue the pattern...

symbol readvar = b2
symbol temp = b1

dirsB = %11111000
dirsC = %00001111
pinsC = %00001111
for b0 = 0 to 48 step 4
	pinsC = %1111
	readtable b0,readvar
	pinsB = readvar * 8
	pinsC = %1110
	pause 2
	pinsB = 0
	pinsC = %1111
	temp = b0 + 1
	readtable temp,readvar
	pinsB = readvar * 8
	pinsC = %1101
	pause 2
	pinsB = 0
	pinsC = %1111
	temp = b0 + 2
	readtable temp,readvar
	pinsB = readvar * 8
	pinsC = %1011
	pause 2
	pinsB = 0
	pinsC = %1111
	temp = b0 + 3
	readtable temp,readvar
	pinsB = readvar * 8
	pinsC = %0111
next
 

Basrad

Member
Sorry for the delay, dont get much time to play with programming :(

Nick in your example what does the * 8 mean on the readvar?

Your example takes each step to 12 lines.. Without seeing your example I managed to get it down from 62 to 20ish

Code:
varG = 0		' repeat - reset var
		do until varG = 38		'loop
		inc varG		'inc loop counter
let pinsc = %11111110
let pins = %00000000
pause 5
let pins = %00000000
let pinsc = %11111101
let pins = %00000000
pause 5
let pins = %00000000
let pinsc = %11111011
let pins = %00000000
pause 5
let pins = %00000000
let pinsc = %11110111
let pins = %00000000
pause 5
let pins = %00000000

loop
I tried the code above with completed table, but could make it work. Will try again..

geoff07 - do you know of any good tutorials for learning about using algebraic expressions for patterns?

Thanks guys!
 

nick12ab

Senior Member
Nick in your example what does the * 8 mean on the readvar?
That shifts the bits left by 3 so that they are in bits 3-7 not 0-4.

Your example takes each step to 12 lines
But it uses a loop so that the same amount of code is used for a pattern of any length.

I tried the code above with completed table, but could make it work. Will try again..
Did you try my code with the completed pattern?
 

eclectic

Moderator
Basrad

Try this code in the PE simulator.
Code:
table 0, (%00000000,1,2,4,8,16,32,64,128, %11111111)
dirsB = %11111111 


led1:

for b0 = 0 to 9
 readtable b0,b1

 
 let pinsB=b1

 
 pause 500

 
 next

 
 goto led1
I've left it UNcommented, so you'll have the fun
and learning experience of changing numbers into patterns.

e
 

boriz

Senior Member
One byte contains 8 bits. Since you only need on/off information, one bit is sufficient to store the condition of one LED. One byte can store the condition of 8 LEDs. 3 bytes can store the condition for 24 LEDs. That's your 20 plus 4 spare bits. So each distinct pattern only needs 3 bytes.

First you encode the pattern into the 3 bytes and store it in the Program, then you write a routine that decodes the bytes into bit patterns for your outputs.

Storing the patterns in the program is easy. You can use the TABLE command or the LOOKUP command.
 
Top