Interesting :)

Hello there, I bought a bargraph LED yesterday from my beloved store Maplin :D I have been experimenting with them making really cool patterns and I thought I would then create a binary count up. My first attempt was just to do the first 5 numbers (1,2,4,8,16) This went fine doing a continueous command of highs and lows. I then decided to carry it on by adding many sub routines to make it the full 10 lines of the LEDs. Again this went superb. I tried to change all of the code so it requires a lot less code and came up with the idea of having 10 sub routines for each led. Here is the code:


Code:
main:
low 0,1,2,3,4,5,6,7,8,9
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
gosub first6
gosub first7
gosub first8
gosub first9
gosub first10
stop


first1:
high 0
pause 1000
return


first2:
low 0 high 1
pause 1000
gosub first1
return

first3:
low 0,1 high 2
pause 1000
gosub first1
gosub first2
return

first4:
low 0,1,2 high 3 
pause 1000
gosub first1
gosub first2
gosub first3
pause 1000
return
 



first5:
low 0,1,2,3 high 4
pause 1000
gosub first1
gosub first2
gosub first3
gosub first4
pause 1000
return


first6:
low 0,1,2,3,4 high 5
pause 1000
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
pause 1000
return


first7:
low 0,1,2,3,4,5 high 6
pause 1000
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
gosub first6
pause 1000
return


first8:
low 0,1,2,3,4,5,6 high 7
pause 1000 
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
gosub first6
gosub first7
pause 1000
return

first9:
low 0,1,2,3,4,5,6,7 high 8
pause 1000
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
gosub first6
gosub first7
gosub first8
pause 1000
return

first10:
low 0,1,2,3,4,5,6,7,8 high 9
pause 1000
gosub first1
gosub first2
gosub first3
gosub first4
gosub first5
gosub first6
gosub first7
gosub first8
gosub first9
pause 1000
return
This is all working and it looks great but when you get to like the 6th digit (32) it starts to take a long time to carry on. I am guessing this is because of all the sub routines then ending or some think. I am using a 20m2 chip.

Am I right in getting this working better by putting the setfreq in there? I have never used it and when I tried to put this at the start it lit one led and froze :/

Init:
setfreq 8

main:............. This didn't work etc...

Picture of the project so far:
IMG00137-20111120-0959.jpg


little question is there a way of fixing this or could someone just post what to do it would be much appreciated Trevor Boultwood :)
 

nick12ab

Senior Member
Try setfreq m32 instead.

Also, under variables in PICAXE Manual 2, look at the >> and << operators under variables, which allow you to simply shift bits left and right in a variable.

Also, Ron Hackett did a similar thing in his book 'PICAXE Projects for the Evil Genius'.
 

Svejk

Senior Member
Try this code (uses pinsB):

Code:
#picaxe 20m2
dirsb = $FF
b1 = 1
pinsb = b1
pause 1000

for b0=0 to 7
  b1 = b1 * 2
  pinsb = b1
  pause 1000
next
do:loop
 
Thanks to both of you! Both methods worked, well until I realised that my code was wrong. I started adding another pause at line 4 therefore it would pause an extra second ever number higher. deleting all of them made it work without the need of changing the frequency. Thanks again Trevor Boultwood
:) See you soon!
 

mleeee

New Member
Trevor - at a quick glance, you're generating rather a lot of subroutine calls using this structure, and I bet you're exceeding the number of times you can call another subroutine from within the previous one. (the maths term is a FACTORIAL progression)

Imagine a binary number in a variable, say binary for 5 (101). LET B3=5 in Picaxe basic.
What happens if you multiply B3 by 2? Ans, you get 10 in decimal in B3 - or 1010 in binary, in B3.
Notice the original pattern 101 has been 'shifted' one space to the Left to give 1010.
Dividing by 2 shifts patterns one 'bit' to the right.
Bits on the end of the pattern will 'fall off' and you loose them.

I'd try writing a subroutine which displays B3 on your new bargraph, which might go:
display:
outpinsB=B3
pause 200
return

I'm assuming your bargraph is connected to the pins of PORT B.

Now, any pattern you put in B3 can be DISPLAYED on your bar-graph real easy. So you could do:

Main:
let B3 = 1
for b4=1 to 8
gosub DISPLAY
let b3 = b3 * 2 'shift pattern one bit to the left
let B3 = b3 + 1 'make bottom bit, bit0, 1
next b4
end

... and that should fill up your bargraph from the right hand end.

Have fun

Matt Lee
 
Top