Shop OBEX P1 Docs P2 Docs Learn Events
cylon flashing light code in spin doesn't seem to work the way its supposed to — Parallax Forums

cylon flashing light code in spin doesn't seem to work the way its supposed to

PatentBotPatentBot Posts: 6
edited 2011-03-27 15:29 in Propeller 1
I wrote this code to flash LEDs (pins) 9 8 7 6 5 4 back and forth

however the code below only flashes the lights in the follwing sequence:

9 8 7 6 5 6 7 8 9 8 7 6 5 6 7 8 9 etc

it doesent light up LED 4, how come?

The problem seems to be on line 7 below which states:

if outa[9..4] ==%000001

This line should get triggered when LED 4 is light and thus go into a loop which reverses the flashing lights. INstead the code only works the way it supposed to when i change the above line to:

if outa[9..4] ==0

I can't figure out why. Any help would be much appreciated.


PUB ShiftLedsLeftRight ' Method declation

dira[9..4]~~
outa[9..4] :=%100000

repeat

waitcnt (clkfreq/5 + cnt)
outa[9..4] >>=1

if outa[9..4] ==%000001 "line 7

repeat until outa[9..4] ==%100000
if outa[9..4]==0
outa[9..4]:=%000001
outa[9..4] <<=1
waitcnt (clkfreq/5 + cnt)

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-03-26 13:48
    Without seeing the indentation, it's hard to make sense of your program.

    You can either use the post code button "#" (after clicking "go advanced") or insert code tags yourself.

    [C0DE]
    your code goes here
    [\C0DE]

    I used a zero instead of an "O" in code so you could see the tags. You'd want to use an "O" in the word code.

    My guess is you're shifting the bit from pin 4 before there's a waitcnt. Try swapping the last two lines.

    Duane
  • PatentBotPatentBot Posts: 6
    edited 2011-03-27 10:18
    Thanks Duane. Swapping the last two lines did work and your right, pin 4 was being shifted before the waitcnt. Much thanks for your help. For anyone else who's interested heres the code
    PUB ShiftLedsLeftRight                 ' Method declation
    
      dira[9..4]~~                    
        outa[9..4] :=%100000   
    
      repeat
      
        waitcnt (clkfreq/5 + cnt)
        outa[9..4] >>=1
                         
        if outa[9..4] ==%000001
          
          repeat until outa[9..4] ==%100000 
            if outa[9..4]==0
              outa[9..4]:=%000001
    
           waitcnt (clkfreq/5 + cnt) 
           outa[9..4] <<=1
    
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-03-27 15:29
    When possible you want to avoid the use of "magic numbers" in code. Here's a new spin (pun intended) on your code that allows you to pass the pins and delay time -- this will let you make changes without major editing.
    pri cylon_eyes(msb, lsb, delay)
    
    '' Runs Glenn Larson "cyclon eyes" style in own cog
    
      dira[msb..lsb]~~
      outa[msb..lsb]~
    
      outa[msb] := 1
    
      repeat
        repeat until outa[lsb]
          waitcnt(cnt + delay)
          outa[msb..lsb] >>= 1
    
        repeat until outa[msb]
          waitcnt(cnt + delay)
          outa[msb..lsb] <<= 1
    

    I'm guessing -- based on purpose and your code -- that you are launching this method into its own cog. I tested on the Propeller Demo Board with this call:
    cognew(cylon_eyes(23, 16, clkfreq / 5), @stack)
    
Sign In or Register to comment.