Shop OBEX P1 Docs P2 Docs Learn Events
Propeller LED Program: 1st Program — Parallax Forums

Propeller LED Program: 1st Program

stuartXstuartX Posts: 88
edited 2012-03-28 06:55 in Propeller 1
I'm new to propeller and going through the Propeller Programming book by Parallax.
This is a proposed program (shown below) that turn on and off LEDs (pings 10-12) via motion detectors (pins 0-3). The activity LED is pin 9.
I would like my program to detect motion, turn on activity LED, turn on that LED for about 5 seconds. If someone can help in any way I would appreciate.
I would also like my program to use multiple cogs.
I realize that this may be a simple project,
Thanks in advance

CON

_CLKMODE=XTAL1+ PLL2X
_XINFREQ = 5_000_000

VAR
long stack1[32]
long stack2[32]
long stack3[32]

PUB Main
cognew(LEDDOOR, @stack1)
cognew(LEDSTAIR1, @stack2)
cognew(LEDSTAIR2, @stack3)

repeat
if (ina[0] ==1)
    LEDDOOR
    LEDACTIVE
  else
    LEDACTIVEOFF
 if (ina[1] ==1)
    LEDSTAIR1
    LEDACTIVE
  else
    LEDACTIVEOFF
if (ina[2] ==1)
    LEDSTAIR2
    LEDACTIVE
  else
    LEDACTIVEOFF

PRI wait2
  waitCnt ((Clkfreq*5 +cnt))

PUB LEDDOOR
outa[10] :=high
wait2
PUB LEDSTAIR1
outa[11] :=high
wait2
PUB LEDSTAIR2
outa[12] :=high
wait2

PUB LEDACTIVE
outa[9] :=high

PUB LEDACTIVEOFF
outa[9] :=low

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-03-26 11:31
    Your IF statements need to be indented below your Repeat, otherwise the program hangs there.
  • stuartXstuartX Posts: 88
    edited 2012-03-26 11:37
    Done. I'm still getting compile errors.
    Thanks for your input.
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-03-26 11:53
    You should start in smaller steps!
    1. make one LED blink
    2. make this a function you can call with a pin-number as parameter and the LED on this pin blinks
    3. extend the function with another pin-number for which it has to wait until it blinks 5s
    4. now you can use this function in COGNEW calls which watch/blink different pins

    This is a nice excercise for creating functions and starting different parallel COGs. But in the end you can also learn how hardware design makes programs easier. Because you can also read the input in parallel and output the read values in parallel by using simple operations. But let's get it running with your original approach first.

    What's totally missing in your code is setting the LED-pins to output mode using DIRA.
    What's wrong is for example using high. The propeller tool does not know anything about high. Make it 1 or %1 or $1 ;o)
  • stuartXstuartX Posts: 88
    edited 2012-03-26 12:16
    I will try that again and make those changes you suggested. I will repost the modified code.
    Thanks for the input.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-03-26 12:25
    You might want to back up a little and take a look at the Propeller Education materials that will walk you through some fundamental lessons.

    http://forums.parallax.com/showthread.php?89958-Propeller-Education-Kit-Labs-Tools-and-Applications&
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-26 12:28
    In addition to what MagIO2 mentioned, your program may not work as expected. In particular, you're calling the various LEDxxxx routines from more than one cog ... the main one and one of the ones you start from the main routine. When you have two (or more) cogs accessing the same I/O pins as outputs, the actual I/O pin state is the logical OR of all of the cogs that are trying to use it for an output. If one cog sets an I/O pin to output 1, it doesn't matter what any other cog does, that I/O pin will output a 1.
  • stuartXstuartX Posts: 88
    edited 2012-03-26 12:46
    @ElectircAye, I will look at that forum, I also have the Propeller programming book. As I mentioned I'm trying to learn the code. This is a failed attempt in which, I'm still learning. Thanks for your input.
    @Mike Green, I'm taking the advice from MagIO2. I understand you to say not to assign multiple cogs to the same pin. I'll change this around and assign one cog to one pin. I'll also re-read Chapter 2. Thank you very much

    I will repost my changes from all the input so far.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-03-26 12:46
    Mike is right. See pages 26-27 of the Propeller Manual. It talks a little about how the pins behave under cog control.
  • stuartXstuartX Posts: 88
    edited 2012-03-27 18:33
    Okay Guys,
    I have this piece working so far (below). This piece of code turn on a LED when a particular motion is triggered.
    What are my steps in assigning three cogs?
    CON
    _CLKMODE=XTAL1+ PLL2X
    _XINFREQ = 5_000_000
    
    PUB Main
    
      dira[16..23]~~
    
    
      repeat
        if (ina[0] ==1)
          outa[16] :=1
          waitcnt(clkfreq * 1 + cnt)
          outa[16] :=0
        if (ina[1] ==1)
            outa[19] :=1
            waitcnt(clkfreq * 2 + cnt)
            outa[19] :=0
        if (ina[2] ==1)
              outa[23] :=1
              waitcnt(clkfreq * 10 + cnt)
              outa[23] :=0
    
  • stuartXstuartX Posts: 88
    edited 2012-03-27 19:06
    Ok This seems to be working with all three cogs.


    [/code]


    CON
    _CLKMODE=XTAL1+ PLL2X
    _XINFREQ = 5_000_000

    VAR
    long StackA[32]
    long StackB[32]
    long StackC[32]
    PUB Main
    cognew (LED_Flash (16, 1, 1), @StackA)
    cognew (LED_Flash (19, 2, 2), @StackB)
    cognew (LED_Flash (23, 3, 3), @StackC)

    PUB LED_Flash (Pin, Duration, Count)
    Duration := clkfreq * Duration
    dira[16..23]~~


    repeat
    if (ina[0] ==1)
    !outa[16]
    waitcnt(Duration + cnt)
    outa[16] :=0

    if (ina[1] ==1)
    !outa[19]
    waitcnt(Duration + cnt)
    outa[19] :=0
    if (ina[2] ==1)
    !outa[23]
    waitcnt(Duration + cnt)
    outa[23] :=0
    [code]
  • stuartXstuartX Posts: 88
    edited 2012-03-28 04:01
    I just reviewed the last posted code and noticed that the paste did not properly indent the code properly. I pasted the same code under my method with the correct indents.
    Another few questions I have are:
    -Could I add another LED not assigned to a cog?
    -Also, I noticed that when its running multiple cogs, there was a delay in turning on the LED....Is this normal for running multiple cogs?


    [/code]
    PUB LED_Flash (Pin, Duration, Count)
    Duration := clkfreq * Duration
    dira[16..24]~~
    repeat
    if (ina[0] ==1)
    !outa[16]
    waitcnt(Duration + cnt)
    {outa[16] :=0}

    if (ina[1] ==1)
    !outa[19]
    waitcnt(Duration + cnt)
    {outa[19] :=0}
    if (ina[2] ==1)
    !outa[23]
    waitcnt(Duration + cnt)
    outa[23] :=0
    if (ina[6] ==1)
    !outa[24]
    waitcnt(Duration + cnt)
    outa[24] :=0
    [code]
  • stuartXstuartX Posts: 88
    edited 2012-03-28 04:09
    Here is a screenshot (attached), since the indents are not showing up.
    860 x 865 - 143K
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-03-28 04:57
    Why do you do a DIRA[16..24]~~ for each COG even if one COG should only change on output?
    DIRA[Pin]~~ sounds better to me!

    Why do all COGs watch all input pins and actually change different outa's? I thought the idea would be to have a COG watching one input and let one output blink?
    PUB LED_Flash( PINin, PINout, Duration, Count )
      Duration := clkfreq * Duration
      dira[ PINout ]~~
    
      repeat
        if( ina[ PINin ] )
           repeat Count*2
             !outa[ PINout ] 
             waitcnt( Duration + cnt )
           repeat until !ina[ PINin ]
    

    The repeat until waits until the input pin goes low again, so you'll only see low-high-transitions - of course you can remove it to have continuous blinking.

    As I said this is a lesson for parallel-programming and not the way how you'd do it in a real project because it's simply a waste of COGs.
  • stuartXstuartX Posts: 88
    edited 2012-03-28 05:48
    @MagIO2,
    I appreciate the input and I'm soaking in all the advice. It may not be as fast as I like because I'm figuring things out. I say all that to say, if you can bear with me.
    I read the Propeller Programming guide and executed some of the examples and understand a good portion of it. I'm practicing to interpret that into the correct code syntax.
    The reason why I'm trying to learn propeller is that I made a project at home in which I have six zones of LED lights that trigger with motion sensor. One zone has one motion and one PING sensor.
    At any one time 2 -3 sensors can trigger about the same time. From my little experience I found that the BS2 can't do this because of timing ( By this I mean sensing 3 motions at the same time while activating the LEDs for each of those zones).
    So that's my motivation, as well as working future projects in the future with the Propeller micro controller. I say all that to say that my thought process was to create a program with the Propeller (multi-cog) was to create a program
    that could handle at least 3 sensors being trigger at the same time while keeping the LED on for a set amount of time. That's why I followed the example in the book thinking that this seems to fit my requirements for my home project.
    Is my thought process incorrect about how I should use the cogs in my program?
    I'm open to all suggestions.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-03-28 06:48
    stuartX wrote: »
    I just reviewed the last posted code and noticed that the paste did not properly indent the code properly.....

    You need to switch your two CODE tags so the one with the slash comes last.

    attachment.php?attachmentid=78421&d=1297987572
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-03-28 06:53
    No, your thought is fine and the narural way of thinking if you have a multi-core controller! As long as you don't run out of COGs you can leave it this way.

    But one COG can also handle all the stuff of the current implementation by itself. So, if you want to learn more lessons there is room to improve!
  • stuartXstuartX Posts: 88
    edited 2012-03-28 06:55
    I used
    and
    
    . It seems my tabs or spaces did not go through. In my snapshot of BST it shows the indents, but when I did a paste between the tags, the indents did not show.
Sign In or Register to comment.