Back to homeTechnology · Secondary Year 3 · Change subject
Start for free
Secondary 3 · Technology
Computational thinking and robotics · Robotics

Programming Controller Boards

A traffic light on a corner in Guadalajara changes color without anyone moving it. Who tells it what to do and in what order? A controller board runs exactly the instructions in its program, whether they are correct or not: it doesn't guess intentions. That's why it pays to think through the algorithm before programming. At the end of the page we'll come back to the traffic light.

Algorithm and program

An algorithm can be described with pseudocode (instructions written in words, in order), with graphical blocks (pieces that snap together), or with a flowchart. The form changes, not the logic.

One algorithm, the cycle of a traffic light (green, amber, red, and repeat), is expressed as pseudocode, as blocks, or as a flowchart, and is translated into a program that a controller board with three LEDs runs. algorithm repeat pseudocode 1repeat forever 2green; wait 3amber; wait 4red; wait blocks repeat forever turn on wait turn on wait turn on wait flowchart green, wait amber, wait red, wait program the same logic is expressed as text, blocks, or a flowchart
Practice★☆☆Not started
Explain what an algorithm is and what a program is, and what difference there is between describing it in pseudocode (using words) and with visual blocks.

Explain what an algorithm is and what a program is, and what difference there is between describing it in pseudocode (using words) and with visual blocks.

Solve
Practice★☆☆Not started
The program instructions for a set of traffic lights are out of order: (a) wait 2 seconds, (b) turn on the red light and turn off the others, (c) turn on the green light…

The program instructions for a set of traffic lights are out of order: (a) wait 2 seconds, (b) turn on the red light and turn off the others, (c) turn on the green light and turn off the others, (d) return to the start, (e) wait 8 seconds (appears twice in the cycle), (f) turn on the amber light and turn off the others. Put them in the correct order and describe what the complete program does.

Solve

Inputs and conditionals: combining with AND, OR, NOT

Examples of conditions: door == OPEN (digital) or temperature > 40 (analog, with a threshold). Try it with the logic gate: change the inputs and watch the output.

Practice★☆☆Not started
Explain what "reading an input" means in a control program and what difference there is between reading a push button (which gives 0 or 1) and reading a light sensor…

Explain what "reading an input" means in a control program and what difference there is between reading a push button (which gives 0 or 1) and reading a light sensor (which gives a number within a range).

Solve
Practice★☆☆Not started
A program consists of only these lines: "repeat forever: read the push button; if pressed, turn on LED; else, turn off LED". Describe in your own words what the setup…

A program consists of only these lines: "repeat forever: read the push button; if pressed, turn on LED; else, turn off LED". Describe in your own words what the setup does and reason what would happen if the "else, turn off LED" part were deleted.

Solve
Practice★☆☆Not started
Explain what a conditional statement ("if... then... else...") is and write the condition needed for these two warnings: an alarm that sounds when a door opens and an…

Explain what a conditional statement ("if... then... else...") is and write the condition needed for these two warnings: an alarm that sounds when a door opens and an alert that beeps when the temperature exceeds $40\ ^\circ\text{C}$.

Solve
Practice★★☆Not started
A parking lot barrier should only raise when a car is waiting (sensor) and the driver has also taken the ticket (push button); it lowers once the car has passed (a…

A parking lot barrier should only raise when a car is waiting (sensor) and the driver has also taken the ticket (push button); it lowers once the car has passed (a second sensor). Write the complete pseudocode for the cycle, combining both conditions at once.

Solve
Practice★★☆Not started
A home alarm, upon detecting an open door, waits 10 seconds before sounding to allow time to disarm it using a passcode. Write its pseudocode including the passcode…

A home alarm, upon detecting an open door, waits 10 seconds before sounding to allow time to disarm it using a passcode. Write its pseudocode including the passcode check and explain the purpose of this delay.

Solve

Remembering an event with a variable

An if only sees the present moment. If an alarm must keep sounding even after the intruder closes the window, the program has to remember that something happened, storing it in a variable:

repetir siempre:

    si armada Y (puerta == ABIERTA O ventana == ABIERTA):

        disparada = VERDADERO

    si disparada:

        sirena ON

    si codigo_correcto:

        disparada = FALSO

        sirena OFF

The variable disparada (triggered) is the system's memory: it is set by the event and is only cleared by a deliberate action (the code), not when the sensor stops detecting.

Loops and waits

Almost every board program has a loop because the system must keep monitoring while it is on. Without a loop, a traffic light would run its sequence just once and stay stuck on the last light.

The wait gives duration to the states: without it, green, amber, and red would follow one another in microseconds and the eye would see all three lights at once; a stairway light would go off the moment it came on.

A long wait has a cost: during it the system is "blind." Look at the figure.

Two timelines: with a long 30-second wait, a button pressed in the middle of the wait isn't read until the end; with short pauses there is a button reading in each pause and it is detected almost immediately. long wait wait(30): doesn't read the button button pressed: it doesn't notice until the end short pauses button pressed: read in the next pause one button reading in each pause
Practice★☆☆Not started
What is the purpose of the "wait" command (a delay) in a control program? Give two examples in which, without that delay, the system would not perform its function.

What is the purpose of the "wait" command (a delay) in a control program? Give two examples in which, without that delay, the system would not perform its function.

Solve
Practice★☆☆Not started
Explain why almost all programs for controller boards contain a loop ("repeat forever") and describe what a set of traffic lights would do if its program did not have a…

Explain why almost all programs for controller boards contain a loop ("repeat forever") and describe what a set of traffic lights would do if its program did not have a loop.

Solve
Practice★★☆Not started
Write in pseudocode the complete program for a simple set of traffic lights: green for 8 seconds, amber for 2, red for 8, and repeat. Indicate at each step which lights…

Write in pseudocode the complete program for a simple set of traffic lights: green for 8 seconds, amber for 2, red for 8, and repeat. Indicate at each step which lights are on and which are off.

Solve
Practice★★☆Not started
In a pedestrian light system, the program reads the push button only once during each 30-second cycle. Explain what the pedestrian experiences upon pressing it and…

In a pedestrian light system, the program reads the push button only once during each 30-second cycle. Explain what the pedestrian experiences upon pressing it and reason why long delays within the program affect the response speed of the system.

Solve
Practice★★★Not started
A student programs the lighting for a communal staircase: when the switch is pressed, the light must stay on for 30 s and then turn off. a) If the student implements the…

A student programs the lighting for a communal staircase: when the switch is pressed, the light must stay on for 30 s and then turn off.

  1. a) If the student implements the delay with a blocking function such as wait(30), predict what will happen if a second neighbor presses the switch 15 s after the light has turned on. Does this behavior fulfill the function of a resettable staircase timer? Justify your answer.
  2. b) Design in pseudocode a non-blocking resettable system using a time_remaining variable and periodic checks (for example, every 1.0 s), so that each new press resets the countdown to 30 s without turning the light off.
  3. c) Predict what risk exists if a physical push-button gets stuck and remains permanently activated, and propose a logical modification to prevent the light from staying on indefinitely.
Solve

Flowcharts

Flowchart with a start, a sensor reading, a decision diamond, and a fork into two paths depending on whether the condition is true or false. start read sensor condition? yes action A no action B

To complete a missing diamond, look at which actions come out of it and ask yourself: what must be true for the "yes" branch to happen?

Line follower

With a single sensor placed on the edge of the line, the question is "does the sensor see black?" If the answer is yes, the robot turns to one side; if not, it turns to the other. This way it moves forward zigzagging along the edge.

With two sensors (L on the left and R on the right, spaced slightly wider than the width of the line), you use conditionals in series, and the order matters:

repetir siempre:

    leer I y D

    si D ve negro: girar a la derecha

    si no, si I ve negro: girar a la izquierda

    si no: avanzar recto

As long as the line stays between the two sensors, both see white and the robot goes straight. If one sensor steps on the line, the robot turns toward that side to correct. But what does it do if both see white because it left the line on a sharp curve? With this program it keeps going straight and gets lost. One improvement is to store in a variable ultimo_giro (last turn) the side it last corrected toward, and when it gets lost (both see white for several readings in a row), turn toward that side until it finds the line again.

Practice★☆☆Not started
The flowchart in the figure controls a parking lot entry barrier, but the question inside the decision diamond is missing. Write the condition that should be checked…

The flowchart in the figure controls a parking lot entry barrier, but the question inside the decision diamond is missing. Write the condition that should be checked there and describe the complete behavior of the barrier.

Start Read push button ? yes Raise barrier Wait 5 s Lower barrier no
Solve
Practice★★☆Not started
A stair light works as follows: pressing the button turns it on; after 30 seconds it turns off automatically; and the cycle can be repeated. Draw or describe its…

A stair light works as follows: pressing the button turns it on; after 30 seconds it turns off automatically; and the cycle can be repeated. Draw or describe its flowchart and then write the corresponding pseudocode.

You can do it on paper and send a photo with your answer.

Solve
Practice★★☆Not started
The flowchart in the figure shows the program for a single-sensor line-following robot tracking the edge of the line, but the condition inside the decision diamond is…

The flowchart in the figure shows the program for a single-sensor line-following robot tracking the edge of the line, but the condition inside the decision diamond is missing. Complete it, describe what the robot does in each case, and reason what will happen when the line ends.

Start Read sensor ? yes Turn to the left no Turn to the right
Solve
Practice★★★Not started
The flowchart in the figure controls a parking lot exit barrier, with a sensor positioned beneath the barrier, but one of the conditions is missing. Complete it,…

The flowchart in the figure controls a parking lot exit barrier, with a sensor positioned beneath the barrier, but one of the conditions is missing. Complete it, describe the overall behavior, and explain what accident this second check prevents.

Start Car waiting? no yes Raise barrier ? no yes Lower barrier
Solve
Practice★★★Not started
To control a vehicle traffic light that includes a pedestrian demand push button, two programming structures are compared: Option A (linear sequential): loop with…

To control a vehicle traffic light that includes a pedestrian demand push button, two programming structures are compared:

  • Option A (linear sequential): loop with successive blocking calls wait(t) (green 30 s, amber 3 s, red 15 s).

  • Option B (state machine): the system remains in the CARS_GREEN state and only transitions state if the pedestrian push button signal is detected.

  1. a) Justify why Option A is inadequate to respond immediately to a pedestrian pressing the button and what response flaw it will cause.
  2. b) If you are asked to add a new function ("the pedestrian green light must flash 3 times for 3.0 s before turning red, but only if the sensor for visually impaired people is not active"), explain what modularity and maintenance advantages Option B presents compared to Option A.
  3. c) Design the state diagram or general pseudocode for Option B, clearly identifying the states, input events, and actions associated with each transition.
Solve

Control by time or by position sensor

A drawbridge or an electric gate is moved by a motor. How does the program know when to stop? There are two options.

Time only assumes; the limit switch verifies. Motor speed changes with load, friction, wind, battery voltage, or wear. If the bridge takes 6 s instead of 5 s, it ends up half closed; if it takes 4 s, the motor keeps pushing against the stop and overheats.

Practice★★★Not started
In a drawbridge driven by an electric gearmotor, it is proposed to replace the two physical limit switches (ls_down and ls_up) with a control based exclusively on…

In a drawbridge driven by an electric gearmotor, it is proposed to replace the two physical limit switches (ls_down and ls_up) with a control based exclusively on timing: running the motor for 5.0 s to open and 5.0 s to close.

  1. a) Predict and justify at least two real-world situations (mechanical, meteorological, or electrical) why this timed system will cause the bridge to fail to close completely or end up damaging the motor.
  2. b) Design in pseudocode the complete operating cycle: upon detecting a boat, turn the road traffic light red, wait 3.0 s for clearance, raise the bridge up to ls_up, allow river passage with a green nautical traffic light and, when the boat leaves, close down to ls_down.
  3. c) Incorporate into your algorithm a safety mechanism based on maximum time (watchdog): if more than 8.0 s elapse without the corresponding limit switch being triggered during the movement, the motor must stop immediately and activate a warning light.
Solve

Debugging

Debugging a robot program: the sensor sends readings to the board, where the condition and the variable are checked, and the values are printed through the serial port on a monitor; three hypotheses are tested in order. variable if (condition) serial port 0 1 0 1 1 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 0 real values 3 erratic sensor 2 variable never read 1 inverted condition each hypothesis is tested separately, from most to least likely

A threshold with two values

When an analog reading keeps oscillating around the threshold, a single comparison value makes the output switch back and forth. An automatic irrigation system that turns the pump on when humidity is $< 30.0\ \%$ and turns it off as soon as it reaches $30.0\ \%$ or more, with a reading that jumps between $29.8\ \%$ and $30.2\ \%$, would turn the pump on and off every second: the motor wears out and current spikes occur.

The solution is to use two thresholds: one to turn on and another, higher one, to turn off. Between the two values the output keeps its state. This intermediate zone is called a dead band, and the technique is called hysteresis.

Practice★★☆Not started
A program counts cars: "repeat forever: wait for sensor to change from 0 to 1; add 1 to cars variable". Explain what it does, what purpose it serves, and what problem…

A program counts cars: "repeat forever: wait for sensor to change from 0 to 1; add 1 to cars variable". Explain what it does, what purpose it serves, and what problem arises if a car stops right over the sensor.

Solve
Practice★★☆Not started
A fan control program states: "if temperature is greater than $26\ ^\circ\text{C}$, turn off fan; if less than $26\ ^\circ\text{C}$, turn on fan". Describe the actual…

A fan control program states: "if temperature is greater than $26\ ^\circ\text{C}$, turn off fan; if less than $26\ ^\circ\text{C}$, turn on fan". Describe the actual behavior of the fan and write the corrected program.

Solve
Practice★★★Not started
The control system of an automatic toll barrier has a presence sensor 1 (just before the barrier) and a sensor 2 (just after). An operator proposes this code: a) Predict…

The control system of an automatic toll barrier has a presence sensor 1 (just before the barrier) and a sensor 2 (just after). An operator proposes this code:

repeat forever:

    if (sensor_1 == DETECTED) then:

        raise_barrier()

    if (sensor_1 == CLEAR) then:

        lower_barrier()
  1. a) Predict what accident will physically occur as soon as the front of the car passes sensor 1 if the rear part is still under the barrier. Justify why the operator's proposal to "add a 5.0 s pause before lowering" does not solve the problem with slow traffic or articulated vehicles.
  2. b) Design in pseudocode a safe algorithm that uses both sensors to ensure that the barrier raises when the vehicle arrives, stays up as long as either of the two sensors detects presence, and only lowers when the vehicle has completely cleared sensor 2.
Solve
Practice★★★Not started
A robotics team is testing a line-following robot with two infrared sensors (L and R) spaced 2.0 cm apart, designed to follow a 1.5 cm black line on a white background.…

A robotics team is testing a line-following robot with two infrared sensors (L and R) spaced 2.0 cm apart, designed to follow a 1.5 cm black line on a white background. The current program applies classic logic: move straight if both see white, turn right if R steps on black, and turn left if L steps on black.

  1. a) Predict what will happen on a very sharp 90° turn if the robot enters fast and both sensors end up simultaneously on the white background off the line. Justify why the basic algorithm fails.
  2. b) Design in pseudocode an improvement that uses a memory variable (last_seen) so the robot knows which way to turn when it completely loses the line.
  3. c) If the track had a wide black line of 3.5 cm (wider than the distance between sensors), predict the robot's behavior on a straight section and explain what additional condition you should program to handle when both sensors detect black at the same time.
Solve
Practice★★★Not started
An installer programs a home alarm control unit with a magnetic sensor on the door, another on the window, and an arming switch using this algorithm: a) Analyze the code…

An installer programs a home alarm control unit with a magnetic sensor on the door, another on the window, and an arming switch using this algorithm:

repeat forever:

    if (armed == TRUE) and (door == OPEN or window == OPEN) then:

        activate_siren()

        wait(60) // seconds

        deactivate_siren()
  1. a) Analyze the code and explain two critical security flaws: what happens if an intruder opens the window, enters, and closes it again within 3.0 s? And what happens if the 60 s elapse and the intruder is still inside with the window open?
  2. b) Design in pseudocode a robust algorithm with state variables that ensures: 1) once triggered, the siren will sound for at least 60 s even if the access point is closed again; 2) it will not turn off as long as any sensor remains open; and 3) if the user turns off the arming switch, the siren will stop immediately at any moment.
Solve
Practice★★★Not started
To automate the watering of a school garden, an analog moisture sensor and an electric pump are available. A first program applies this rule: "repeat forever: if…

To automate the watering of a school garden, an analog moisture sensor and an electric pump are available. A first program applies this rule: "repeat forever: if moisture < 30.0 %, turn pump on; else, turn pump off; wait 1 second".

  1. a) If the sensor reading rapidly fluctuates between 29.8 % and 30.2 % due to localized absorption or electrical noise, predict how the electric pump will behave and what mechanical and electrical consequences it will have.
  2. b) Explain why replacing the wait with "wait 10 minutes" does not guarantee optimal watering either (analyze the delay in water infiltration and the risk of over-irrigation).
  3. c) Design a solution using hysteresis control: define two different logical moisture thresholds (start and stop) and write the structured pseudocode or describe in detail the corresponding flowchart, justifying why it prevents constant switching.
Solve

Summary

Back to the beginning: the traffic light changes by itself because a board runs, in a loop, the algorithm that someone thought out: turn on a light, wait, move on to the next one, and repeat.

See all the exercises in this section