View Full Version : Problem with bit shifting LEDs

01-18-2008, 05:33 AM
so I have 16 LEDs lined up on pins 0 through 15 and two buttons on pin 22 and 23

the program taht I wrote out would bit shift and move the lit LED over one way or another depending on which button I pushed... I ran into a few problems so I improvised and added two extra methods to define and everything worked smoothly except that when I started the program and moved it to the right first, it would start on pin 0 instead of pin 15 like it should have. Ive tried many different things to get this working correctly but the when I added another BIT to one of the defenitions, it fixed the problem, but then when I would go LEFT as soon as it got to pin 15, it wouldnt switch over to pin 0 again.....

here is that code

_xinfreq = 5_000_000иииииииииииииииииииии
и _clkmode = xtal1 + pll16x
иии outa[0..15] := %10000000_00000000_0ии====иI added thisиto fix the problem but then got another problem
иии Index
PUB Def2
иии outa[15..0] := %10000000_00000000
иии Index
PUB Index
иии dira[22] := 0
иии dira[23] := 0
иии ina[22] := 0
иии ina[23] := 0
и repeat
иии if ina[22] == 1
иииии Main
иии elseif ina[23] == 1
иииии Oppo
PUB Main
dira[0..15] := %11111111_11111111
иии repeat
иииии repeat while ina[22] == 1
иииииии waitcnt(clkfreq/10 + cnt)
иииииии outa[0..15] >>= 1
иииииии If outa[0..15] == %00000000_00000000
иииииииии Def
иииииии elseifnot outa[15..0] == %00000000_00000000
иииииииии Index
PUB Oppo
dira[15..0] := %11111111_11111111ииииии
и repeat
иии repeat while ina[23] == 1
иииии waitcnt(clkfreq/10 + cnt)
иииии outa[15..0] >>= 1
иииии If outa[15..0] == %00000000_00000000
иииииии Def2
иииии elseifnot outa[0..15] == %00000000_00000000
иииииииии Index

01-18-2008, 07:34 AM
I am not sure I have spotted all your bugs... but let's start at least...

(1) You use RECURSION when you shouldn't! I don't know where this funny habbit comes from.. In fact I have seen two beginners now doing this.
Look: When you are in the endles loop of MAIN or OPPO you stack INDEX upon it. Think of a different way to RETURN from those routines!

(2) Be always consistent, whether to use OUTA[0..15] or OUTA[15..0]; those are different and you will get (and you have gottenhttp://forums.parallax.com/images/smilies/smile.gif ) absolutely confused when using both in the same program.

(3) Don't check again for the complement in an ELSEIF; this is bad practice, as it will confuse you as well. It is absolutly unnecessary - use just ELSE.

(4) Don't set INA!
(5) You don't need to set DIRAs to zero; but this does no harm.

After considering all this, your program will most likely run http://forums.parallax.com/images/smilies/smile.gif
Much luck!