ST program bit toggle problem

Puntigamer

Member
Join Date
Jan 2020
Location
Graz
Posts
3
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?
 
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:

On_Off.png
 
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:
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.
 
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.
 
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.
 
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!)
 
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
 
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
 
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.
 
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 [B]xor onp[/B](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.
 
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).
 

Similar Topics

Replies
15
Views
3,468
Hi All, I would like to get some data like Run / Program bit, CPU time, Scan times etc. to my DCS system, could anybody please point me to a...
Replies
2
Views
1,158
Hey everyone, I'm currently in my last year of high school and I have to automate a scorebord for a foosball table using 4 optical sensors (sender...
Replies
38
Views
6,194
OK, so I know that this question has probably been asked many times but....is there a way to convert an old PV PLUS program (32 bit) to my new 64...
Replies
7
Views
2,414
Hi I've been programming in RSLogix 500 Micro Starter edition and its come time to download my program to my Micrologix 1200. I'm using a Win7...
Replies
9
Views
13,536
Back
Top Bottom