BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Fri Jul 20, 2018 1:44 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 2:01 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Hi all, trying to work out how to latch a switch in code, so one button press says gives a 0 value and displays off on an lcd, press it again and it give a value of 1 and displays on. another press and back to off, etc...
Had a play with the dwnst and toggle commands but couldn't get it to work.
Any pointers in the right direction or sample code would be great. thanks in advance.


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 2:40 pm 
Offline
Master

Joined: Sun Jan 03, 2010 4:01 pm
Posts: 134
Location: Eau Claire, WI
Greetings,

I think I understand your question...so here I go :)

What I do is setup a variable that will track the status of each press of the button (using the button command). So each time the button is pressed the variable is incremented by 1; of course you will need to use an IF/THEN or WEND statement to ensure that once the variable meets a certain value that it is reset back to 0. Then of course I have a bunch of IF THEN statements to do what I need to do for each value (in this case display a message to the LCD).

I'm sure that there is a more elegant way of accomplishing this task but that is what makes sense to me :)

I'll try to find a code example for you later today to post (on wrong computer) if no one posts their example.

Thanks-Brian


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 5:04 pm 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
Here it is a code that will allow you to read the condition of port P0 and send a message of the status of the switch to the screen.

switch_input var bit
main

switch_input = IN0 'Read input port P0 ------change the port for the one your using

if switch_input = 0 then
serout s_out ,i9600,["Switch OFF" ,13]
elseif switch_input =1
serout s_out ,i9600,["Switch ON" ,13]
endif

pause 1000

goto Main


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 6:27 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Thanks for the reply. But im confused. Im using momentary tactile switches that as you push them, make contact until you let go. So every push is a 1, but i want every time there is a 1 to trigger a 1 then a 0 the a 1 then a 0 1 0 1 0 1 etc...
If i understand your program switch_input will be a 1 only when its pressed and held. Have i misunderstood? Thanks again


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 8:00 pm 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
Sorry I misunderstood what you was looking for.

Try this one to see if it is what you want.
'*********************************************
switch_input var bit
latch var bit
toggle_1 var bit
toggle_1 = 0 : latch = 0

main

switch_input = IN0 'Read input port P0

if switch_input = toggle_1 then
goto no_rd_sw
elseif switch_input = 1 and latch = 0
latch = 1 : toggle_1 = 1
elseif switch_input = 0 and latch = 1
latch = 0 : toggle_1 = 0
endif
no_rd_sw
pause 500
serout s_out,i9600,[ "Latch = ",dec latch,13]
goto Main


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Mon Jan 06, 2014 11:32 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
thanks, on the surface that looks like it should work. Ive cut and paste it into basicmicro studio, changing your serial display for my parallel with the line:
LCDWRITE p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr, "Latch = ",dec latch,13]

by the way what does the number 13 do?

also changed the input pin for IN12 as i have my lcd on pins in0-in7 on a nano 28, with 3 switches on in11 -in13 (p8-p10)

what happens now is, all it does is shows "latch = 1 II"
the switches do nothing.
can i use the p9 instead of in12?

thought id try and see what its doing in debug, but if i go into it, turn on the variable display, and try to use the step out or into, it does nothing. Doesnt seem to work. have i missed something. thanks again


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 1:25 am 
Offline
Master

Joined: Sun Jan 03, 2010 4:01 pm
Posts: 134
Location: Eau Claire, WI
The 13=carriage return...this should move the "cursor" to the next line of the LCD (or other display device).


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 2:04 am 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
Do you have a pull up in the switch? It worked in my micro test kit but I have a pull up. Add a 10K resistor from +5 to the micro input pin. The switch need to ground the input pin. Also at the beginning do a Input P12.


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 12:06 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
hi, the number 13 gives "II" on the lcd not at carriage return. strange?
also the switches already have the 10K and are part of a working circuit and code, this is just a new part but being tried on its own before i add it in.
can i use P9 instead of IN12, pin instead of input?
tauro did you try it on IN12 or IN0?
the changes i made seem right to me but are they?
thanks again


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 1:47 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
right,
got it working sort off. the issue was my pin misunderstanding p0 = IN0, p9 = IN9 not physical pin number 12/IN12. Duh!!

code works but not as expected.
If the switch is not pressed the code loops round giving a result of 1, if the switch is held for the 1/2 sec it shows a 0, but on release and next loop goes back to 1.

what i expected to see was every time the switch is pressed it would toggle until pressed again.
so 0 (unpressed) does nothing but when its pressed (sees a 1) it will show a 0, sees a 1 again show a 1, sees a 1 again shows a 0, sees a 1 again shows 1, etc...
i apologizes if i haven't explained myself properly and thanks again for your help


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 5:09 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
Note I am doing this on the fly so could have some issues. Also been mostly doing c/c++ for awhile so again
I am rusty! Note: code looks nicer when in code blocks ;)

Also, as mentioned, you need to use either a pull-up resistor (assumed here) or a pull-down resistor, otherwise the signal is simply floating and you don't really know what you will get...

Also side note: there is a basic command button that does most of this for you, including debouncing your press. Take a look at the manual on this as it shows you code, plus schematics...

Code:
switch_input var bit
latch var bit
latch = 0

main:
   switch_input = IN0 'Read input port P0
   if switch_input = 0 then ' Wait for button to be pressed
      do ' loop until the button is released
         pause 1   ' small delay to debounce
         switch_input = IN0 'Read input port P0
      while switch_input = 0
      latch = !latch
      serout s_out,i9600,[ "Latch = ",dec latch,13]
      pause 1   ' small delay to debounce
   endif
goto Main

Again no promises!


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 6:10 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
The momentary switch i am using is already wired to +ve via a 10K resister and works fine.
Ive looked at and modded your code but stuck on "latch = !latch" what does ! do?
thanks


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 6:17 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
When i first tried to do this i did go in the direction of the button and toggle command but struggled to get the two to work together. The button command i am using in my main program and and works fine with my switches. The toggle command i believe only works with a pin "toggle P0" not with a variable "toggle switch_input" correct?


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 7:18 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
latch = !latch

Sets the value latch to the logical NOT of latch. So it should toggle it.

Lots of ways to do it.
Code:
if latch then
   latch = 0
else
   latch = 1
endif


Also if I remember my syntax this should also work:
Code:
latch = latch ^ 1

This exclusive ors 1 with the current value,

Kurt


Top
 Profile  
 
 Post subject: Re: How to latch a switch in code?
PostPosted: Tue Jan 07, 2014 11:40 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Great, that works. This is what i have so far. Although it works im not convinced its tidy or the correct way to do it. Please take a look and make changes or suggestions. Thanks again for all your help.

Code:
worksw1   var word
switch_input var bit
latch var bit
latch = 0

switch
button p8,0,100,20,worksw1,1,main
goto switch
main:
   switch_input = IN8 'Read input port P0
   if switch_input = 0 then ' Wait for button to be pressed
      do ' loop until the button is released
         switch_input = IN8 'Read input port P0
      while switch_input = 0
      latch = latch ^1
      if latch = 0 then
      LCDWRITE  p0\p1\p7\p6\p5\p4,p2,[lcdclear,homelcd,scr, "Latch = off ",dec latch]
   else
      LCDWRITE  p0\p1\p7\p6\p5\p4,p2,[lcdclear,homelcd,scr, "Latch = on ",dec latch]
      endif
      endif
goto Main


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO