Propeller LED Program: 1st Program
stuartX
Posts: 88
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
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
Thanks for your input.
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)
Thanks for the input.
http://forums.parallax.com/showthread.php?89958-Propeller-Education-Kit-Labs-Tools-and-Applications&
@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.
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?
[/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]
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]
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?
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.
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.
You need to switch your two CODE tags so the one with the slash comes last.
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!