How do I get a LED to turn ON and OFF using this code
sam_sam_sam
Posts: 2,286
This might be a stupid Question But I am very new to this
The reason why there are two post on the same thing is because the first one disappeared then reappeared
The reason why there are two post on the same thing is because the first one disappeared then reappeared
pub wait_press(pin, ms) | debounce, t '' waits for (active-high) button press of ms milliseconds dira[pin] := 0 ' set pin to input mode debounce := 0 ' clear debounce timer t := cnt ' sync with system cnt repeat until (debounce == ms) ' wait specified db time waitcnt(t += clkfreq/1_000) ' hold 1ms debounce := ++debounce * ina[pin] ' scan input
Comments
Please don't call me out in the thread subject line; I'm not your personal consultant and it's rude to the others (that stopped reading after seeing my name) that could/would help you.
The only reason that I put your name in the subject line was that you wrote the code that i was asking the question I am sorry if this is a no,no I know this next time but how would I use what I had posted I wanted to see how this code worked
What would I have to change to get it to work the other way to flash when I push the button
Note how the debounce method is used exactly as I first presented it. The ability to pass parameters to methods is what allows use to write code that can be re-used. Embedding pin constants, as you have done, is a very bad idea.
instead of
debounce := ++debounce * ina[pin]
write
debounce := ++debounce * (1 - ina[pin] )
By the way ... if you change the second parameter of wait_press or wait_release to 500 you can simply remove the waitcnt in the main loop.
PS: Guess the buttons you both have are connected differently. One having a pushup, the other one having a pulldown resistor.
I just start working through..... Propeller Education Lab ver 1.2 I ....was using the example that I have seen so far
Embedding pin constants, as you have done, is a very bad idea.
I Know that I Still have a LOT to learn about How to write Spin Code
I am very great full for the help and welcome being corrected when it is need
Thank You
Guess the buttons you both have are connected differently. One having a pushup, the other one having a pulldown resistor.
I am using the Propeller Professional Development Board and I had to use this one for it to work the same way
debounce := ++debounce * (1 - ina[pin] )
Thank you for your help
Now if I was writting this for a Basic Stamp I know how to write this
I can write this and it will keep the LED ON all the time
For this
have you worked out why it is bad to embed constants like pin numbers in your code? If not then imagine your program was very large and used this constant a lot.
Graham
I did not know that this was the only option for this code could you show me the other code for this
unless you add a second button. If that's what you want then the debounce code would be different.
Could you show me what this code would look like
You'd simply do a
This code waits until the button has been pressed for at least 200ms. If so, the LED is toggled, which means if the LED was on it will be switched off, if it was off it will be switched on. After that the wait_release simply makes sure that the button has been released. So, a long push won't toggle again. It's always toggled with the next push.
Maybe that's what you want?!
(BTW toggeling is not the only way you can switch an LED on/off with only one button - you could for example measure the time and a short push will switch it on, a long push will switch it off)
I also want some thing that when you push the button it wait for some amout of time before that statement become true and turn ON the LED
Then when you push the again same thing again but instead it turn the LED OFF
Can this be done with one button or dose this have to be done with two buttons
toggling is your only option unless you add a second button
I did not know that this was the only option for this code could you show me the other code for this
unless you add a second button. If that's what you want then the debounce code would be different.
Could you show me what this code would look like
I would like to know what you exactly mean by
maybe I got tomatos on my eyes - but - I would state the oposite: in most cases it is a good idea to use constants.
But I assume that you are a well experienced programmer and that you have good reasons to write this.
I'm really interested to know and understand the reasons.
After thinking about it I still can only guess:
- constant names that say to less about their meaning?
- everything is meant ironic????
best regards
Stefan
Embedding pin constants, as you have done, is a very bad idea. this is what JonnyMac was talking about
This the way it should have been
I know what Graham talking about I have had very long Basic Stamp code routine before and if you do not have CON define pins it hard to follow the code that is written
If not then imagine your program was very large and used this constant a lot.