Coding and Logic for Microcontroller Control - StudyPulse
Boost Your VCE Scores Today with StudyPulse
8000+ Questions AI Tutor Help
Home Subjects Systems Engineering Coding for control

Coding and Logic for Microcontroller Control

Systems Engineering
StudyPulse

Coding and Logic for Microcontroller Control

Systems Engineering
01 May 2026

Coding and Logic Statements to Control Systems via Microcontrollers

Overview

In VCE Systems Engineering Unit 4, students extend their understanding of microcontroller programming to implement control systems — programs that continuously monitor sensor inputs and adjust actuator outputs to achieve desired system behaviour. This requires structured use of coding constructs (sequence, selection, iteration) and logic statements (Boolean conditions) within a control loop.

KEY TAKEAWAY: A microcontroller control program is a continuous loop that reads sensors, evaluates logic conditions, makes decisions, and drives actuators. The logic statements are the intelligence that determines how the system responds to real-world conditions.

The Control Loop Structure

Every microcontroller control program has this fundamental structure:

SETUP:
    configure inputs and outputs
    set initial values

MAIN LOOP (runs continuously):
    1. READ sensors
    2. CALCULATE or COMPARE values
    3. DECIDE on actions (logic statements)
    4. WRITE to actuators
    5. (optional) WAIT a fixed interval
    REPEAT

This is a closed-loop structure in software — the program continuously cycles through read-decide-act.

Logic Statements

IF / ELSE IF / ELSE

The fundamental decision structure:

IF condition THEN
    action_A
ELSE IF other_condition THEN
    action_B
ELSE
    action_C
END IF

Example — three-zone temperature control:

IF temperature > 35 THEN
    set fan to HIGH speed
    turn heater OFF
ELSE IF temperature > 28 THEN
    set fan to LOW speed
    turn heater OFF
ELSE IF temperature < 20 THEN
    turn heater ON
    set fan to OFF
ELSE
    turn heater OFF
    set fan to OFF
END IF

Boolean Operators in Conditions

Conditions can use AND, OR, NOT to express compound logic:

IF (temperature > 30) AND (humidity > 80) THEN
    activate dehumidifier AND cooling fan
END IF

IF (light_level < 200) OR (time_of_day > 18:00) THEN
    turn on lights
END IF

IF NOT (door_closed) THEN
    sound alarm
END IF

VCAA FOCUS: VCAA questions often provide a written description of system behaviour and ask students to write pseudocode or a flowchart. Identify the conditions (sensor thresholds) and the corresponding actions (actuator states) to construct the logic.

Nested Logic

Conditions can be nested for complex multi-condition decisions:

IF mode == AUTO THEN
    IF temperature > setpoint THEN
        fan = ON
    ELSE
        fan = OFF
    END IF
ELSE
    fan = manual_setting
END IF

Variables and Thresholds

Good control programs use named variables for setpoints and thresholds rather than hard-coded numbers. This makes the program readable and adjustable:

CONST temp_high = 30      // °C  fan activates above this
CONST temp_low  = 25      // °C  fan deactivates below this (deadband)
CONST fan_pin   = 3       // Digital output pin

LOOP:
    temp = readThermistor(A0)
    IF temp > temp_high THEN
        digitalWrite(fan_pin, HIGH)
    ELSE IF temp < temp_low THEN
        digitalWrite(fan_pin, LOW)
    END IF
END LOOP

The deadband (temp_low to temp_high) prevents rapid switching (hunting) around the setpoint.

Counting and Accumulation

Some control tasks require counting events or accumulating values:

count = 0

LOOP:
    IF sensorPin == HIGH THEN
        count = count + 1
        wait until sensorPin goes LOW    // prevent double-counting
    END IF

    IF count >= 10 THEN
        trigger_alarm()
        count = 0
    END IF
END LOOP

Application: Counting products on a conveyor, counting revolutions for speed measurement.

Timing in Control Programs

Time delays and timed actions are implemented using counters or built-in timer functions:

// Example: Keep motor on for 5 seconds after button press
IF button_pressed THEN
    motor = ON
    wait(5000 ms)
    motor = OFF
END IF

Polling vs. interrupts: A simple program checks the sensor every loop iteration (polling). More sophisticated programs use hardware interrupts that respond to a sensor event immediately, regardless of what else the program is doing.

State Machine Programming

For systems with multiple distinct modes of operation, a state machine approach is cleaner than complex nested IF statements:

States: IDLE, STARTING, RUNNING, STOPPING, FAULT

state = IDLE

LOOP:
    IF state == IDLE THEN
        IF start_button THEN state = STARTING

    ELSE IF state == STARTING THEN
        motor = ON
        timer = 0
        state = RUNNING

    ELSE IF state == RUNNING THEN
        timer = timer + 1
        IF timer > max_time THEN state = STOPPING
        IF fault_detected THEN state = FAULT

    ELSE IF state == STOPPING THEN
        motor = OFF
        state = IDLE

    ELSE IF state == FAULT THEN
        motor = OFF
        alarm = ON
    END IF
END LOOP

APPLICATION: State machine programming is used in real industrial systems, washing machines, traffic light controllers, and robotic systems. Understanding the concept — even if not required to implement it fully — demonstrates sophisticated engineering thinking in VCAA extended responses.

Common Coding Patterns in Control Systems

Pattern Pseudocode structure Application
Threshold trigger IF value > threshold THEN action Temperature alarm, overspeed cut-out
Deadband IF value > high THEN … IF value < low THEN … Thermostat, hysteresis
Proportional output output = Kp × (setpoint - measured) Speed control, position control
Timed action action ON; wait(T); action OFF Timed relay, pulse output
Count accumulation count++; IF count >= N THEN … Batch counter, pulse counting
State machine IF state == X THEN … Multi-mode operation

STUDY HINT: Practise translating written system descriptions into pseudocode and flowcharts. The key step is identifying: (1) what is measured (sensor variable), (2) the decision condition (threshold, comparison), and (3) the action taken (actuator state). Every control program reduces to this three-part structure.

Flowchart to Pseudocode Translation

Flowchart description: System reads moisture sensor. If moisture < 300 (dry), turn on pump. If moisture ≥ 300 (wet), turn off pump. Repeat every 10 seconds.

Pseudocode:

SETUP:
    pumpPin = OUTPUT
    sensorPin = ANALOGUE INPUT

LOOP:
    moisture = analogRead(sensorPin)

    IF moisture < 300 THEN
        digitalWrite(pumpPin, HIGH)   // pump ON
    ELSE
        digitalWrite(pumpPin, LOW)    // pump OFF
    END IF

    wait(10000 ms)
END LOOP

COMMON MISTAKE: Forgetting to turn the actuator OFF in the ELSE branch. If the condition is no longer met, the actuator must be explicitly deactivated — it does not turn off automatically just because the IF condition is false. Always include both ON and OFF states in the logic.

Table of Contents