You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old January 30th, 2020, 01:25 AM   #1
Puntigamer
Member
Austria

Puntigamer is offline
 
Join Date: Jan 2020
Location: Graz
Posts: 3
Book ST program bit toggle problem

Hello everyone.

I'm trying to write a program in ST which would toggle a bit with one button. So when the button is pushed the bool sets to 1 and when I push again it goes back to zero. I kinda did that but the problem is that if I hold the button, then the bit is switching on and off all the time. Can someone support me with a simple solution?
  Reply With Quote
Old January 30th, 2020, 01:29 AM   #2
janner_10
Lifetime Supporting Member
United Kingdom

janner_10 is offline
 
Join Date: Dec 2014
Location: Tewkesbury
Posts: 957
http://www.plctalk.net/qanda/showthread.php?t=95862

The search function is very useful
  Reply With Quote
Old January 30th, 2020, 04:36 AM   #3
parky
Member
United Kingdom

parky is offline
 
parky's Avatar
 
Join Date: Oct 2004
Location: Midlands
Posts: 1,665
Try this:
I have written it in a way that shows built one shots, however, Depending on your plc you could use OSR or Pulse, toggle if they exist.

On_Oneshot:= Button & NOT ON_Latch;
ON_Latch:= Button;
Off_Oneshot:= Button & NOT OFF_Latch & Toggle_Bit;
Off_Latch:= Button;
RST( Off_Oneshot , Toggle_Bit );
SET ((On_Oneshot & NOT Off_Oneshot), Toggle_Bit);

Pic shows it in ladder:
Attached Images
File Type: png On_Off.png (15.6 KB, 120 views)
  Reply With Quote
Old January 30th, 2020, 06:39 AM   #4
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 356
Quote:
Originally Posted by parky View Post
Pic shows it in ladder:
More my OCD than picky, and the OP wanted ST anyway, but the [Button] XICs/NOs in the last two rungs are redundant.

The way I know is
  1. The are not in the ST
  2. I always put them in too at first, and then remove them later

Also, I would assume [Button] includes debounce, if necessary.

Last edited by drbitboy; January 30th, 2020 at 06:40 AM. Reason: add debounce caveat
  Reply With Quote
Old January 30th, 2020, 07:01 AM   #5
parky
Member
United Kingdom

parky is offline
 
parky's Avatar
 
Join Date: Oct 2004
Location: Midlands
Posts: 1,665
I agree they are not needed, this was a quick & dirty bit of code and my time is precious. However as you pointed out they are not in the ST. Perhaps instead of criticising you could give the poster some of your ideas.
  Reply With Quote
Old January 30th, 2020, 08:06 PM   #6
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 356
Quote:
Originally Posted by parky View Post
instead of criticising...
parky,

I assure I was not criticising; I was trying to improve on your answer, and I apologize for doing it publicly where it could look like I was criticising. That was poor judgement on my part: I should have sent you a PM and then tha could correct it th'self. I am truly sorry.

To be clear: your code was more than adequate and, other than a very very minor tweak, nothing I could add would improve on it. Believe me I tried: I'm a habitual code golfer.

My OCD comes in because, somewhere down the line, the OP and future users are going to look at this thread and find your excellent code, and they may use it, so I wanted them to have your best version.

I tried to make sure that what I said was not aimed at you personally, and was meant to denigrate neither you nor your effort in any way, by adding the qualifiers at the end: 1) you knew not to put the redundant check into the ST; 2) I knew I would have put the redundant check in as well on the first pass, had I answered before you; 3) I added a to the latter, i.e. I was laughing at and mocking myself.

I only made the comment for essentially the reason you said: you did it quick and dirty, the same way I would have done; I would have likely written very similar code, with the same redundant check; and then I would have gone d'Oh myself when you corrected my code.
  Reply With Quote
Old January 31st, 2020, 05:29 AM   #7
parky
Member
United Kingdom

parky is offline
 
parky's Avatar
 
Join Date: Oct 2004
Location: Midlands
Posts: 1,665
No problems Drbitboy, perhaps I was a bit over the top and tetchy, must be my age lol. Your explanation is accepted without question.
Anyway, just an explanation as to why I put ladder version in there:
As I did not know if the original poster had any experience in ST, and assumed that they may have at least some in ladder, I created it in ladder to show how it could be done as a reference, I would have used the PLS (OSR for you AB guys) function but not knowing what plc it was intended for (although most have that function) and normally would integrate the pulse two rungs into one if rolling my own, but the idea was to show how a pulse can be generated and for learners two rungs makes a bit more sense. If I had a little more time I would of added comments but there you go.
  Reply With Quote
Old February 1st, 2020, 08:40 AM   #8
Gene Bond
Member
United States

Gene Bond is offline
 
Join Date: May 2004
Location: Indianapolis, IN
Posts: 418
Along the same lines (Ladder), here's a Doc I picked up on this group a while back which shows multiple ways to 'skin that cat' (in Ladder).

It may show some different thought processes to convert to ST (but not by me!)
Attached Files
File Type: pdf ALTERNATOR METHODS.pdf (36.7 KB, 10 views)
  Reply With Quote
Old February 1st, 2020, 09:34 AM   #9
Lare
Member
Finland

Lare is offline
 
Join Date: Jan 2006
Location: Finland
Posts: 1,295
Quote:
Originally Posted by Gene Bond View Post
Along the same lines (Ladder), here's a Doc I picked up on this group a while back which shows multiple ways to 'skin that cat' (in Ladder).

It may show some different thought processes to convert to ST (but not by me!)

If you go to plctalk.net main page, select downloads and then Allen bradley, Alternator methods is one example program



http://www.plcs.net/downloads/index....=Allen_Bradley
  Reply With Quote
Old February 1st, 2020, 10:26 AM   #10
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 7,060
This should be a one liner folks.
ST has a RisingEdge FB and an XOR function.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old February 1st, 2020, 11:02 AM   #11
nehpets
Member
Wales

nehpets is offline
 
nehpets's Avatar
 
Join Date: Mar 2009
Location: Wales
Posts: 415
The Op doesn't indicate which platform they are using so generically

something like this may give food for thought.



if pbutton and not oneshot_mem
oneshot = 1;
else
oneshot = 0;
end if;
oneshot_mem = pbutton;

If oneshot and not output
Output = 1;
Elseif oneshot and output
Output = 0;
Endif;


Steve
__________________
Why do programmers get confused between Halloween and Christmas - OCT31 = DEC25
  Reply With Quote
Old February 1st, 2020, 02:02 PM   #12
ndzied1
Lifetime Supporting Member
United States

ndzied1 is offline
 
ndzied1's Avatar
 
Join Date: Aug 2002
Location: Chicago, Illinois
Posts: 2,488
If you can define an R_TRIG then it's very easy (the following was checked in CoDeSys).

Code:
VAR
   Signal:	BOOL;
   Out:		BOOL;
   OneShot:	R_TRIG;
END_VAR

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

OneShot(CLK:= Signal);

IF OneShot.Q THEN
   Out := NOT Out;
END_IF
__________________
nOrM
======================
nOrM=Norman Dziedzic Jr.
I've never been to China but my phone has.
  Reply With Quote
Old February 1st, 2020, 05:29 PM   #13
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 7,060
out := out xor OneShot.Q;


It has been a while since I programmed in ST. I know the R_TRIG is a FB. One must instantiate an instance of R_TRIG then it can be used.


I don't see why ladder people don't use this simple inline form. It is much closer to LD except now the output is on the left instead of on the right.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old February 2nd, 2020, 06:27 AM   #14
LadderLogic
Member
United States

LadderLogic is offline
 
LadderLogic's Avatar
 
Join Date: Jun 2003
Location: Chicagolandia
Posts: 1,357
Quote:
Originally Posted by Peter Nachtwey View Post
I don't see why ladder people don't use this simple inline form. It is much closer to LD except now the output is on the left instead of on the right.
I think this is because for most of the "ladder people" (OK, OK, I am speaking just for myself) having a one-shot functionality implemented in ST as a function block that requires instantiation feels like anathema. This is such a basic and ubiquitous thing that it has to be a basic command and to hell with the need to maintain internal state bit. Moreover, many ladder people have been a bit spoiled by the ability to make one-shots by just using modifiers for the contacts and coils commands, like it is done for Omron, rather than having to use a separate bit for a one-shot.

If something like that had ever existed in ST, the answer would look something like

Code:
out := out xor onp(Signal);
- where "onp" ("on pulse", or it could be written in some other simple form) is just another command that would not have to be declared. A perfect one-liner with minimal hassle.
__________________
Don't trust, don't fear, don't beg...
  Reply With Quote
Old February 2nd, 2020, 11:18 AM   #15
parky
Member
United Kingdom

parky is offline
 
parky's Avatar
 
Join Date: Oct 2004
Location: Midlands
Posts: 1,665
Mitsubishi have one shot in LD & ST and that is what I coded in, however, the reason for the home brew ones was that I did not know what plc the poster is using and for clarity did it that way, interestingly, using IF & Else statements on some platforms produces more code than using the equivalent in LD or STL or individual lines like Var3 := Var1 & NOT Var2;.
IF statements tend (on some platforms compiles into STL code but uses jumps round the else statement).
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Problem with simple S7 FC, would you take a look? Paul B LIVE PLC Questions And Answers 5 October 8th, 2010 07:47 AM
fishenguy fishenguy LIVE PLC Questions And Answers 54 November 13th, 2007 08:33 PM
S7 - using Arrays with Multiplex Addressing in ProTool RMA LIVE PLC Questions And Answers 4 August 12th, 2005 07:39 AM
10 Bit Encoder Decoder/ GE90-30 Control Freak LIVE PLC Questions And Answers 6 July 12th, 2005 11:28 AM
SLC 5/03 data loss andydaoust LIVE PLC Questions And Answers 1 October 10th, 2002 06:38 PM


All times are GMT -5. The time now is 10:29 PM.


.