Sequencing one light with one push button S7

Derman0524

Member
Join Date
Dec 2018
Location
Toronto
Posts
22
Hi there,

I'm having trouble with a task I was given on Siemens S7. I have to sequence one light with one PB.

You press the button once, the light flashes once
You press the button again, the light flashes 2 times
you press the button again, the light flashes 3 times
You press the button again, the sequence stops

I had this question during an interview last year and I wanted to revisit the problem. The interviewer said it was an easy very easy question and can easily be done, any suggestions? I used a flip flop for pressing it once but I'm not sure how to continue. Any helps would be greatly appreciated.

Thanks!
 
Hi there,

I'm having trouble with a task I was given on Siemens S7. I have to sequence one light with one PB.

You press the button once, the light flashes once
You press the button again, the light flashes 2 times
you press the button again, the light flashes 3 times
You press the button again, the sequence stops

I had this question during an interview last year and I wanted to revisit the problem. The interviewer said it was an easy very easy question and can easily be done, any suggestions? I used a flip flop for pressing it once but I'm not sure how to continue. Any helps would be greatly appreciated.

Thanks!

You might want to look at state machines/sequences. There are a ton of posts on this forum discussing the concepts.

You can do it in LAD, but something like Graph/SFC might make it easier.
 
counter is way to go.

initial = 0

first press of button -> 1, blink lamp one time only
2nd press -> 2
3rd -> 3
4th -> 0


You need add only blinker and enale blink to certain time to this.

Blinking needs 5, 3 or only 1 timer depending how you program
 
Last edited:
A counter is one option. Then you could use the accumulator in the counter to flash the light.

Personally, I would go with a State-machine / Grafcet and write a sequence.

The other 'gotcha' that I would add to this statement is to make sure the button input itself is on a timer and use that timer done bit to trigger the steps (debounce circuit) Otherwise, if you held the button in for too long it would jump to the next state.
 
Can anyone provide some quick LAD examples? I'm not sure what a state-machine is as well I'm quite new to PLC programming so I apologize in advance and thanks for the hasty replies!
 
Search for "state machine" either in this forum or in a more general context out there on the internet. Plenty of basic descriptions to be found.



The general consensus around here seems to be we do not provide sample code without a bit more elbow grease from your side. Us providing you with sample code is not nearly as instructive as you trying to find your way through a typical example of homework assignment like this by yourself. If you get stuck at some point, then show what you got at that point, explain your reasoning and see if we can give a nudge in some direction to get you moving forward again.


The problem at hand can be done in many different ways, e.g. straight forward ladder code, slightly more advanced ladder code with for instance sequencers or some of your own home grown function blocks, you could use a state machine in either Sequential Function Chart, ladder, ST or whatever else floats your boat. I'm sure there must be plenty more paths.


If you have no idea how to start on this, then park this one aside for a little while. Do some more basic exercises with timers, counters. Some latching, debouncing, triggers may help, too. Read up on state machines if you feel so inclined as the concept is a very useful tool in any programmer's toolbox.
 
Every time I add an SFB timer, my flip flop stops working entirely and that's where I'm getting sort of lost. I want to make this work with timers and counters, including the flip flop but the flip flop doesn't like the flip flop incorporated.

PLC Flip Flop.png
 
I'm not familiar with that programming environment, but it seems to me:

1. Your use of the oneshot and latching to create a flipflop seems correct to me. Nice, that's one down.

2. You use mem2 for the ENABLED bit of your SFBTIME block. I guess this input should just always remain TRUE for your case. Use mem2 for the IN bit instead.

3. You did not enter anything for the PT input. This is the Preset Time value for your SFBTIME. It needs not a logic bit but a time value. The way this is written may vary between programming environments. Could look something like T#3s for 3 seconds or T#50ms for 50 msec. Someone else may know your software and correct me with the correct syntax when necessary.

4. The SFBTIME has an output bit Q. Following your naming scheme, my guess is that you'd want to assign Q to "mem3".
 
Last edited:
I added in the SFB timer quick to show you guys, I understand how it works and how it starts to time but I'm not sure why it breaks the flip flop. I tried with mem2 going into the IN of the SFB but I'm at a loss.
 
Show more then, so we can see what you do set for inputs and outputs. For now I have no idea why it would get stuck at that point if you set things correctly.


I'm in a different time zone from you and off to bed now so I can make a good fresh start tomorrow at 5:45am. I was in Toronto last summer however :)
 
I've attached a basic ladder.

I think of a state machine as a sequence. I use MOV statements to move a number into the sequence memory location.

The most important part is the first rung. I use a 'TO' number and an 'AT' number. The 'AT' is obviously what state you are at. the 'TO' number is what is used in the rungs and tells the machine what state to move to on the next scan. The first instruction puts the 'TO' number into that 'AT' register.

The 'AT' number is used on the left logic for checking each rung statement. The 'TO' number is used on the right and becomes true if the rung is energized.

This ensures that you complete a scan cycle on the PLC and you don't skip over steps.


Then, I use the number in the AT register to compare. If the number is in between, equal to, greater than, less than, etc that state number, do something.

For instance, at State 30, I want the cylinder to work output to turn on.

STATE MACHINE.PNG
 
Numbers 0, 10, 20, 30 is used so that it is easier to add new states later. You don't need to shift all numbers. You can use also 0, 100, 200 or 0, 2000 or 3000 etc.
It is also to useful to limit states only to these numbers on logic, so that state can't jam, if state number is somehow changed to wrong.
 
Last edited:
Here's the source code of an example function block that you pass the number of flashes required. You could call this from your higher level sequencer.


Code:
FUNCTION_BLOCK FB 3
TITLE =
VERSION : 0.1


VAR_INPUT
  iFlashCount : INT ;    
  bStart : BOOL ;    
END_VAR
VAR_IN_OUT
  bFinished : BOOL ;    
  bFlasher : BOOL ;    
END_VAR
VAR
  sfbTimer : "TON";    
  bTimerQ : BOOL ;    
  iCountSoFar : INT ;    
  bRunning : BOOL ;    
  bStartEdgeStore : BOOL ;    
  bFlashOn : BOOL ;    
  bFlashOff : BOOL ;    
END_VAR
BEGIN
NETWORK
TITLE =flasher one shot

      AN    #bTimerQ; 
      =     L      0.0; 
      BLD   103; 
      CALL #sfbTimer (
           IN                       := L      0.0,
           PT                       := T#500MS,
           Q                        := #bTimerQ);

      NOP   0; 
NETWORK
TITLE =start seq running

      A     #bStart; 
      FP    #bStartEdgeStore; 
      S     #bRunning; 
      R     #bFinished; 

NETWORK
TITLE =sync start flash to timer

      A     #sfbTimer.Q; 
      A     #bRunning; 
      AN    #bFlashOn; 
      AN    #bFlashOff; 
      =     L      0.0; 
      A     L      0.0; 
      BLD   102; 
      S     #bFlashOn; 
      A     L      0.0; 
      BLD   102; 
      R     #sfbTimer.Q; 
      A     L      0.0; 
      BLD   102; 
      S     #bFlasher; 
      A     L      0.0; 
      JNB   _001; 
      L     #iFlashCount; 
      T     #iCountSoFar; 
_001: NOP   0; 
NETWORK
TITLE =flash off 

      A     #sfbTimer.Q; 
      A     #bFlashOn; 
      R     #bFlashOn; 
      S     #bFlashOff; 
      R     #sfbTimer.Q; 
      R     #bFlasher; 
NETWORK
TITLE =finish or flash on again and loop

      A     #sfbTimer.Q; 
      A     #bFlashOff; 
      JNB   _002; 
      L     #iCountSoFar; 
      L     1; 
      -I    ; 
      T     #iCountSoFar; 
      AN    OV; 
      SAVE  ; 
      CLR   ; 
_002: A     BR; 
      =     L      0.0; 
      A     L      0.0; 
      BLD   102; 
      R     #bFlashOff; 
      A     L      0.0; 
      A(    ; 
      L     #iCountSoFar; 
      L     0; 
      ==I   ; 
      )     ; 
      S     #bFinished; 
      R     #bRunning; 
      A     L      0.0; 
      A(    ; 
      L     #iCountSoFar; 
      L     0; 
      <>I   ; 
      )     ; 
      S     #bFlasher; 
      S     #bFlashOn; 
END_FUNCTION_BLOCK
 
Example sequencer using the previously posted flash counter:


Code:
FUNCTION_BLOCK FB 4
TITLE =
VERSION : 0.1


VAR_INPUT
  bStartPB : BOOL ;    
END_VAR
VAR_OUTPUT
  bFlasherQ : BOOL ;    
END_VAR
VAR
  bStartEdgeStore : BOOL ;    
  bRunning : BOOL ;    
  iFlashCount : INT ;    
  Step : INT ;    
  bRequestFlash : BOOL ;    
  fbFlashRequst : FB 3;    
  bFlasher : BOOL ;    
  bFinished : BOOL ;    
END_VAR
VAR_TEMP
  bStartOneShot : BOOL ;    
END_VAR
BEGIN
NETWORK
TITLE =detect button press and generate one shot

      A     #bStartPB; 
      FP    #bStartEdgeStore; 
      =     #bStartOneShot; 
NETWORK
TITLE =first press - flash once

      A(    ; 
      A     #bStartOneShot; 
      A(    ; 
      L     #Step; 
      L     0; 
      ==I   ; 
      )     ; 
      JNB   _001; 
      L     10; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_001: A     BR; 
      )     ; 
      JNB   _002; 
      L     1; 
      T     #iFlashCount; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_002: A     BR; 
      S     #bRequestFlash; 
      R     #bFinished; 
NETWORK
TITLE =wait for flashing to finish

      A(    ; 
      L     #Step; 
      L     10; 
      ==I   ; 
      )     ; 
      A     #bFinished; 
      JNB   _003; 
      L     20; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_003: A     BR; 
      R     #bRequestFlash; 
NETWORK
TITLE =2nd press flash twice

      A(    ; 
      A     #bStartOneShot; 
      A(    ; 
      L     #Step; 
      L     20; 
      ==I   ; 
      )     ; 
      JNB   _004; 
      L     30; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_004: A     BR; 
      )     ; 
      JNB   _005; 
      L     2; 
      T     #iFlashCount; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_005: A     BR; 
      S     #bRequestFlash; 
      R     #bFinished; 
NETWORK
TITLE =wait for flashing to finish

      A(    ; 
      L     #Step; 
      L     30; 
      ==I   ; 
      )     ; 
      A     #bFinished; 
      JNB   _006; 
      L     40; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_006: A     BR; 
      R     #bRequestFlash; 
NETWORK
TITLE =3rd press flash thrice

      A(    ; 
      A     #bStartOneShot; 
      A(    ; 
      L     #Step; 
      L     40; 
      ==I   ; 
      )     ; 
      JNB   _007; 
      L     50; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_007: A     BR; 
      )     ; 
      JNB   _008; 
      L     3; 
      T     #iFlashCount; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_008: A     BR; 
      S     #bRequestFlash; 
      R     #bFinished; 
NETWORK
TITLE =wait for flashing to finish

      A(    ; 
      L     #Step; 
      L     50; 
      ==I   ; 
      )     ; 
      A     #bFinished; 
      JNB   _009; 
      L     60; 
      T     #Step; 
      SET   ; 
      SAVE  ; 
      CLR   ; 
_009: A     BR; 
      R     #bRequestFlash; 
NETWORK
TITLE =4th press reset seq ready for 1st press again

      A     #bStartOneShot; 
      A(    ; 
      L     #Step; 
      L     60; 
      ==I   ; 
      )     ; 
      JNB   _00a; 
      L     0; 
      T     #Step; 
_00a: NOP   0; 
NETWORK
TITLE =flash counter

      A     #bRequestFlash; 
      =     L      1.0; 
      BLD   103; 
      CALL #fbFlashRequst (
           iFlashCount              := #iFlashCount,
           bStart                   := L      1.0,
           bFinished                := #bFinished,
           bFlasher                 := #bFlasher);
      NOP   0; 
NETWORK
TITLE =flash to output Q

      A     #bFlasher; 
      =     #bFlasherQ; 
END_FUNCTION_BLOCK
 

Similar Topics

I need to make a sequence of lights flash on and off in automation studios using AB PLC. I have to use timers and counters. The required sequence...
Replies
51
Views
13,476
Does anyone have sample logic with conveyors that start and stop according to product on the line?
Replies
1
Views
90
Hello, I am struggling to figure out some logic. I have an analog pressure sensor coming into the PLC and would like to have multiple timing...
Replies
4
Views
143
One of my customers builds equipment with a MicroLogix 1100 using a program I wrote some years ago. Now he wants to transition to a Micro 820...
Replies
1
Views
1,850
Well Gentlemen, once again, I am trying to figure out how to do something in Logix, and I cannot seem to get it to work like all the YouTube...
Replies
13
Views
2,928
Back
Top Bottom