Shop OBEX P1 Docs P2 Docs Learn Events
Simple code issue... — Parallax Forums

Simple code issue...

Jake6075Jake6075 Posts: 3
edited 2014-10-07 18:33 in General Discussion
Hello everyone, I'm new here and to programming but I have electrical knowledge. Time to learn the other side. Anyways I've looked around but I feel like my problem is almost too basic... Here it is:

I have 8 buttons that come across 3 wires as binary. all of the buttons are also connected to another wire connected to a pin I called ButtonPress. When you press a certain button it selects a Preset of lights to come on and I have that much working but my question is how do I make the lights stay on after? Or asked a different way... How can I make the lights stay on and just switch the preset when the next button is pressed?

Here was my reason for using the fourth wire maybe there is a better way to do this too. With out that there would be no way of knowing that the "0" button had been pressed since there would be no voltage. You could say that all 0s is Pre0 but since the buttons ae momentary, every time you release a button you'd get a 0 value as well. thus switching to Pre0 regardless of the button you push. That led me to the thought of some how saving the data only when a button is pressed. Just not sure what the best way is and I'm sure I'm just missing something simple. Thanks in advance!

Jake

PUB LedsOn | Pre0, Pre1, Pre2, Pre3, Pre4, Pre5, Pre6, Pre7, Preset, index, ButtonPress, Temp


Pre0 := 0000
Pre1 := 0000
Pre2 := 0000
Pre3 := 1000
Pre4 := 0100
Pre5 := 1000
Pre6 := 0000
Pre7 := 0000
ButtonPress := ina[24]


repeat
dira[4..9] := 1111
dira[21..24] := 00
Preset := lookup(index: Pre0, Pre1, Pre2, Pre3, Pre4, Pre5, Pre6, Pre7)
index := ina[21..23]

if ButtonPress == 1
Temp := Preset

outa[4..9] := Temp

Comments

  • SapphireSapphire Posts: 496
    edited 2014-10-07 17:56
    I think the problem is in your if statement:
    if ButtonPress := 1
    

    should be
    if ButtonPress == 1
    
  • Jake6075Jake6075 Posts: 3
    edited 2014-10-07 18:06
    Thank you! I just changed it but the lights are still not lighting up. There must be something else wrong too.
  • David BetzDavid Betz Posts: 14,516
    edited 2014-10-07 18:12
    I think you need to put the assignment to ButtonPress inside the repeat loop. It is only sampled once as it is now.
    PUB LedsOn | Pre0, Pre1, Pre2, Pre3, Pre4, Pre5, Pre6, Pre7, Preset, index, ButtonPress, Temp 
    
        Pre0 := %000000
        Pre1 := %100000
        Pre2 := %010000
        Pre3 := %001000
        Pre4 := %000100
        Pre5 := %001000
        Pre6 := %010000
        Pre7 := %100000
    
        dira[4..9] := %111111
        dira[21..24] := %0000 
    
        repeat
            index := ina[21..23] 
            Preset := lookup(index: Pre0, Pre1, Pre2, Pre3, Pre4, Pre5, Pre6, Pre7)
    
            ButtonPress := ina[24]
            if ButtonPress == 1
                Temp := Preset
    
            outa[4..9] := Temp
    
  • SapphireSapphire Posts: 496
    edited 2014-10-07 18:14
    Do you have the percent (%) symbol in front of all your binary numbers?

    It's sometimes striped in the forum post, so check that too.
  • Jake6075Jake6075 Posts: 3
    edited 2014-10-07 18:19
    That was it!! Thank you very much!

    Jake
  • AribaAriba Posts: 2,690
    edited 2014-10-07 18:33
    I think the main problem is that you always write to OUTA[4..9], also when the IF ButtonPress==1 was false. It's hard to say without seeing the indentions.
    You should post your code in a code-box (The # button in the advanced editor).

    There are other things I would change:
    - read the index before you make the lookup, so the current value is used
    - read the index only if the buttons are pressed (ButtonPress=1)
    - define the bit ranges with [HighBit..LowBit] otherwise all your bit pattern get reversed.

    You can also simplify the whole code a bit, so it would look like that (I've not changed your bit order yet):
    PUB LedsOn | Preset, index, ButtonPress
    
     dira[4..9] := %111111
     dira[21..24] := %0000 
    
     repeat
       ButtonPress := ina[24]
    
       if ButtonPress == 1
         index := ina[21..23] 
         Preset := lookup(index: %000000, %100000, %010000, %001000, %000100, %001000, %010000, %100000)
         outa[4..9] := Preset
    

    The above code reads the index and outputs the pattern repeatly as long as you press a button. This works only if your ButtonPress signal goes to 0 before the index changes. If this is not the case you need to detect when a button gets pressed and write the LEDs only once when a button is newly pressed. For that you need another variable which holds the previous state of the button, so you can detect the change. The following code does this and simplifies it again a bit:
    PUB LedsOn | ButtonNew, ButtonOld
    
     dira[4..9] := %111111
     dira[21..24] := %0000 
    
     repeat
       ButtonNew := ina[24]
       if ButtonNew == 1 and ButtonOld == 0
         outa[4..9] := lookup(ina[21..23]: %000000, %100000, %010000, %001000, %000100, %001000, %010000, %100000)
    
       ButtonOld := ButtonNew
    

    Andy
Sign In or Register to comment.