SPIN CODE EXAMPLES FOR THE BEGINNER (Public Version)
Dave Scanlan
Posts: 160
SPIN CODE EXAMPLES
IMPORTANT: The examples on this thread are for educational purposes only.·
PLEASE POST EXAMPLES OF SPIN CODE ON THIS THREAD THAT MEET THE FOLLOWING CRITERIA:
· 1. Can be used to learn Spin code
· 2. Have the necessary documentation
· 3.·Have been tested
THE DIFFICULTY LEVEL OF THESE EXAMPLES IS SET WITH THE FOLLOWING IN MIND:
(1) The user has no Propeller with which to execute the code
(2) The user has no documentation
·
DOCUMENTATION:
·· 1.·To view all examples, go into this thread.
·· 2. PDF File·of·many examples on this thread (See file attachment.)
······ (File compiled by Charlie Johnson, updated May 15, 2006)
DOWNLOAD CODE:
·· 1. ZIP FILE OF 16 EXAMPLES (See file attachment.)
······ (File compiled by Brain Riley)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
Post Edited (Dave Scanlan) : 6/2/2006 8:02:29 AM GMT
IMPORTANT: The examples on this thread are for educational purposes only.·
PLEASE POST EXAMPLES OF SPIN CODE ON THIS THREAD THAT MEET THE FOLLOWING CRITERIA:
· 1. Can be used to learn Spin code
· 2. Have the necessary documentation
· 3.·Have been tested
THE DIFFICULTY LEVEL OF THESE EXAMPLES IS SET WITH THE FOLLOWING IN MIND:
(1) The user has no Propeller with which to execute the code
(2) The user has no documentation
·
DOCUMENTATION:
·· 1.·To view all examples, go into this thread.
·· 2. PDF File·of·many examples on this thread (See file attachment.)
······ (File compiled by Charlie Johnson, updated May 15, 2006)
DOWNLOAD CODE:
·· 1. ZIP FILE OF 16 EXAMPLES (See file attachment.)
······ (File compiled by Brain Riley)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
Post Edited (Dave Scanlan) : 6/2/2006 8:02:29 AM GMT
zip
681K
Comments
ONE LED BLINKS FIVE TIMES·
FEWER COMMENTS (EASIER TO READ)
EXAMPLE 01
Post Edited (Dave Scanlan) : 3/15/2006 2:23:03 PM GMT
·HOW TO DECLARE A LOCAL VARIABLE AND CALL MORE THAN ONE PROCEDURE
FEWER COMMENTS (EASIER TO READ)
EXAMPLE 02
Post Edited (Dave Scanlan) : 3/19/2006 3:18:51 AM GMT
thanks a lot for sharing your code samples with the public. I think this and any following samples will help people interested in the Propeller and the Spin language to better understand this really powerful system.
Please let me add one word of caution to users who want to down-load Spin code examples:
Spin code is indentation-sensitive, i.e. instructions that shall belong to a block must be indented, like the four non-comment lines following the Repeat 5 in Dave's first example. When you highlight and copy Spin code from the screen, indentation might get lost, or messed up when you paste this text into the Propeller IDE, or any other text editor. Therefore, you better use the file attachment to download the source code into a local directory on your computer.
This also means that all others publishing Spin sample code should always attach a copy of the original spin file to their posts.
I'm pretty sure, this will help avoiding a lot of confusion. (See the other "Spin Code Formatting" thread that has grown pretty long in the meantime. Instead of "fooling around" with IE, Firefox, Opera, etc. simply make use of file attachments ).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
USING TWO COGS (TWO PROCESSORS)
FEWER COMMENTS (EASIER TO READ)
EXAMPLE 03
Post Edited (Dave Scanlan) : 3/19/2006 3:19:20 AM GMT
DISPLAYS TEXT ON A VIDEO MONITOR
EXAMPLE 04
FEWER COMMENTS (EASIER TO READ)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
Post Edited (Dave Scanlan) : 6/19/2006 4:32:50 PM GMT
The LED is hooked up between two pins so that it can be both forward biased to make it light up, and then reverse biased so that it can be used as a photodiode to measure ambient light levels. In the second mode, the 220 pf capacitor is charged up to 5 volts reverse bias across the LED, and the program measures how long it takes for the capacitor to discharge down to 1.65 volts (like RCTIME on the Stamp). The Propeller runs on 3.3 volts, and its input switching threshold is 1/2 of that, close to 1.65 volts.
The first program illustrates how to make Propeller pin an input, and the rate of flashing of the LED is proportional to the ambient light level. The program also shows a couple of different ways to sample the input: 1) using a program REPEAT loop, 2) using the WAITPEQ command (wait for pin equal...), and 3) using the WAITPNE command (wait for pin not equal ...).
The second program builds on that to turn the 8 leds on the demo board into a display like a VU meter, to show the ratiometric light level. The Propeller language has some pretty amazing math operators built in. The following is the condensed version of the code. Please see the listing for the documentation dcomponent. The Propeller IDE gives you several different views of your program documentation easier.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 3/5/2006 2:16:00 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
I can see that SPIN is going to be·great (and looks fairly simple)·to learn once we get the official documentation.
thanks for the posts above, keeping it simple is good at this stage.
Cheers,
Chris - West Oz
Thanks for the support.
When you get the docs and stuff, I hope you will add to this thread.
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
··········································
·Note: Posted with permission by Dave Scanlan
········· Follow his directions carefully.· Great demo.
kenjj
USING TWO COGS (PROCESSORS) WITH ARGUMENTS AND PARAMETERS
EXAMPLE 05
FEWER COMMENTS (EASIER TO READ)
Post Edited (Dave Scanlan) : 3/19/2006 3:20:23 AM GMT
Yes, I like the ability to see both. The detail is always nice, and as one gets onto things a bit more, then the condensed version is easier to read.
Thanks for your efforts, and keep up the good work.
Cheers,
Peter (pjv)
Thanks for responding.
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE LET·ME KNOW IF YOU FIND THE VERSION WITH LESS DOCUMENTATION
HELPFUL AND·I WILL CONTINUE TO POST TWO VERSIONS FOR EACH EXAMPLE.
··
Why in Example #4 "count" was not defined as a variable?
Good to see this type of code.
Chuck
The variable "count" was defined as a local variable in Example 04.· See EXAMPLE 02 for defining variables as local.
Thanks for responding and for your support.
Dave
I can honestly say I'm getting a good 'preview' for the SPIN language.
These examples are exciting to read. Both with and with out the examples.
(I'm not sure if it's Dave's program (as the poster) or Beau's (his name is in the file credits)
The example program is very interesting, and prompts the following questions:
#1: How did you derive the 50,000 as the basis for timing the display?
#2: The coding...
PUB Message | scan 'What happened to PUB Start ?
.....
.....
repeat '<- ok, a forever endless loop
repeat scan from 0 to 26 '<- scan was declaired as a pram into this routine, but no value ???
I take it the command 'repeat' can take a complex (or simple) form of a 'FOR NEXT' ?
If no PUB Start is present, the the SPIN startup code runs the first PUB it finds?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
SO the PUB Start could be PUB AnyName, and it can contain code for that one cog, launch other cogs etc...
So very cool...
Tracy, I did learn something here, today. I never knew that by revers biasing a LED you could read ambient light in a room. so very kewl...
How accurate is it?
And the whole "SHAKE A MESSAGE" thing is awesome. I was (and still am), puzzled as to how they get them to work. Now, I have an idea.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
The number was really just hunt-and-peck trial and error. Other values will certainly work,
I wanted to select a value that did not appear to "flicker" when the board was stationary.
This example demonstrates:
1) Using the I/O port to control LEDs
2) Using indexed data tables
3) Persistence of vision in combination with 1 and 2 to visually "see" by way of motion what is stored in the data table.
#3 is what will get you if you don't read the directions carefully, as some will attest.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
[noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
In example 5,
PUB Start
WaitPeriod_Arg := 8_000_000
CogNew(BlinkingLED_A16 (WaitPeriod_Arg), @Stack0)
.
.
.
.
'LED Blinks Fast
PRI BlinkingLED_A16 (WaitPeriod_Par) | Pin '
Is the WaitPeriod_Par a local variable?
If so could the WaitPeriod_Arg and WaitPeriod_Par both be called WaitPeriod? Or would this be a name conflict?
I understand the naming as it is for ease of getting the idea of Arguments and parameters accros.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Technically, there is a difference between a local variable and a parameter, but what they do share in common is that they both can be used in BlinkingLED_A16 like variables; that is, you can assign values to them, and you can access their values.
Because Waitperiod_Par is a "pass-by-value" parameter, it has its own location in memory just like Pin, a local variable.·Their scope is limited to just BlinkingLED_A16.· The only way you can get to WaitPeriod_Par from outside of·BlinkingLED_A16 is to appropriately use an argument.·
WaitPeriod_Par is not a local variable.
Both WaitPeriod_Par and Pin "die" as soon as the procedure has finished.
I suggest you try Google for more information.· I typically spend at least two hours explaining this stuff in lectures. Not so easy to understand.· Perhaps I should reference some of my powerpoint lecture slides.
·You could not use WaitPeriod for both the argument and the parameter in this example 05.· Yes, you are correct.· There would be a name conflict. I am about to post example 06 and in it you can use the same name for both the argument and its corresponding parameter.
Dave
SAME AS EXAMPLE 05, BUT WITH A LOCAL VARIABLE AS THE ARGUMENT
FEWER COMMENTS (EASIER TO READ)
EXAMPLE 06
··············· SAME AS EXAMPLE 05, BUT WITH A "LOCAL VARIABLE" AS THE ARGUMENT····
Post Edited (Dave Scanlan) : 3/19/2006 3:21:17 AM GMT
There is a similar problem in the full version of EXAMPLE 06. Part of a comment does not hold its format. The word "stack" gets pushed over to the left margin. This too looks ok in the preview.
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE LET·ME KNOW IF YOU FIND THE VERSION WITH LESS DOCUMENTATION
HELPFUL AND·I WILL CONTINUE TO POST TWO VERSIONS FOR EACH EXAMPLE.
··
Your explanation and Example 6 answered my question...Thanks
Yes i have to agree with above post, seeing code posted to help learn spin is great. I just recently purchased the SX Tool Kit and it would be nice if the·SX·forum would do the same.
Regards,
Kevin
Your comment is appreciated by all who post their programs here.
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE LET·ME KNOW IF YOU FIND THE VERSION WITH LESS DOCUMENTATION
HELPFUL AND·I WILL CONTINUE TO POST TWO VERSIONS FOR EACH EXAMPLE.
··