Turn on 2 LEDs with 1 button
stefhorn
Posts: 12
in BASIC Stamp
Hi I'm quite new to microprocessors.
I have a basic stamp 2 and I am working on a program which turns turns two LEDs on an off with one button as follows: Button is pressed the first time and LED1 goes on. Button is pressed the second time and both LEDs are on. Button is pressed the third time and both LEDs go off. I played around all morning with "Button" and "count" etc but had not much luck as I am still lacking the required knowledge. Could somebody help me please.
I have a basic stamp 2 and I am working on a program which turns turns two LEDs on an off with one button as follows: Button is pressed the first time and LED1 goes on. Button is pressed the second time and both LEDs are on. Button is pressed the third time and both LEDs go off. I played around all morning with "Button" and "count" etc but had not much luck as I am still lacking the required knowledge. Could somebody help me please.
Comments
I have got that far with the code but struggle with the part that keeps LEDs to stay on when the button is released. I don't expect you to write code for me, just need to be pointed into the right direction. Thanks
Thanks, l have been there but
couldn't find any help that keeps the LEDs on when the button is released again. Even Google did not come up with any answers.
Yes, the hardware is all ready to go. Just need to complete the code. I feel like learning a new language. Its hard to get started but the penny will drop eventually. I purchased the basic stamp over than 10 years ago for a project that I never completed as I just didn't have the time and patients. Now semi retired I pulled it out of the drawer eager to give it another go.
Once I have more practice, I might figure out a way to save the state of ledState to memory ( if at all possible with basic stamp2) so next time I turn the light on the same number of bulbs turn on as last time.
That can be fixed by a subroutine that forces the button to be released. This version has that, as well as READ and WRITE to save your state to non-volatile memory.
If not, you need to know about interfacing to high voltages before you hook anything up.
Thanks, I am intending to interface the 2 outputs as well as the input with high voltage relays?
My neighbor is an electrician, I will ask him to check for compliance.
I would definitely suggest using solid state relays between the BS2 and the lights to switch them on and off.
When I moved to Dallas in the late 90's I joined the Dallas Personal Robotics Group. The tag line on their web site was, "It's harder than it looks." I'd prefer to say, "There's more to it than you think -- if you want to do it well." We only see the skill of the professional athlete while on display, never the hours of practice to get to that level. Like many here, I've been programming for a long time and I tend to do things -- even the simple things -- as if they were a professional project. Taking simple things for granted, I've found, often leads to trouble.
If you want to be an embedded programmer there are a number of languages you *could* learn -- but none are better or worse except for the way you want to do things. For the Propeller, I prefer Spin (its native language). Others prefer C. Many prefer BASIC. A few in the forums prefer Forth. All get the job done -- and that's what matters: getting the job done. I've been consulting with the Propeller since 2006 and never once has a client demanded which programming language I use; they only ask me to meet their operational requirements. The BASIC Stamp is only programmable in BASIC. If you decide to work with the Propeller, or other processors, your choice of programming tools will open up. While I focus on Spin, I've learned and understand the other languages as well, because working with them a bit causes me to think differently, and that can be beneficial when solving programming problems.
I got a ceiling light for our kitchen that has a function like yours built in, based on toggling the switch to cycle through the states. Instead of changing the number of bulbs lit, it changes the color temperature of the light. I wonder exactly how they do that.
Just because you neighbour is an electrician doesn’t mean he understands electronics and the isolation necessary. But he will have a healthy regard for the mains because he knows it can kill.
Photos before you hook it up might help but don’t expect to get authoritative advice. There are also legal requirements too.
I’m just trying to ensure you’re not going to take unnecessary risks, or endanger anyone else.
Thank you, another interesting solution. I am not trying to pretend that I understand all of it. One of the 3 light bulbs is directly connected to the light switch so only 2 bulbs have to be controlled. I managed to change that. I does not appear to read / write to memory which is not a big problem. I ordered the parts and hopefully have it all working by Xmas.
...ok, so I attempted to unpack this bit of code knowing full well Mr. Allen's acumen in getting the BS to "dance". But, I'm stumped. I couldn't get any further than the use of INA and IN0 as used in the lines marked with "<---***".
is defined as PIN 7, which puts it into pin group INB?
...I just don't understand. INA consists of pins 0-3. And I believe INA can be treated as an array. But does this mean BS pin 7 is wired to a pin in group INA? Or is it the *state* of BtnPin that's used as the index into the INA array?
...same confusion as above. Why is INA/IN0 involved when pin 7 is the button input? Again, is it the *state* of BtnPin the issue?
After using the BS2 for a few years, I'm actually ashamed to ask these questions. Once they are clarified, I'll move to unpacking the "math" line.
Methinks there may be other readers wondering the same thing, so I'll be the sacrificial question-asker.
I could have written, that is better. I slipped on the PBASIC syntax options for the PIN specifier. This also works,
The PIN specifier is rather tricky, and I have a whole page on it at this URL, BS2pbasic25.htm#PIN_examples. Sometimes, as in the first usage above, it is treated as reading the state of the pin, and in the second usage as an array index offset from IN0, it is treated as the pin number. The page also includes a summary of an email that Jeff Martin had sent me to clarify the logic of it.
There is more about the math for pushbutton state machines at this URL.
A number of my projects involve switches; one has 10 push buttons. I'll review the method of reading them as you noted above.
And thank you for the reminder of the wealth of Stamp information on your site.
Agreed.
I had occasion to pick up an old project, a BASIC Stamp to read the output from 20 rain gages and two anemometers. All switch actions to be counted. I'd prototyped it on the Stamp. But I ended up using the same algorithm in PIC chips, in order to conserves power, using the wake up on change feature, and also to isolate the exposed sensors from the Stamp core. This was used for a study in forest hydrology, looking at rainfall distribution on the forest edge and under individual trees. The photo has RJ11 connectors for the rain gages, the PIC chips by each row, and the Stamp and support circuits on the plug-in board.
So changed the code a little:
'{$STAMP BS2}
'{$PBASIC 2.5}
BtnPin PIN 7
leds VAR OUTA ' control 3 leds, on p0 to p2, patterns 000, 001, 011, 111
state VAR Nib ' state increments from 0 to 3 and back to 0, to light 0,1,2 or 3 leds.
oldBtn VAR Bit
newBtn VAR Bit
main:
HIGH 01 'to stop relay 1 clicking on and off when controller is first powered up
HIGH 00 ' as above
DIRA = %0111
state = 2 ' initially both bulbs are off
oldbtn = IN0(btnPin)
DO
newbtn = IN0(BtnPin)
state = ((newBtn ^ oldBtn & oldBtn) + state) // 3
PAUSE 100
'oldbtn = newbtn
leds = DCD state -1
PAUSE 5 ' this pause can be 5ms to 100ms, or it can hold other code to do other tasks.
LOOP
However, to make it all work as intended, I need the state sequence reversed to 2, 1, 0, 2....... but can`t figure out how.
leds = ~(DCD state - 1)
The ~ operator on the Stamp simply inverts all the bits, so as the state goes 0,1,2, the leds go In any case, the program can set an initial state for when the power first comes on.
Alternatively, you could use the LOOKUP command, like Jon did in the program he posted above. That is a clearer way to do it. I enjoy tricky math, but it is hard to explain even to one's self when coming back to it later. With LOOKUP, there is no confusion, and the order of states can be anything you need.
Another alternative would be to run through the states backward, leds = DCD (2 - state) - 1. So state goes 0,1,2 and 2-state goes 2,1,0.
I see that the line, 'oldbtn = newbtn is commented out. No, that has to be there, memory of the past state, or the logic won't work.
Thank you everyone for helping me with my first Basic Stamp project.