Skip to content

Standard Blocks

Coldwave Script includes a set of ready-made blocks that simplify common control tasks significantly. Instead of implementing counters or timers manually every time, you can rely on these proven components. All blocks have inputs (IN, PT, CU, …) and outputs (Q, CV, ET, …) and are instantiated like variables.

TON – On‑Delay Timer

Inputs:

  • IN (BOOL): start signal
  • PT (TIME): preset duration

Outputs:

  • Q (BOOL): TRUE when IN has been active for at least PT
  • ET (TIME): elapsed time

Description: TON activates Q only when IN remains TRUE continuously for PT. ET exposes elapsed time.

vb
PROGRAM  
    DIM T AS TON  
    T(IN = TRUE, PT = T#2000ms)  
    PRINT T.Q, T.ET  
END PROGRAM

TOF – Off‑Delay Timer

Inputs:

  • IN (BOOL): input signal
  • PT (TIME): delay duration

Outputs:

  • Q (BOOL): TRUE while IN=TRUE or delay time has not elapsed
  • ET (TIME): elapsed time since IN turned FALSE

Description: TOF keeps Q TRUE for PT after IN falls to FALSE.

vb
PROGRAM  
    DIM T AS TOF  
    T(IN = TRUE, PT = T#3000ms)  
    PRINT T.Q, T.ET  
END PROGRAM

TP – Pulse Timer

Inputs:

  • IN (BOOL): trigger signal
  • PT (TIME): pulse length

Outputs:

  • Q (BOOL): TRUE for exactly PT after a rising edge on IN

Description: TP produces a pulse of length PT on a rising edge, regardless of subsequent IN changes.

vb
PROGRAM  
    DIM T AS TP  
    T(IN = TRUE, PT = T#1000ms)  
    PRINT T.Q  
END PROGRAM

CTU – Up Counter

Inputs:

  • CU (BOOL): count‑up trigger (rising edge increments)
  • PV (INT): preset value

Outputs:

  • Q (BOOL): TRUE when CV ≥ PV
  • CV (INT): current counter value

Description: CTU increments CV on each rising edge of CU. When CV ≥ PV, Q becomes TRUE.

vb
PROGRAM  
    ALIAS %I0 AS pulse ' CTU needs a rising edge as pulse
    DIM C AS CTU  
    C(CU = pulse, PV = 3)  
    PRINT C.CV, C.Q  
END PROGRAM

CTD – Down Counter

Inputs:

  • CD (BOOL): count‑down trigger (rising edge decrements)
  • PV (INT): start value

Outputs:

  • Q (BOOL): TRUE while CV > 0
  • CV (INT): current counter value

Description: CTD starts at PV and decrements CV per edge. Q becomes FALSE when CV reaches 0.

vb
PROGRAM  
    ALIAS %I0 AS pulse ' CTU needs a rising edge as pulse
    DIM C AS CTD  
    C(CD = pulse, PV = 5)  
    PRINT C.CV, C.Q  
END PROGRAM

CTUD – Up/Down Counter

Inputs:

  • CU (BOOL): increment trigger
  • CD (BOOL): decrement trigger
  • PV (INT): preset value

Outputs:

  • Q (BOOL): TRUE when CV ≥ PV
  • CV (INT): current counter value

Description: CTUD combines CTU and CTD. Useful for bidirectional counts (e.g., encoders).

vb
PROGRAM  
    ALIAS %I0 AS pulse ' CTUD needs an edge as pulse
    DIM C AS CTUD   
    C(CU = TRUE, CD = FALSE, PV = 10)  
    PRINT C.CV, C.Q  
END PROGRAM

R_TRIG – Rising Edge Detector

Inputs:

  • CLK (BOOL)

Outputs:

  • Q (BOOL): TRUE for exactly one cycle when CLK rises FALSE→TRUE

Description: Detects rising edges.

vb
PROGRAM  
    DIM F AS R_TRIG  
    DIM clk AS BOOL = TRUE ' this needs to come from a real input that creates edges
    F(CLK = clk)  
    IF F.Q THEN  
        PRINT "Rising edge detected"  
    END IF  
END PROGRAM

F_TRIG – Falling Edge Detector

Analogous to R_TRIG, but detects TRUE→FALSE transitions.

vb
PROGRAM  
    DIM F AS F_TRIG  
    DIM clk AS BOOL = FALSE ' this needs to come from a real input that creates edges 
    F(CLK = clk)  
    IF F.Q THEN  
        PRINT "Falling edge detected"  
    END IF  
END PROGRAM

Examples

Blinking using TON:

vb
PROGRAM  
    DIM T AS TON  
    DIM state AS BOOL = FALSE  
    IF NOT T.Q THEN  
        T(IN = TRUE, PT = T#500ms)  
    END IF  
    IF T.Q THEN  
        state = NOT state  
        T(IN = FALSE, PT = T#500ms)  
    END IF  
    PRINT state  
END PROGRAM

Delayed shutdown using TOF:

vb
PROGRAM  
    DIM T AS TOF  
    DIM light AS BOOL = TRUE  
    T(IN = light, PT = T#2000ms)  
    PRINT T.Q  
END PROGRAM

Counting pulses with CTU:

vb
PROGRAM  
    DIM C AS CTU  
    DIM pulse AS BOOL = TRUE ' this needs to come from a real input that creates edges 
    C(CU = pulse, PV = 5)  
    PRINT "Count=", C.CV, " Done=", C.Q  
END PROGRAM

Edge‑triggered counting (R_TRIG + CTU):

vb
PROGRAM  
    DIM F AS R_TRIG  
    DIM C AS CTU  
    DIM clk AS BOOL = TRUE  ' this needs to come from a real input that creates edges
    F(CLK = clk)  
    IF F.Q THEN  
        C(CU = TRUE, PV = 3)  
    END IF  
    PRINT C.CV, C.Q  
END PROGRAM

Exercises with Solutions

Task 1: Use TON to enable an output after 2 seconds.

vb
PROGRAM  
    DIM T AS TON  
    ALIAS %I0 AS trig  ' Trigger-Input
    T(IN = trig, PT = T#2000ms)  
    PRINT T.Q  
END PROGRAM

Task 2: Count 5 pulses and print "Done" when finished.

vb
PROGRAM  
    DIM C AS CTU  
    ALIAS %I0 AS trig  ' Trigger-Input
    C(CU = trig, PV = 5)  
    PRINT C.CV, C.Q  
END PROGRAM

Task 3: Detect rising edge and output a pulse.

vb
PROGRAM  
    DIM F AS R_TRIG  
    DIM out AS BOOL = FALSE  
    DIM clk AS BOOL = TRUE  ' this needs to come from a real input that creates edges  
    F(CLK = clk)  
    out = F.Q  
    PRINT out  
END PROGRAM

Summary

  • Timers (TON, TOF, TP) control time behavior.
  • Counters (CTU, CTD, CTUD) count events.
  • Edge detection (R_TRIG, F_TRIG) detects signal transitions.
  • All blocks are stateful and must be instantiated.