Shop OBEX P1 Docs P2 Docs Learn Events
Storing (or recording) the state of switches? — Parallax Forums

Storing (or recording) the state of switches?

TonyATonyA Posts: 226
edited 2007-08-02 15:59 in Propeller 1
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·

Comments

  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-07-30 18:30
    Sure.
    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
  • deSilvadeSilva Posts: 2,967
    edited 2007-07-30 18:44
    Nicely done, Fred - gives TonyA enough freedom to do some coding for himself.
  • TonyATonyA Posts: 226
    edited 2007-07-30 19:27
    Hi,

    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
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-07-30 20:21
    cnt is a system variable that increases with the system clock all the time (it loops around when full), by storing it in initial before the process and then taking the initial time from cnt after the process you have the elapsed time in clock cycles.

    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
  • TonyATonyA Posts: 226
    edited 2007-07-30 20:34
    Thanks, good idea.

    Will get back.

    Tony
  • TonyATonyA Posts: 226
    edited 2007-07-30 20:49
    Hi again,

    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
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-07-30 22:13
    By memory do you mean the EEPROM? The array is of course in RAM which is memory but it will disapear once the power is removed, if you wanted to save the data to the EEPROM where the program is also stored that is possible yes.

    see: http://forums.parallax.com/showthread.php?p=622841

    WHEN the time comes.

    Graham
  • rjo_rjo_ Posts: 1,825
    edited 2007-07-30 23:34
    TonyA,

    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
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-07-31 00:10
    deSilva said...
    Nicely done, Fred - gives TonyA enough freedom to do some coding for himself.
    It's a nice question and was fun to work it out in pseudocode. But really, I think I'm just a couple manual pages ahead of TonyA.

    (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)?·
  • TonyATonyA Posts: 226
    edited 2007-07-31 22:03
    Hi,

    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
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-07-31 23:09
    TonyA said...
    Hi,

    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 scure 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
    1.

    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
    ·
  • TonyATonyA Posts: 226
    edited 2007-07-31 23:38
    Thanks. Well, I wrote a pseudo-code thing. I thought maybe it would be easier to explain thing.

    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 StablerGraham Stabler Posts: 2,507
    edited 2007-08-01 08:43
    You really should read the manual, you need to look up things like arrays and repeat loops otherwise Fred will end up coding it and you will not learn anything. Personally I think you are trying to run before you can walk, using a few objects that others have created is not programming, learn the basics and you can then build a program using them.

    Graham
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-08-01 09:40
    This puzzles me: buttons[noparse][[/noparse]0] := snd.PlaySoundFM(0, snd#SHAPE_NOISE, 50, snd#SAMPLE_RATE, 255, $FFFF_FFFF)

    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?
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-01 16:25
    In general

    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]
  • TonyATonyA Posts: 226
    edited 2007-08-01 16:51
    Hi,

    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

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-01 17:06
    1) Look at the REPEAT statement. For example, to add up the values in the array, do:
       sum := 0
       repeat i from 0 to 2
          sum := sum + array[noparse][[/noparse] i ]
    
    


    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.
  • TonyATonyA Posts: 226
    edited 2007-08-01 17:07
    Graham,

    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
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-01 17:13
    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
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-01 17:18
    Everyone else, I was replying to a post Tony deleted.

    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
  • TonyATonyA Posts: 226
    edited 2007-08-01 17:20
    Thanks Mike and Graham, it's starting to come together now.

    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
  • TonyATonyA Posts: 226
    edited 2007-08-01 17:30
    Hi,

    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
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-02 14:15
    TonyA said...


    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?


    You are confusing initialiation with use.

    Look again at what I wrote:
    Graham said...

    Create an array:

    VAR
    long myarray[noparse][[/noparse]100] (Fred did say see page 315 for this)

    This part creates the array, it creates a thing called my array with 100 draws.
    Graham said...


    Fill array element:

    myarray[noparse][[/noparse]38] = ina ' storing the state of the inputs as an example


    THis puts ina (the state of the pins but it could be a number) into draw 38 of the array.
    Graham said...

    Use an array element:

    x := myarray[noparse][[/noparse]38]

    This takes the contents of the 38th draw and puts it in x.
  • TonyATonyA Posts: 226
    edited 2007-08-02 15:27
    Thanks.

    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

  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-02 15:59
    TonyA said...
    Thanks.

    Ok so if I did this:

    VAR

    buttons [noparse][[/noparse] 3] 'these brackets keep getting deleted when I post!<FONT style="FONT-SIZE: 10pt">

    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?


    yes! But everything after the button[noparse][[/noparse] 3] would be outside of the var section, in the start method for example.

    TonyA said...

    I have seen Mike do this:

    repeat i from 0 to 2

    is "i" here called the "index"?


    That is correct, i is a variable that will contain the draw number and indexes the array.
    TonyA said...

    So, I would have to declare "i" as a variable?

    yes indeed.
    TonyA said...

    Also, in Mike's example what is this:

    " array[noparse][[/noparse] i ] " ?


    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
Sign In or Register to comment.