Simple code issue...
Jake6075
Posts: 3
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
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
should be
It's sometimes striped in the forum post, so check that too.
Jake
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):
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:
Andy