During the first cycle, the IF bFirstScan condition evaluates to true. The initialization code executes. bFirstScan is set to FALSE at the end of the block.
IF fbFirstScan.bFirstScan THEN // First cycle only bResetDrives := TRUE; bStartupComplete := FALSE; tStartupTimer(IN:=FALSE); END_IF beckhoff first scan bit
// 3. Request axes to home (if needed) fbAxis1.bHomeRequest := TRUE; During the first cycle, the IF bFirstScan condition
PROGRAM MAIN VAR bFirstScan : BOOL := TRUE; // Initialize as TRUE bInitialized : BOOL; END_VAR // --- Logic --- IF bFirstScan THEN // Place all your initialization code here iSpeed := 100; bInitializeDone := TRUE; bFirstScan := FALSE; // Turn off after first run END_IF; // ... Rest of your program Use code with caution. Method B: Using PLC Attributes (TwinCAT 3 Best Practice) IF fbFirstScan
This method is robust and directly ties the initialization code to the lifecycle of the PLC task itself.
A simpler, more manual approach uses a boolean variable to track if the first scan has already occurred. This method is particularly useful for quick prototyping or simple programs.
🔄 Forces your Sequential Function Chart (SFC) or CASE statements to jump to the 'IDLE' or 'INIT' state regardless of where they were during the last shutdown.
Browse through our collection of Knowledge Base articles.