simple code for operating push switches

djmikeys

New Member
what I want to do is use a button where when it is held down it is on and when you let go it is off. I am trying to write code so that when the button is pressed 1st time it turns a device on, when it is pressed a 2nd time it turns the device off. Is there any simple code to acheive this?
 

tmack

Member
Here you go, This should work for you.



'sample code for 08m
'Simple on/off
'pin 3 is set for input
'pin 1 is output

main: if pin3= 1 then one
if pin3= 0 then two
one: high 1
goto main
two: low 1
goto main
 

Matt_C

Member
Try this -

Code:
start:
do : loop until pin0 = 1
do : loop until pin0 = 0
toggle 0
goto start
Try in the sim.
Press pin0 and the output0 goes on
Press pin0 again and output0 goes off
 
Last edited:

tmack

Member
Here is something similiar I did that may be of interest to you also . When you press the button you toggle through a sequence of 3 outputs. So when you press the button once the first led is on you press the same button again the first turns off and the second turns on . You press it again and #2 turns off and #3 turns on.Then it just repeats. I am using it to toggle between 3 different cameras overt he internet using one relay it works great.

symbol PB = pin4
symbol Cam1 = 0
symbol Cam2 = 1
symbol Cam3 = 2

symbol state = b0

main:
high Cam1
low Cam2
low Cam3
state = 0
do

'wait for button press

do
loop until PB = 0

' debounce PB
do
pause 5
loop until PB=1
pause 5

inc state

if state > 2 then
state = 0
end if

on state gosub cam1up, cam2up, cam3up

loop

end

cam1up:
low Cam2
low Cam3
high Cam1

return

cam2up:
low Cam1
low Cam3
high Cam2

return

cam3up:
low Cam1
low Cam2
High Cam3

return
 

djmikeys

New Member
These codes seem to turn the device on when depressed and turn off when released (or am i doing something wrong). What I am looking for is when the button is pressed and released it turns on and when it is pressed and released a second time it turns off.

Thanks for your help so far, any more suggestions?
 

Matt_C

Member
My original code will do that.
In the sim you need to turn on the input and turn it off again.
Check closer and you will see.
 

djmikeys

New Member
What am I doning worong!?
what I am doing is using a sound sensor as a 'button'. when the volume gets to a set volume i want an LED to turn on and stay on. the volume will then decrease but when it reaches the same set volume again it will turn the LED off. The code I am using right now works fine for turning the LED off but it turns on with very little volume. The LED is on pin0 and the sound sensor is on pin4. Here is the code I am using:

-------------------------------------

symbol PB = pin4
symbol Cam1 = 0
symbol state = b0

main:

readadc 4, b1
debug b1
if b1 => 195 then goto ledon
if b1 =< 194 then goto ledoff

ledon:
state = 0
do

do
loop until PB = 0

do
pause 5
loop until PB=1
pause 5

inc state

if state > 2 then
state = 0
end if

on state gosub ledon, ledoff

loop

end

high cam1

goto main

ledoff:
low cam1

goto main

---------------------------

Any ideas?
 

Katzenjammer

New Member
What am I doning worong!?
what I am doing is using a sound sensor as a 'button'. when the volume gets to a set volume i want an LED to turn on and stay on.
What's generating the sound?

the volume will then decrease but when it reaches the same set volume again it will turn the LED off.
Why will it decrease?

The code I am using right now works fine for turning the LED off but it turns on with very little volume.
Did you miss out some words here?
 

Katzenjammer

New Member
It looks like you might have some problems with your code. (Indenting properly really helps spot many such problems)


Code:
symbol PB = pin4
symbol Cam1 = 0
symbol state = b0

main:

    readadc 4, b1
    debug b1
    if b1 => 195 then goto ledon
    if b1 =< 194 then goto ledoff

; ---------------------
ledon: 
    state = 0    ; first of 3 possible states, but only 2 are explicitly used
    do             ; begin main ledon loop

        do         
        loop until PB = 0    ; twiddle until pin4 goes low

        do
            pause 5
        loop until PB=1      ; twiddle, sampling every 5, until pin4 goes high 
        pause 5               ; wait for something

        inc state              ; bump state

        if state > 2 then    ; if state is too high, 
            state = 0          ; wrap 
        end if

        on state gosub ledon, ledoff    ; states 0 and 1 turn led on and off
                                                 ; state 2 apparently falls through and
                                                 ; jumps to ledon.  So states 0 and 2
                                                 ; jump to ledon, state 1 to ledoff
                           ; also, you're calling without a return, which should 
                           ; promptly crash the program, 
                           ; presuming this language uses a stack to store the 
                           ; return addresses from calls, which I suppose it must.

    loop           ; end main ledon loop (is an unconditional loop legal?)

    end            ; die unconditionally ; ***NEVER EXECUTES***

    high cam1   ; turn on (?a camera?)  ; ***NEVER EXECUTES***

goto main       ; ***NEVER EXECUTES***

; ---------------------
ledoff:
    low cam1    ; turn off whatever it is

goto main       ; jump to top
 
Last edited:

BeanieBots

Moderator
Also, we seem to have moved from a digital input to an analogue one!

So, let's forget for the time being whatever complex amplifier/filter arrangement you have that is converting sound into a nice clean steady analogue voltage and worry about what the required function is.

Is this correct?
Quiet = 194
Loud = 195

Not much distinction between the two, but whatever.
So, if the sound goes above "loud" then toggle the output but only if it's previously been below "quiet" or toggle EVERY time it goes above "loud"?
In which case, what is the lower threshold for?
 

Katzenjammer

New Member
Does this look roughly like the code you want?


Code:
symbol now_off    = 0 
symbol now_on    = 1
symbol lo_enough = 190  ; having a "no-op"  region (191-199 in this example)
symbol hi_enough = 200  ; keeps your whatzits from going out of their tiny minds
symbol state = bx

state = now_off ; initialise your state
main:
    ???? ????    ; somehow read input to get your value
    if value <= lo_enough then
        if state = now_on then 
            gosub turn_stuff_off
        endif
    elseif value >= hi_enough then 
        if state = now_off then
            gosub turn_stuff_on
        endif
    endif
goto main

; ------------------------
turn_stuff_on:
    ???? ????                   ; switch on your led, camera, or whatever it is
    state = now_on          ; update your state 
return

; -------------------------
turn_stuff_off:
    ???? ????                   ; switch off your led, camera, or thing
    state = now_off          ; update your state
return
 
Last edited:
Top