Guides

Prompting Tips

How to write effective prompts for better AI results

Prompting Tips

The quality of AI-generated code depends heavily on how you phrase your requests. This guide will help you write prompts that get better results.

Core Principles

1. Be Specific

The more specific your request, the better the result.

❌ "Create a timer"

✅ "Create a TON timer called tonDelay with PT of 5 seconds
    that starts when bStart is TRUE"

2. Include Data Types

Always specify the data types you want:

❌ "Add a temperature variable"

✅ "Add a REAL variable called rTemperature with range -40.0 to 200.0"

3. Describe the Behavior

Explain what should happen, not just what you want:

❌ "Add motor control"

✅ "Add motor control where:
    - bStart rising edge starts the motor
    - bStop sets motor to off
    - Motor can only start if bReady is TRUE"

Prompt Templates

Creating a Function Block

Create a function block called [NAME] that:
- Inputs: [list inputs with types]
- Outputs: [list outputs with types]
- Internal variables: [list any needed]
- Behavior: [describe the logic]

Example:

Create a function block called FB_ValveControl that:
- Inputs: bOpen (BOOL), bClose (BOOL), tTimeout (TIME)
- Outputs: bIsOpen (BOOL), bIsClosed (BOOL), bError (BOOL)
- Behavior:
  - bOpen opens the valve, bClose closes it
  - If neither open nor closed after tTimeout, set bError TRUE
  - bOpen and bClose should not both be TRUE

Adding Features

Add [FEATURE] to [EXISTING CODE]:
- [specific requirement 1]
- [specific requirement 2]
- Keep existing functionality

Example:

Add speed ramping to FB_Motor:
- New input rTargetSpeed (REAL)
- Ramp rate of 10% per second
- Output rActualSpeed follows target with ramping
- Keep existing start/stop logic

Implementing Algorithms

Implement [ALGORITHM NAME]:
- Input parameters: [list]
- Output: [describe]
- Requirements: [list any specific requirements]

Example:

Implement a moving average filter:
- Input: rNewValue (REAL)
- Output: rFilteredValue (REAL)
- Window size: 10 samples
- Should handle initialization gracefully

Debugging Help

I'm getting this error: [ERROR MESSAGE]
In this code: [PASTE CODE]
What's wrong and how do I fix it?

Advanced Techniques

Reference Existing Code

The AI knows your project context. Use it:

"Create FB_Pump similar to FB_Motor but for pump control"

"Use the same error handling pattern as FB_Conveyor"

"Add the same logging as we have in FB_Alarm"

Provide Examples

Show the AI what you want:

Create a state machine like this pattern:

CASE nState OF
    0: (* Idle *)
        IF bStart THEN nState := 10; END_IF
    10: (* Running *)
        IF bStop THEN nState := 0; END_IF
END_CASE

But for a 5-step wash cycle process.

Ask for Alternatives

"Show me two different ways to implement this debounce logic"

"What's a better way to structure this state machine?"

Request Explanations

"Explain why you chose TON instead of TP for this timer"

"What does this line do: bEdge := bInput AND NOT bPrev;"

Common Patterns

State Machines

Create a state machine for [PROCESS]:
- States: [list states]
- Transitions: [describe when to move between states]
- Actions: [what happens in each state]

Error Handling

Add error handling:
- Error conditions: [list what triggers errors]
- Error codes: [list codes and meanings]
- Recovery: [how to clear errors]

Timing Operations

Add timing for [OPERATION]:
- Delay before: [time]
- Timeout after: [time]
- What happens on timeout: [behavior]

What to Avoid

Don't Be Vague

❌ "Make it better"
❌ "Add some error handling"
❌ "Fix this"

Don't Assume Context

❌ "Add a variable for that thing we discussed"
✅ "Add a REAL variable rFlowRate for the pump flow measurement"

Don't Ask for Too Much at Once

Break large requests into smaller steps:

❌ "Create a complete HVAC control system"

✅ Step 1: "Create FB_TemperatureControl for heating/cooling"
✅ Step 2: "Create FB_FanControl for ventilation"
✅ Step 3: "Create FB_HVACMain that coordinates temperature and fan control"

Iteration is Key

Don't expect perfection on the first try. It's normal to:

  1. Make an initial request
  2. Review the result
  3. Ask for modifications
  4. Repeat until satisfied
"Change the timer from 5 seconds to 10 seconds"
"Rename bError to bFault for consistency"
"Add a comment explaining the state machine"

Next Steps

On this page