Storing (or recording) the state of switches?
TonyA
Posts: 226
Hi,
Is it possible to store, or "record" the state of 3 switches and then have those switch states automatically repeated?
push momentary switch·1·=·light up green led
"············"············"····· 3·= light up red led
"··········· "··········· "····· 2= light up yellow led
Then press·another button and have this sequence repeat automatically.
How would I go about doing this in Spin?
Thank for any advice.
Tony·
Is it possible to store, or "record" the state of 3 switches and then have those switch states automatically repeated?
push momentary switch·1·=·light up green led
"············"············"····· 3·= light up red led
"··········· "··········· "····· 2= light up yellow led
Then press·another button and have this sequence repeat automatically.
How would I go about doing this in Spin?
Thank for any advice.
Tony·
Comments
set up an array for buttons pressed and the time of press
initialize an index to the array at zero
save the initial counter
on button 1 through 3
time[noparse][[/noparse]index] = cnt - initial
pressed[noparse][[/noparse]index] = led number
initial = cnt
index = index + 1
wait for button to go back to start state -- to prevent filling up array with a single press
on button 4
turn off leds
wait to show that you have begun playback
loop from 0 to index
. turn on led using pressed[noparse][[/noparse]index]
. wait for time[noparse][[/noparse]index]
wait a bit to admire your replication
go back to top to clear index and reset initial to current time
Ok. (I'm just getting started with Spin and programming). So, I would set up 2 arrays (button pressed and time of press)?
The cnt keeps track of when I pressed a button?
Thanks,
Tony
Have a look at the waitcnt command in the manual for another use of cnt.
May I recommend that at this stage you do a bit more reading and just get some leds flashing using cnt to regulate the frequency, simple stuff you can then build from. This project is not complex but if you can break it down it will go much smoother and questions can be more directed if you get stuck (rather than the usual why doesn't my massive program work).
Cheers,
Graham
Will get back.
Tony
I was wondering too, if it is possible to store the events (button presses) in memory, and then retreive the events for repeating?
Thanks again.
Tony
see: http://forums.parallax.com/showthread.php?p=622841
WHEN the time comes.
Graham
For certain kinds of backgrounds and cognitive styles, the manual is perfect. If you read it... and it gives you that certain buzz you are looking for, great.
If not... go immediately to the lab sections of the Propeller Education Kit... it goes over how to hook up a button and get the state.
Remember... there are dozens of people with the same kinds of questions. You just happen to be the one asking. If you sleep on it and it still doesn't make sense, that's a question worth asking.
Rich
(Tony -- be sure to do the PE Kit exercises. See the dark blue sticky list at the top of the forum page.)
An interesting consideration which I avoided: can this be done with two cogs (yes) reasonably (I don't know)?·
I was wondering if anyone might be able to help with the algorithm laid out for me by Fred (above).
I'm not sure how I would go about storing values in an array, and then how to read them.
So, I would first set this up;
VAR
long buttons
Then, could I do this:
if(game_pad.button(game_pad#NES0_A)) 'if gamepad button A is pressed, assign a value to buttons[noparse][[/noparse]0]
buttons[noparse][[/noparse]0] := ? ' not sure would go here.
if(game_pad.button(game_pad#NES0_B))
buttons := ?
1. Am I thinking of this correctly in the above? If so, how would I then go from storing values in the array to then executing code which uses those values?
If my thinking above is completely wrong, where can I find some more info on storing values in arrays and then reading through the values?
The manual doesn't really get into this and I can't really understand SPIN example #16 in the forum thread.
(I don't want anyone to do the code for me, just help with learning more about storing values in arrays and reading them. )
Thanks again,
Tony
VAR
. LONG Buttons[noparse][[/noparse]10] ' array of 10 items.· See pages 315,316
2. buttons := ?
In my pseudocode, the number of the button, aka the io pin,·was put into an array. So the answer depends on how YOU number the buttons.
so you could decide on this:
NES0_A·is button 1
NES0_B is button 2
Simpler to me is to use the pin number of the led output turned on by the button:
NES0_A turns on led line x
So, button[noparse][[/noparse]number] := x
Fred
·
CON
_clkmode = xtal2 + pll8x ' enable external clock and pll times 8
_xinfreq = 10_000_000 + 0000 ' set frequency to 10 MHZ plus some error for xtals that are not exact add 1000-5000 usually works
VAR
long freqVar
long buttons
OBJ
game_pad : "gamepad_drv_001.spin" 'instantiate the gamepad driver object
snd : "NS_sound_drv_051_22khz_16bit.spin" 'sound driver
PUB main
game_pad.start 'gamepad driver's start method
game_pad.read 'gamepad driver's read method
snd.start(7) 'sound driver's start method
Repeat
if(game_pad.button(game_pad#NES0_A))
buttons[noparse][[/noparse]0] := snd.PlaySoundFM(0, snd#SHAPE_NOISE, 50, snd#SAMPLE_RATE, 255, $FFFF_FFFF)
else
buttons[noparse][[/noparse]0] := snd.PlaySoundFM(0, snd#SHAPE_NOISE, 50, snd#SAMPLE_RATE, 1, $FFFF_FFFF)
if(game_pad.button(game_pad#NES0_B))
buttons[noparse][[/noparse]1] := snd.PlaySoundFM(1, snd#SHAPE_SQUARE, 150, snd#SAMPLE_RATE, 255, $FFFF_FFFF)
else
buttons[noparse][[/noparse]1] := 0
if(game_pad.button(game_pad#NES0_START))
buttons[noparse][[/noparse]2] := snd.PlaySoundFM(1, snd#SHAPE_SAWTOOTH, 150, snd#SAMPLE_RATE, 255, $FFFF_FFFF)
else
buttons[noparse][[/noparse]2] := 0
if(game_pad.button(game_pad#NES0_SELECT)) 'this is supposed to play back the sounds
repeat buttons[noparse][[/noparse]0] to buttons 'this is where I try to "play back" the sounds commands
1. My problem understanding things, is where I am placing the snd.PlaySound... function. (I know this isn't right, but just to demonstrate what I would like to do.)
2. The last line of my pseudo code: repeat buttons[noparse][[/noparse]0] to buttons (I know that isn't correct but added it there to show what I would like to do).
And,·I'm missing the time array.
Thanks again.
Tony
Post Edited (TonyA) : 7/31/2007 11:43:34 PM GMT
Graham
I assume snd.PlaySoundFM is an object, but what sort of value does it represent? Which you are putting into button[noparse][[/noparse]0]. It looks like an object that actually does the sound function.
Meanwhile, your arrays need to properly defined.
Var
. long arrayone[noparse][[/noparse]10] ' 10 longs for arrayone, referenced arrayone[noparse][[/noparse]0] ... arrayone[noparse][[/noparse]9]
. long arraytime[noparse][[/noparse]10]
That aside, where to put the snd.PlaySound? Make the distinction between the call to the routine (object) and the object. So in a rookie spin program the object goes somewhere after the main program loop. The calls to the object obviously are after a button press or a replicated button press. So it seems that you have a middle logical layer between the snd.PlaySound object and the button state. I'd do this:
press button, thereby changing a pin state
note the pin, toss it into your array
use the pin number as flag to the middle logical layer
__________
Middle logical layer (pin) 'this translates a pin number to a sound call; in other words, here's where your program knows the parameters.
on pin do sndPlaySound A(parameters), sndPlaySound B(parameters), sndPlaySound C(parameters) 'jesus! what sort of pseudo code does that sound like?
_________
sndPlaySound(...parameters...) 'makes a sound specified by parameters. It knows how to do that, but it doesn't know the parameters.
________
So to duplicate the sound, just call the Middle logical layer with appropriate pin number. Which of course you've put into your arrays, yes?
Good questions:
1. A general question "can anyone tell me about arrays or point me towards information"
2. A very specific question "in my program this part does not seem to work correctly"
Avoid
1. "why doesn't my program work"
2. "Can you basically give me the code for the thing you just told me the outline of how to do"
And please don't get me wrong its about getting the maximum benefit for you not imposing some facist ideal, you will simply get much more useful information and will learn a heck of a lot more. At the moment I think you are lacking some general programming knowledge, that won't take long to gain but it will be quicker if you take the bull by the horns and fill the gaps, I see you have found the labs (that_rjo mentione above) they will help a lot.
Create an array:
VAR
long myarray[noparse][[/noparse]100] (Fred did say see page 315 for this)
Fill array element:
myarray[noparse][[/noparse]38] = ina ' storing the state of the inputs as an example
Use an array element:
x := myarray[noparse][[/noparse]38]
Ok, I have to·study much more of the basics. I appreciate the help and feedback/advice very much.
(I apologize for the "jumping ahead thing") I should have made my questions more clear and basic.)
Since I'm not really understanding any of the above, I'll reform my question.
I looked up the references to "array" and "arrays" in the manual; pages 107, 165 and 313. But still have some very basic questions. I am probably overlooking something, or just not smart enough to understand it yet. (Most likely, the latter).
I understand how to set up an array (suppose it is an array for 3 "elements"):
Var
long array[noparse][[/noparse]3] ·' this is an array that contains 3 elements, each element is 32 bits.
PUB something
array[noparse][[/noparse]0] := X 'assign a variable called "X" to the first element in the array
array[noparse][[/noparse]1]·:= Y 'assign another variable called "Y" to the second element in the array
array[noparse][[/noparse]2] := Z 'assign another variable called "Z" to the last element in the array
if something happens
then
X := a value 'if something happens, then the X variable is assigned a value.
if something else happens
then
Y := another value 'if something else happens, assign a value to the Y variable.
if yet something else happens
then
Z := another value ' if something happens, assign a value to the Z variable.
Now for my questions;
1. How do I read through the values contained in this array? (I have seen in the manual how to "Index" through a LookUp table, but I can't seem to find in the manual how to "Index" through values stored in an array like this. Please let me know if I am overlooking something in the manual.)
2. What can go in the "value" of each variable above? Must it be a 32 bit value? (I know I shouldn't ask this next question here, maybe wait until much later when I understant things more, but I'm going to take a chance and ask it... 3. Can you call a method by equating the variable to a method call?)
Please disregard the question # 3 in the parenthesis, if It's completely stupid. I'll worry about that stuff later.
Thanks again, I appreciate it very much.
I noticed that when I am posting my pseudo code, after hitting "submit" some of the characters are being omitted. For intance When I first posted the above, my [noparse]/noparse brackets were left out.
Tony
P.S. Why does the text in all of my posts start out in a large font, then successively become smaller with each paragraph?
Post Edited (TonyA) : 8/1/2007 5:01:07 PM GMT
2) You've declared array as longs or 32-bit values. Any 32-bit value can be stored there. If you want 16-bit values, use word. If you want 8-bit values, use byte.
3) A method call works like a function. When you say "X := foobah(y)", you're calling foobah with the argument y, then using foobah's result value to assign to X.
The forum software uses stuff in square brackets to control formatting. This includes numbers in square brackets. Put a space between the opening bracket and the contents. You can put a space in before the closing bracket, but you don't have to. Notice that I've done that in my example above.
I appreciate your help, but I just want to point out I didn't ask any of those questions: ie. Why isn't my program working? and I did state a few posts up that I don't want anyone to give me the code for my program.
Tony
An array is like a cabinet with a set of draws, each has a number so
myarray[noparse][[/noparse] 3] := 4
puts the number 4 in draw number 3.
When you need the number in draw 3 then you just write
time := myarray[noparse][[/noparse] 3]
Mike has shown how you can make a loop that will increment the draw number you are looking at (draw number is i), his code is adding up all the contents but you can do what you like.
Graham
Tony, I know you didn't ask question one, I listed it for your reference.
You may have said you didn't want code but I'm wondering how else the level of detail you were asking for could have been supplied.
Graham
One thing I was wondering. I thought that
myarray[noparse][[/noparse] 3] := 4· would put the number 4 in the first position, which is position 0? Or is it MSBF?
I also thought that if you wrote myarray[noparse][[/noparse] 3] you would have "drawers" or "positions" in the array like this;
0 (first position), 1 (second position) and 2 (last position or element in the array). Which would equal 3 "elements" or "drawers" or "boxes"·in the array.
Would that be·correct?
Thanks again,
Tony
Post Edited (TonyA) : 8/1/2007 5:41:03 PM GMT
Yeah, I deleted one of my previous questions to help minimize any further confusions, for anyone else reading. It was my mistake for posting my "pseudo code". It made my questions seem much more complicated than they really are.
Thanks again.
Post Edited (TonyA) : 8/1/2007 5:38:09 PM GMT
You are confusing initialiation with use.
Look again at what I wrote:
This part creates the array, it creates a thing called my array with 100 draws.
THis puts ina (the state of the pins but it could be a number) into draw 38 of the array.
This takes the contents of the 38th draw and puts it in x.
Ok so if I did this:
VAR
buttons [noparse][[/noparse] 3] 'these brackets keep getting deleted when I post!
buttons[noparse][[/noparse] 0] := 100 'puts the number 100 in "draw" 0
buttons[noparse][[/noparse] 1] := 300 'puts the number 300 in "draw" 1
buttons[noparse][[/noparse] 2] := 400 'puts the number 400 in "draw" 2
Is this correct?
Now how do I iterate through buttons[noparse][[/noparse] 0] to buttons[noparse][[/noparse] 2] ?
I have seen Mike do this:
repeat i from 0 to 2
is "i" here called the "index"?
So, I would have to declare "i" as a variable?
Also, in Mike's example what is this:
" array[noparse][[/noparse] i ] " ?
Thanks again,
Tony
Post Edited (TonyA) : 8/2/2007 3:39:01 PM GMT
yes! But everything after the button[noparse][[/noparse] 3] would be outside of the var section, in the start method for example.
That is correct, i is a variable that will contain the draw number and indexes the array.
yes indeed.
Well Mike's example is looping through the elements of an array that he has called Array.
Array[noparse][[/noparse] i] is the ith draw of that array. He has another variable called sum that he is adding the number stored in each draw of Array to to provide a total. It is an example rather than a solution to your specific problem.
Graham
Post Edited (Graham Stabler) : 8/2/2007 4:03:53 PM GMT