Stupid Newbie Question
SteveWoodrough
Posts: 190
I'm working my way slowly through my PE kit and thought I would try to do a little off road coding. One of the spin programs in the manual·lights a set of LED's in sequence. I decided to see if I could reverse the LED sequence to make the lights "bounce" from one end of the array to the other. I did it (see attached SPIN) but not in a very elegant fashion. I'd be interested in feedback on ways to write this better.
Thanks
Steve Woodrough
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Recent Project Videos:
http://www.youtube.com/watch?v=jI79Xsm_Kyk&feature=channel_page
Comments
I do not believe there is a stupid newbie question. We all learn at a different pace, and we all start from knowing nothing (at some point).
Your question is a double edge sword. There are really hundreds of ways to do what you did, "better" depends on who is critiquing, and how deep you want to get.
Just to give you some ideas of different ways of doing the same thing (ok, almost the same thing):
Although I didn't try it, this should work.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Maybe I should have read more carefully.
But like I said, there are hundreds of ways to do the same thing.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
What you did can't be done much better when you want a general shifter which lives with any state the outa has when called.
Maybe you could use some boolean algebra and use some operators to do the same thing without an explicit if statement:
outa[noparse][[/noparse]9..4]:= ( (outa[noparse][[/noparse]9..4]<<1) & %111111) #> 1
outa[noparse][[/noparse]9..4]<<1 first shifts
an & on the result of the shift with %111111 simply allows the 1 to be within the range we use for LEDs. If it's outside the result will be 0.
#> 1 limits the minimum, so the value we write back to outa[noparse][[/noparse]9..4] is not allowed to smaller than 1. 2 #> 1 returns 2 whereas 0 #> 1 returns 1
This solution looks a bit more like 'you really know what you do' ;o) but I my guess is that the operation itself is slower than using the if-version.
But to be honest, I don't know how your LED should bounce with that code? It's an endless loop, so light comes in from the right and leaves at the left.
You have to excuse Mag, he sometimes get way too deep and thorough.
Mag, ease up on the guy, your gonna make his head pop.
Truthfully, you did ask, and while some of this code is probably way over your experience level, they are all good examples of what can be done.
I figure after all those examples, you are continuing to read and do other lessons.
Also, just for information, not all of the code that was posted uses LED 4-9.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Posting questions on the forum is no substitute for RTFM'ng so I will continue with my studies. However, I have a few more questions.·
What is the difference between:··· :=· and ==?
Oop's!· I just realized that the sample code (Junk.spin)·I posted earlier was not anything like what I thought I posted.· Below is what I meant to post. ·My apologies in advance for not using the attachment manager.·
The net effect of this routine is very simple.· The LED light bounces from 4-9 and back from 9 to 4.· What I do not like is the repeat 6 command.· It seemed to be a very brute force method.·
I found that I could replace: if outa[noparse][[/noparse]9..4] == 0·with if outa[noparse][[/noparse]9..4] == %000000 in the first line of both repeat loops and the routine worked just fine.· (if outa[noparse][[/noparse]9..4] := %000000 DOES NOT work) Why?
For the second repeat loop (LED sequnce 9,8,7,6,5,4) I wanted to replace the:
if outa[noparse][[/noparse]9..4] == %000000 with if outa[noparse][[/noparse]9..4] == %100000 but the LED's only light in sequence 4,5,6,7,8,9 (first loop).· My obviously flawed logic was to try to identify the end of the first sequence (4-9) by waiting until only LED 9 is lit.· It does not work that way...Why?
If someone could shed a little light on this I would appreciate it.
Just for grins I tried an elseif outa[noparse][[/noparse]9..4] == %100000 and I get a compile error (expect variable)· Why? So clearly I'm in over my head and need to get back to Reading The Full Manual...
Thanks for taking the time,
Steve
PUB ShiftLedsLeft
··· dira[noparse][[/noparse]9..4] ~~
repeat················································· 'endless loop
··· repeat· 6·········································· 'Repeat loop 6 times
······
······ if outa[noparse][[/noparse]9..4] == 0······························ 'If LEDs off
········· outa[noparse][[/noparse]9..4] := %000001························ 'Set Output 4 high
·····
······ outa[noparse][[/noparse]9..4] <<= 1································ 'Shift output right 1 pin each loop
······ waitcnt(clkfreq/20 + cnt)······················· 'wait 1/20th second
··· repeat· 6·········································· 'Repeat loop 6 times
············································································
······ if outa[noparse][[/noparse]9..4] == 0······························ 'If LEDs off········
········· outa[noparse][[/noparse]9..4] := %100000························ 'Set Output 9 high··
······
······ outa[noparse][[/noparse]9..4] >>= 1································ 'Shift output left 1 pin each loop
······ waitcnt(clkfreq/20 + cnt)······················· 'wait 1/20th second
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Recent Project Videos:
http://www.youtube.com/watch?v=jI79Xsm_Kyk&feature=channel_page
Cool Cars:
http://www.youtube.com/watch?v=SLnPhYKZCqo&feature=channel_page
http://www.youtube.com/watch?v=d2xGkYN4v7g&feature=channel_page
== is a boolean operator. If both sides are equal, then it evaluates to true.
In this code you simply don't shift the 1 out of the bits you use. You have 6 bits, so, if bit 4 is set you know that you can shift it 5 times to the left - if bit 9 is set, you can shift it 5 times to the right.
If some other code is setting the bit and you want to shift it from exactly that position, then I'd do it like that:
In this version you could even set 2 or 3 LEDs anywhere you want and it will bounce all the bits.
EDIT:
Ok ... now I see a little difference to what your code is doing. You really shift the bit out of sight, so for 1/20 of a second no LED is switched·on at all. Is this what you want? Is your version even doing what you want?
The "if" lights LED 4 or LED 9, but just a few microseconds later it's already being shifted to the next position instead of staying for 1/20s.
So, let's modify my first change a bit and it will shift the bit out of sight as well and behave very much like your version.
Post Edited (MagIO2) : 4/29/2009 8:05:02 AM GMT
I am treading the same path, there are so many folk here that have vast experience that it is eay to tag on to the code that they pour out leaving you ignorant of the little things that are very, very important. I have started to read throungh the PE tutorial so I can cut down on the "stupid" mistakes. I still have problems with the parrallel processing, I have spent decades in linear-with-interupts land.
To break this down in a different way (not that SRLM's is incorrect)
":=" means - to make equal to.
"==" means - if equal to.
When I started those would confuse me, even after someone told me about assignment, and boolean.
I'm not sure when the Constant assignment phase starts in the lessons, but it makes your life much easier when it does. That way you can name a constant something that makes sense, and stop using literals (the actual value).
Just for basic information and getting help, the code tags are your friend. If you post code in the forum without the code tags, the forum software will reformat the code. Then we will not know if you indented correctly. Spin is indention sensitive, so always put any spin code between code tags. With short objects as yours it is usually easier to jut post the code. When the programs get bigger members do like you to attach the zip(archive) of your code. It saves having to scroll through 400 lines of code. Your code is not big enough to attach as of yet, so it is probably easier to just put it between the code tags.
(code) (/code) <
you must replace the "(" with "[noparse][[/noparse]".
Code tags will give you this
Don't get discouraged. If you hit a problem, just post the code. This is the best way to learn, trust me. Mag has been helping me with assembly for the propeller lately, and he is good with it. I've been using the propeller since it came out, and just now do I feel somewhat comfortable learning assembly. It's is a personal thing though.
Keep asking questions. It is the best way to learn.
Mag, I haven't had time to do the binary search, I just finished the assembly from hates.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Post Edited (James Long) : 4/29/2009 8:06:22 AM GMT
I'll explain this further, just in case Mag's explanation didn't drop the anchor.
The reason it doesn't work....... when the first loop finishes (repeated 6 times), there was no %100000 left (only %000000). You had shifted so far, the one (1) had scrolled off and was lost. You have 6 places, but the first is already at one, that means if you shift 6 times, you have shifted one time too many.
If you had used
It would have worked. Mag says this, just in a different way.
The elseif problem is unknown. We would need to see how you intended to use it (your code with it).
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
@James Long Thank you for your help as well. I now have a post it note on my project box that says:
":=" means - to make equal to. "==" means - if equal to. I take it then that := is more like a traditional Pbasic = so that x=3145 is expressed as x :=3145 and that == is used for logic type statements such as if x == 3145 then y := (?) 5.
What is the & in:
repeat until outa[noparse][[/noparse]9..4] & %000001
Is there a table in the prop manual that lists all these operators?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Recent Project Videos:
http://www.youtube.com/watch?v=jI79Xsm_Kyk&feature=channel_page
Cool Cars:
http://www.youtube.com/watch?v=SLnPhYKZCqo&feature=channel_page
http://www.youtube.com/watch?v=d2xGkYN4v7g&feature=channel_page
So that's a bitwise AND: it's the output register with the mask, and will keep repeating going until the 4 bit is 1.
Ah ... ehm ... this was only a hidden test, now I'm sure you understood ;o))
How the·until works in this case is: until expects a boolean value. Only 0·has the meaning of·false any other value is·like a·true. Until loops ... well .. until the boolean expression is true. So, when the output bit is exactly at the position given in the bitmask, the value will be true and the until stopps.
Post Edited (MagIO2) : 4/30/2009 7:22:10 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Recent Project Videos:
http://www.youtube.com/watch?v=jI79Xsm_Kyk&feature=channel_page
Cool Cars:
http://www.youtube.com/watch?v=SLnPhYKZCqo&feature=channel_page
http://www.youtube.com/watch?v=d2xGkYN4v7g&feature=channel_page