sequencing leds on picaxe sheild base s.2 to s.9

tinyb

Member
hi

just in the process of converting the sparkfun inventor's kit from arduino to picaxe. Which has been an interesting and enjoyable challenge but I must now return to an aspect that I cannot find a solution for, cascading LEDs from s.2 to s.9 and back again. The arduino uses an array to move from 2 to 3 etc. This would be easy if the shield outputs were all from the same port and in sequential order but they are not.

a simple shield base solution would be to:
Code:
'LEDs connected to s.2 to s.9 of the shield base

#sim axe401
#picaxe 28x2
#no_table

do
	high s.2
	pause 500
	low s.2
	high s.3
	pause 500
	low s.3
	high s.4
	pause 500
	low s.4
	high s.5
	pause 500
	low s.5
	high s.6
	pause 500
	low s.6
	high s.7
	pause 500
	low s.7	
	high s.8
	pause 500
	low s.8
	high s.9
	pause 500
	low s.9
loop
A simple 28x2 solution would be to:

Code:
#sim none
#picaxe 28x2
#no-table

b0 = 1
dirsB = %11111111

do
	pinsB = b0
	if b0 = 128 then 	'%10000000 for binary or $80 for hex
		b0 = 1			'%00000001 for binary or $01 for hex reset the led to the first one
	else
		b0 = b0 * 2		'set the next led on - see binary maths in the manual 1 for help
	end if
	pause 500

loop
I want to do the second one of the shield base without complicating the wiring up (or changing it from the first example from above)

the next step would be to 'knight rider' it but don't want to individually code each LED.

Thought of a lookup table or using the scratch pad to store the binary sequence of the s.[pins] output but would need to change between the B and C ports.

the sequence would look like:
s.2 - pinsB = %00000100 or $04 or 4
s.3 - pinsB = %00000001 or $01 or 1
s.4 - pinsB = %00000010 or $02 or 2
s.5 - pinsB = %00100000 or $20 or 32
s.6 - pinsB = %01000000 or $40 or 64
s.7 - pinsB = %10000000 or $80 or 128
s.8 - pinsC = %00000001 or $01 or 1
s.9 - pinsC = %00000010 or $02 or 2

how do i change from pinsB to pinsC without making it too complecated - remeber this is activity 2 on an introductory set of activities. I don't mind introducing lookup tables or memory pointers as this is an extention activity but if i need to have too many complicated decision statements it will turn th euser off, especially when moving to the knight rider style of example.

Can anyone help?

thanks in advance - if there is no suitable solution I will leave it out - contemplated it already but thought I would give it to the great minds of the forum.

bye
tiny
 

hippy

Technical Support
Staff member
One option is to use 'b0' to hold the 'virtual outputs' and have a routine to translate those to the real outputs. This trick is usually called "Hardware Abstraction" ...

Code:
#Picaxe 28X2
#Sim Shield

dirS.0 = 1
dirS.1 = 1
dirS.2 = 1
dirS.3 = 1
dirS.4 = 1
dirS.5 = 1
dirS.6 = 1
dirS.7 = 1

Do
  b0 = %00000001
  Do
    Gosub OutputLeds
    b0 = b0 * 2
  Loop Until b0 = 0
  b0 = %10000000
  Do
    b0 = b0 / 2
    Gosub OutputLeds
  Loop Until b0 = 0
Loop

OutputLeds:
  pinS.0 = bit0
  pinS.1 = bit1
  pinS.2 = bit2
  pinS.3 = bit3
  pinS.4 = bit4
  pinS.5 = bit5
  pinS.6 = bit6
  pinS.7 = bit7
  Return
You can run that through the simulator to see it working.

Then rename dirS.0-dirS.7 to dirS.2-dirsS.9 and likewise for pinS.0-pinS.7 to pinS.2-pinS.9
 
Last edited:

tinyb

Member
Thanks hippy

as always an excellent solution that works as described - easy to manipulate the outputs using b0 as required

but

where have dirS.[pin] and pinS.[pin] come from - just looked at the latest manual and find no reference? None in the shield documentation either?

I think that i can explain this as an extension activity for number 2.

changed the code to work from s.2 to s.9 as described in the question - works a treat.

Code:
#picaxe 28x2
#sim shield


dirS.2 = 1
dirS.3 = 1
dirS.4 = 1
dirS.5 = 1
dirS.6 = 1
dirS.7 = 1
dirS.8 = 1
dirS.9 = 1

Do
  b0 = %00000001
  Do
    Gosub OutputLeds
    b0 = b0 * 2
  Loop Until b0 = 0
  b0 = %10000000
  Do
    b0 = b0 / 2
    Gosub OutputLeds
  Loop Until b0 = 0
Loop

OutputLeds:

  pinS.2 = bit0
  pinS.3 = bit1
  pinS.4 = bit2
  pinS.5 = bit3
  pinS.6 = bit4
  pinS.7 = bit5
  pinS.8 = bit6
  pinS.9 = bit7  
  Return
thanks
tiny
 
Last edited:

tinyb

Member
thanks

found that reference after i posted. didn't think that i could use it for outputing but i suppose that i set the pins as outputs using the dirS.[pin] and hence the pinS.[pin] becomes an output command. dirS.[pin] is not explained and the example is for inputs. this is why we have the forum and people like hippy etc, thanks again.

played around further with the code.
the arduino example had 4 variations:
  1. one after another no loop - shown earlier - turned off in sequence, will have to change for my documentation
  2. one after another in a loop - shown below
  3. one at a time - hippy based example above
  4. in and out - its now after midnight, so will work on that one tomorrow (i mean later today after a sleep)

one after another in a loop
Code:
#picaxe 28x2
#sim shield


dirS.2 = 1
dirS.3 = 1
dirS.4 = 1
dirS.5 = 1
dirS.6 = 1
dirS.7 = 1
dirS.8 = 1
dirS.9 = 1

Do
  w1 = %00000001
  b0 = %00000001
  Do
    w1 = w1 * 2 
    b0 = w1 - 1  	
    Gosub OutputLeds
	
  Loop Until b0 = 255
  b0 = %11111111
  w1 = %100000000
  Do
    
    
    Gosub OutputLeds
	w1 = w1 / 2
	b0 = w1 - 1
  Loop Until b0 = 0
  
Loop

OutputLeds:

  pinS.2 = bit0
  pinS.3 = bit1
  pinS.4 = bit2
  pinS.5 = bit3
  pinS.6 = bit4
  pinS.7 = bit5
  pinS.8 = bit6
  pinS.9 = bit7  
  Return
thanks
tiny
 
Last edited:

tinyb

Member
yay - sleep now had - students now taught - and finally got to work on example 4 the in out bit

Code:
#picaxe 28x2
#sim shield


dirS.2 = 1
dirS.3 = 1
dirS.4 = 1
dirS.5 = 1
dirS.6 = 1
dirS.7 = 1
dirS.8 = 1
dirS.9 = 1

Do

  
 for b1 = 0 to 3
 	lookup b1,(%00011000, %00100100, %01000010, %10000001), b0
      	
    Gosub OutputLeds
	
  next b1
 
 for b1 = 3 to 0 step-1
 	lookup b1,(%00011000, %00100100, %01000010, %10000001), b0
      	
    Gosub OutputLeds
	
  next b1
Loop

OutputLeds:

  pinS.2 = bit0
  pinS.3 = bit1
  pinS.4 = bit2
  pinS.5 = bit3
  pinS.6 = bit4
  pinS.7 = bit5
  pinS.8 = bit6
  pinS.9 = bit7  
  Return
well all the problems of the world are now solved!

bye
tiny
 

hippy

Technical Support
Staff member
well all the problems of the world are now solved!
Excellent work. If you wanted a 'KITT' / 'Cylon' display using more than the usual 8 LED's you can put the 'virtual LED' control in 'w0' and then output to more pins using the same technique. Which can also be used with PICAXE not on a Shield Base.

It's a very handy trick, especially when you build the hardware and find the physical LED's are wired in the wrong order; rather than rewire you just have to change the 'OutputLeds' routine.
 

westaust55

Moderator
Another possible AXE401 solution.

Code:
#picaxe 28x2
#sim shield

dirS.2 = 1
dirS.3 = 1
dirS.4 = 1
dirS.5 = 1
dirS.6 = 1
dirS.7 = 1
dirS.8 = 1
dirS.9 = 1

Do
  FOR b0 = 0 to 6
    Gosub OutputLeds
  NEXT b0
  FOR b0 = 7 TO 1 STEP -1
    Gosub OutputLeds
  NEXT b0
Loop

OutputLeds:
	LOOKUP b0, (S.2,S.3,S.4,S.5,S.6,S.7,S.8,S.9), b1	
             ; LOOKUP b0, (2,0,1,5,6,7,8,9), b1 ; alternate line to the above
	LOW S.2, S.3, S.4, S.5, S.6, S.7, S.8, S.9
	HIGH b1
  Return
 
Last edited:

tinyb

Member
Westy

trust you to throw a spanner in the works after all the problems of the world have been solved.
Initially I didn't like your solution as much til i had a play with it. It maybe too early to explain bytes and bit to the new user so yours using a lookup table for this example is the same as the next example post 6 above.
will look at the pros and cons later.

just one question westy - with the number version - why that sequence of numbers? it does work but i don't quite understand it.

thnaks
tiny
 
Last edited:

westaust55

Moderator
Hello tinyb,

The numbers for the alternate LOOKUP program line are just the actual constant values assigned to the PICAXE pins. Each PICAXE pin identifier (eg C.4 or for the shield say S.5) are in fact aliases for a constant.
It is like having an unwritten/hidden program lines such as:
SYMBOL S.3 = 0
SYMBOL S.5 = 5
SYMBOL S.11 = 13

but as can also be seen, when just a number is given, it is harder to understand what they mean.

I am not trying to promote that the code I posted is better than that given by hippy. hippy is the “master” of achieving the best code for PICAXE programming.
The LOOKUP command can in fact be quite slow when there are many values involved, but in this case it does save a few bytes of program space.
 

tinyb

Member
Thanks westy

That does make sense. it is going back to the original picaxe days of high 3 for what is now high b.3 and portC being the next numbers on from that - C.5 being 12?

in the material i have referenced this forum post so people can see the various solutions - I am going for the 'masters' as it introduces the bits, bytes and words. I am leaving it a binary as it clearly shows the pin allocations (am tossing up about having the hex version for example 4 as a comment to introduce this aswell)

thanks
tiny
 
Top