Simple program is erratic?
in Propeller 1
VAR
byte Button
PUB ButtonLED
dira[15]:= %1
dira[6]:= %0
repeat
if Button== 0
if ina[6]== 0
if Button== 1
if ina[6]== 1
!outa[15]
Here is my program using one push button to turn a LED on and off. Some times it works and some times it douse not? Button is P6. LED is P15.
Thank you for any improvements that anyone might give. Still reading all the forms from start to finish. Lots of information!!! 30 aug 2019(fri)
byte Button
PUB ButtonLED
dira[15]:= %1
dira[6]:= %0
repeat
if Button== 0
if ina[6]== 0
if Button== 1
if ina[6]== 1
!outa[15]
Here is my program using one push button to turn a LED on and off. Some times it works and some times it douse not? Button is P6. LED is P15.
Thank you for any improvements that anyone might give. Still reading all the forms from start to finish. Lots of information!!! 30 aug 2019(fri)
Comments
VAR byte Button PUB ButtonLED dira[15]:= %1 dira[6]:= %0 repeat if Button== 0 if ina[6]== 0 if Button== 1 if ina[6]== 1] !outa[15]
You have to take care of indentation. If you post code, use the [codx][/codx] tags ( replace codx by code )
PUB ButtonLED 'LED on while button pressed dira[15]:= 1 repeat outa[15] := ina[6]
If you want to toggle the LED with each button press you can try this:I did not build a circuit to test this code but I'm pretty sure it will work.
VAR byte Button PUB Toggle_LED | toggle 'LED toggles on and off with each button press dira[15]:= 1 'LED toggle := 0 repeat If ina[6] 'Is the button pressed? waitcnt(clkfreq / 10 + cnt) 'Debounce button. Let the button settle If ina[6] 'is the button still pressed? ++toggle Button := toggle // 2 ' // is modulus symbol. Button should only contain a one or zero outa[15] := Button waitcnt(clkfreq / 4 + cnt) 'need time to release button
Also, the variable Button is never changed so it's not necessary.
When a switch or push button is operated, the contacts will switch on/off intermittently many times over a very short instant of time. So your program needs to take care of this. The simplest way is with a timer. So, try this...
PUB ButtonLED dira[15]:= %1 ' enable the output to the LED dira[6]:= %0 ' disable the output for the button (not necessary_ repeat if ina[6] == 0 ' if the button is pressed !outa[15] ' this toggles the output repeat 10_000 ' wait some time for the button to debounce
If you shorten the repeat loop count (which is used a a simple delay) you may see the LED change state when the switch is pushed. This will be erratic since it depends on how many state changes the code sees when you press it.
BTW I have assumed the switch has a pullup resistor to 3V3 and the switch is to ground. (a value > 1K would work so 10K would be nice).
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 ' 5.00MHz rxPin = 31 'serial txPin = 30 baud = 115200 OBJ fdx : "FullDuplexSerial" PUB Main | baud2 'sequence rgb leds outa[15]~~ ' initialise pin 15 LED as "1" dira[15]~~ ' enable pin 15 LED as output fdx.start(rxPin,txPin,0,baud) ' start serial driver to PC '''fdx.tx(0) 'cls repeat fdx.str(string("If you can read this, the clock is 80MHz",13)) waitcnt(clkfreq/1 + cnt) ' delay 1s !outa[15] ' toggle LED