Welcome to Algorithms and Programming: Controlling and Simulating!
Have you ever wondered how an automatic door knows when to open, how a set of traffic lights changes colour, or how video games make characters jump like they are in real life? The secret behind all of these is computer programming!
In these study notes, we will explore how we can use algorithms and computer programs to control real physical objects in the world around us and simulate physical systems inside a computer screen. Don't worry if this seems tricky at first—we will break everything down step by step!
1. Controlling vs. Simulating: What is the Difference?
Computers can interact with the physical world in two main ways: by controlling physical objects or by simulating them.
A. Controlling a Physical System
Controlling a physical system means writing a computer program to move, light up, or direct real hardware components in the real world.
• How it works: A computer or microcontroller takes in information from the real world (using sensors) and tells physical parts (like motors or lights) what to do.
• Everyday examples: Automatic streetlights turning on when it gets dark, robotic vacuums turning away from walls, or a pelican crossing changing lights when a button is pushed.
• Hardware you might use: BBC micro:bit, Crumble controller, LEGO Education / SPIKE, or programmable floor robots (like Roamer or Sphero).
B. Simulating a Physical System
Simulating a physical system means using software to create a digital model on a screen that mimics how things behave in the real world.
• How it works: The computer uses rules and math to copy real-world forces, movement, and events inside a virtual world.
• Everyday examples: A computer game where a ball falls due to gravity, a Scratch project that models a working set of traffic lights, or space flight simulators.
• Software you might use: Scratch, MakeCode, or turtle graphics / Logo-based environments.
Quick Summary: Control happens in the real, physical world with real hardware. Simulation happens inside the computer screen as a digital model!
2. The Essential Problem-Solving Toolkit
Before we can control or simulate anything, we need our fundamental computing tools:
• Algorithm: A clear, step-by-step set of rules or instructions to solve a problem or accomplish a goal. (Think of it like a recipe for a cake!)
• Program: An algorithm that has been turned into code that a digital device or computer can run and execute.
• Decomposition: Breaking down a large, complicated problem into smaller, manageable chunks. For example, instead of trying to code an entire robot car at once, you break it down into: (1) spin left wheel, (2) spin right wheel, (3) read distance sensor.
• Debugging: The step-by-step process of finding, diagnosing, and fixing mistakes (bugs) in your algorithm or code.
Top Tip for Debugging: Never guess randomly! Test your program one small piece at a time to find the exact place where something went wrong.
3. Inputs and Outputs: Talking to the World
Every control system and simulation relies on Inputs (information coming in) and Outputs (actions going out).
Physical Inputs (Sensors) vs. Physical Outputs (Actuators)
• Physical Inputs (Sensors): These detect changes in the physical world and send data to the computer.
Examples: Pushbuttons, tilt sensors, ambient light sensors, temperature sensors, motion sensors, and ultrasonic distance sensors.
• Physical Outputs (Actuators & Displays): These are physical parts that perform an action when the computer commands them.
Examples: LEDs, motors, buzzers, speakers, display screens, and robotic wheels.
Virtual Inputs vs. Virtual Outputs (In Simulations)
• Virtual Inputs: Pressing a key on the keyboard, clicking a mouse, or a sprite touching a specific coordinate trigger on the screen.
• Virtual Outputs: A sprite moving across the screen, a costume change, a sound effect played through your speakers, or a graphical animation.
Memory Trick:
• Input = Information goes IN to the computer brain.
• Output = Actions come OUT from the computer brain.
4. Programming Constructs (The Code Blocks)
To control or simulate systems accurately, we use four key building blocks in our programs:
1. Sequence
Running instructions in a clear, step-by-step, linear order from top to bottom. If the order is wrong, the system will not work properly! (Imagine putting your shoes on before your socks—order matters!)
2. Selection
Making decisions based on conditions using IF... THEN... ELSE blocks.
• Example: IF light sensor detects darkness, THEN turn LED on, ELSE turn LED off.
3. Repetition (Loops)
Repeating a block of code so we don't have to write the same instruction over and over again:
• Forever (Infinite loop): Keeps checking sensors or repeating actions constantly.
• Repeat \(n\) times (Count loop): Repeats an action for a specific number of times, like flashing a light \(5\) times.
• Repeat until (Conditional loop): Keeps doing an action until a specific condition becomes true (e.g., drive forward until the distance sensor detects an obstacle).
4. Variables
A named digital storage box in computer memory that holds a value that can change while the program runs.
• Examples: speed, timer, score, or distance_value.
5. Watch Out! Common Coding Traps to Avoid
Trap 1: The "One-Time Check" Mistake
The Mistake: You write an IF... THEN block to check if a button is pressed, but you place it on its own without a loop. When you run your program, it checks the button in less than a millisecond at startup, sees it is not pressed, and ends the program!
The Fix: Wrap your IF... THEN check inside a forever loop so the microcontroller continuously monitors the sensor all day long.
Trap 2: Assuming the Computer "Knows" What to Do
The Mistake: Expecting a robotic buggy to automatically stop before hitting a wall without being told.
The Fix: Remember that computers have zero human intuition. They follow instructions literally. You must give exact, step-by-step instructions to read the distance sensor and stop the motor.
Trap 3: Mixing Up Sensors and Actuators
The Mistake: Thinking a buzzer is an input because you can hear it, or thinking a button is an output because you press it.
The Fix: Always ask: "Is information going INTO the computer, or is an action coming OUT?" A button sends a signal in (input/sensor), while a buzzer plays a sound out (output/actuator).
6. Summary Checklist
Before you finish, check that you understand these key ideas:
• Controlling uses software to drive real-world hardware (motors, LEDs, buzzers).
• Simulating uses software to model real-world behavior on a screen (in Scratch or MakeCode).
• Inputs collect data (sensors/keys); Outputs carry out actions (actuators/sprites).
• Decomposition helps you tackle big projects by breaking them into smaller steps.
• Selection and Repetition allow your programs to make smart choices and monitor sensors continuously.